VB获取外部程序控件中的参数。
各位朋友,我想用VB获取外部程序控件中的参数,以及修改里面的参数。
是这样的,我用spy++获取一个外部程序内部信息,参数如下。
窗口句柄:304E4
窗口类名:ThunderRT6ComboBox
标题文本:81449281
进程ID: 1590
1。我现在想用VB获取标题文本中的字符,并显示在Text1.text中
2。写入数据到标题文本中。或者说如果标题文本为空就写入"81449281"到里面。
请问各位朋友该怎么实现呢。谢谢大家了。
[解决办法]
标准模块:
'标准模块
Option Explicit
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
Public Const WM_GETTEXT As Long = &HD&
Public Const WM_SETTEXT As Long = &HC&
Public GetHwnd As Long
'Private Declare Function GetWindow Lib "user32.dll" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
'Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
'Public Const GW_HWNDFIRST As Long = 0
'Private Const GW_HWNDNEXT As Long = 2
'Public Const GW_CHILD As Long = 5
'Public Const GW_ENABLEDPOPUP As Long = 6
Private Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Public Declare Function EnumChildWindows Lib "user32.dll" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
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
Public Function EnumWindowsProc(ByVal hWnd As Long, ByVal lParam As Long) As Boolean
Dim WindowCaption As String, LengthCaption As Long ', WindowClassName As String * 256
LengthCaption = GetWindowTextLength(hWnd)
WindowCaption = Space(LengthCaption)
Call GetWindowText(hWnd, WindowCaption, LengthCaption + 1)
If InStr(1, WindowCaption, "Form1") > 0 Then
'GetHwnd = hWnd
EnumChildWindows hWnd, AddressOf EnumChildWindowsProc, ByVal 0&
EnumWindowsProc = False
End If
'Call GetClassName(hWnd, WindowClassName, 256)
'WindowClassName = Left(WindowClassName, InStr(WindowClassName, Chr(0)) - 1)
'Form1.List1.AddItem hWnd & " 标题:" & WindowCaption & " 类名:" & WindowClassName
EnumWindowsProc = True
End Function
Public Function EnumChildWindowsProc(ByVal hWnd As Long, ByVal lParam As Long) As Boolean
Dim WindowCaption As String, LengthCaption As Long, WindowClassName As String * 256
LengthCaption = GetWindowTextLength(hWnd)
WindowCaption = Space(LengthCaption)
Call GetWindowText(hWnd, WindowCaption, LengthCaption + 1)
Call GetClassName(hWnd, WindowClassName, 256)
'WindowClassName = Left(WindowClassName, InStr(WindowClassName, Chr(0)) - 1)
If InStr(1, WindowClassName, "Edit") > 0 Then
'GetHwnd = hWnd
Dim cText As String * 256
Call SendMessage(hWnd, WM_GETTEXT, 256, ByVal cText)
Form2.Text1.Text = Left(cText, InStr(cText, Chr(0)) - 1)
If Len(Trim(Form2.Text1.Text)) = 0 Then
cText = "81449281" '为空就设置为81449281
Call SendMessage(hWnd, WM_SETTEXT, Len(cText), ByVal cText)
End If
EnumChildWindowsProc = False
End If
'Form1.List2.AddItem hWnd & " 标题:" & WindowCaption & " 类名:" & WindowClassName
'Debug.Print hWnd
'Form1.List2.AddItem hWnd & " " & WindowCaption
EnumChildWindowsProc = True
End Function
Option Explicit
Private Sub Form_Load()
EnumWindows AddressOf EnumWindowsProc, ByVal 0&
End Sub