我尝试从另一个应用程序(例如记事本或写字板)读取文本。 我必须这样做,在VB 6巫婆我没有编程,但我不能使用其他语言。从另一个应用程序的文本框中读取文本
我发现代码Vb.Net,我修改了它的VB 6,但我总是得到错误:“运行时错误‘424’:所需的对象”
代码是:
Const WM_GETTEXTLENGTH = &HE
Const WM_GETTEXT = &HD
Private Declare Function FindWindow Lib "USER32" Alias "FindWindowA" (_
ByVal lpClassName As String, _
ByVal lpWindowName As String _
) As Long
Private Declare Function FindWindowEx Lib "USER32" Alias "FindWindowExA" (_
ByVal hWndParent As Long, _
ByVal hWndChildAfter As Long, _
ByVal lpszClassName As String, _
ByVal lpszWindowName As String _
) As Long
Private Declare Function GetWindowText Lib "USER32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "USER32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA" (_
ByVal hWnd As Long, ByVal Msg As Long, _
wParam As Any, lParam As Any) As Long
Private Sub Command1_Click()
Dim notepadHandle As Long
notepadHandle = FindWindow("Notepad", vbNullString)
Dim childhandle As Long
childhandle = FindWindowEx(notepadHandle, 0, "Edit", vbNullString)
If notepadHandle = 0 Then
MessageBox.Show ("can't find")
Else
'MessageBox.Show (CStr(childhandle))
' MessageBox.Show (WindowText(childhandle))
MessageBox.Show (WindowText(childhandle))
End If
End Sub
Public Function WindowText(ByVal window_hwnd As Long) As String
Dim txtlen As Integer
WindowText = ""
If window_hwnd = 0 Then Exit Function
txtlen = SendMessage(window_hwnd, WM_GETTEXTLENGTH, 0, _
0)
If txtlen = 0 Then Exit Function
txtlen = txtlen + 1
Dim txt As String
txt = String(txtlen + 1, Chr$(0))
txt = Convert.ToString(SendMessage(window_hwnd, WM_GETTEXT, txtlen, txt))
WindowText = Convert.ToString(txt)
End Function
我在哪里犯错?
Thanx
我的vb6有静态变量。和静态程序。 Static Statement 用于过程级声明变量并分配存储空间。只要代码正在运行,用Static语句声明的变量就会保留它们的值。 – phd443322
@ phd443322我真的在考虑将类定义为“静态”,而不是变量。 –
属性/方法在类中可以是静态的。 Property Set Statement 声明形成Property过程的主体的名称,参数和代码,该过程设置对对象的引用。 语法 [公众|私人|朋友] [静态]属性设置名称([arglist中,参考) [声明] [退出房产] [声明] 高端物业 – phd443322