用VB编写一段枚举窗口句柄的代码
枚举所有类名为ABC的、可以发送按键消息的窗口句柄用VB编写、要源码并附详细说明!需要注意的是得到的句柄必须可以发送按键消息。比如可以枚举出所有梦幻诛仙、大明龙权等等游戏...
枚举所有类名为ABC的、可以发送按键消息的窗口句柄用VB编写、要源码并附详细说明!需要注意的是得到的句柄必须可以发送按键消息。比如可以枚举出所有 梦幻诛仙、大明龙权等等游戏的窗口句柄附件中有个例子,但是无法得到梦幻诛仙、大明龙权等游戏的窗口句柄,可以做为参考!编写格式要按例子中的格式,窗体、用户控件、模块三部分。窗体下不要写代码。建议在其基础上修改。以下有二段得到可发送按键消息的窗口句柄的代码,一种是C写的,一种是C转成的VB。但不是枚举所有,可以做为参考。C代码:// GetKeyFocusWnd 得到当前可以发送按键消息的窗口(窗口句柄)long CQMPlugin::GetKeyFocusWnd(){ HWND hWnd = GetForegroundWindow(); DWORD dwTargetThreadID = GetWindowThreadProcessId(hWnd,NULL); DWORD dwCurrentThreadID = GetCurrentThreadId(); BOOL bRetVal = AttachThreadInput(dwCurrentThreadID,dwTargetThreadID,TRUE); GUITHREADINFO sThreadInfo = {0}; sThreadInfo.cbSize = sizeof(sThreadInfo); bRetVal = GetGUIThreadInfo(dwTargetThreadID,&sThreadInfo); HWND hTargetWnd = hWnd; if(sThreadInfo.hwndFocus) hTargetWnd = sThreadInfo.hwndFocus; else if(sThreadInfo.hwndActive) hTargetWnd = sThreadInfo.hwndActive; AttachThreadInput(dwCurrentThreadID,dwTargetThreadID,FALSE); return (long)hTargetWnd;} VB代码:Private Type RECTLeft As LongTop As LongRight As LongBottom As LongEnd Type Private Type PGUITHREADINFOcbSize As Longflags As LonghWndActive As LonghwndFocus As LonghwndCapture As LonghwndMenuOwner As LonghwndMoveSize As LonghwndCaret As LongrcCaret As RECTEnd TypePrivate Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As LongPrivate Declare Function GetForegroundWindow Lib "user32" () As LongPrivate Declare Function GetCurrentThreadId Lib "kernel32" () As LongPrivate Declare Function GetGUIThreadInfo Lib "user32" (ByVal idThread As Long, ByRef pgui As PGUITHREADINFO) As LongPrivate Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As LongFunction GetKeyFocusWnd() As LongDim hwnd As LongDim dwTargetThreadID As LongDim dwCurrentThreadID As LongDim bRetVal As BooleanDim sThreadInfo As PGUITHREADINFOhwnd = GetForegroundWindowdwTargetThreadID = GetWindowThreadProcessId(hwnd, vbNull)dwCurrentThreadID = GetCurrentThreadIdbRetVal = AttachThreadInput(dwCurrentThreadID, dwTargetThreadID, True) sThreadInfo.cbSize = Len(sThreadInfo)bRetVal = GetGUIThreadInfo(dwTargetThreadID, sThreadInfo) Dim hTargetWnd As LonghTargetWnd = hwnd If sThreadInfo.hwndFocus Then hTargetWnd = sThreadInfo.hwndFocusElseIf sThreadInfo.hWndActive Then hTargetWnd = sThreadInfo.hWndActiveEnd IfAttachThreadInput dwCurrentThreadID, dwTargetThreadID, False GetKeyFocusWnd = hTargetWndEnd Function
展开
1个回答
2011-08-12
展开全部
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Function EnumWindowsProc(ByVal lngHwnd As Long, ByVal lParam As Long) As Boolean
Dim s As String * 255
GetWindowText lngHwnd, s, 255
If InStr(s, "记事本") <> 0 Then Debug.Print lngHwnd
EnumWindowsProc = True
End Function
'==================窗体代码如下~句柄显示在立即窗口
Private Sub Command1_Click()
EnumWindows AddressOf EnumWindowsProc, 0&
End Sub
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Function EnumWindowsProc(ByVal lngHwnd As Long, ByVal lParam As Long) As Boolean
Dim s As String * 255
GetWindowText lngHwnd, s, 255
If InStr(s, "记事本") <> 0 Then Debug.Print lngHwnd
EnumWindowsProc = True
End Function
'==================窗体代码如下~句柄显示在立即窗口
Private Sub Command1_Click()
EnumWindows AddressOf EnumWindowsProc, 0&
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询