我嘗試從另一個應用程序(例如記事本或寫字板)讀取文本。 我必須這樣做,在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