vb 判断控件是否有焦点
我是指判断其他窗口的控件哦比如1.exe打开后有一个text1和text2怎样在另一个程序中判断1.exe中的text1是否有焦点?(不是1.EXE的窗体窗体有焦点)……...
我是指判断其他窗口的控件哦 比如1.exe打开后有一个text1和text2怎样在另一个程序中判断1.exe中的text1是否有焦点?(不是1.EXE的窗体窗体有焦点)
……用什么API?
GetForegroundWindow好像只能获得当前激活的窗体,与控件无关。
ynisue,怎么用?
用findwindowex找到控件句柄后怎么判断?sendmessage发送什么? 展开
……用什么API?
GetForegroundWindow好像只能获得当前激活的窗体,与控件无关。
ynisue,怎么用?
用findwindowex找到控件句柄后怎么判断?sendmessage发送什么? 展开
3个回答
展开全部
给你写一段简单的Demo吧。(窗体中一个timer,两个label)
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPointXY Lib "user32" Alias "WindowFromPoint" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetFocus Lib "user32" () As Long
Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Sub Form_Load()
SetWindowPos Me.hwnd, -1, 0, 0, 240, 100, conSwpNoActivate Or conSwpShowWindow
End Sub
Private Sub Timer1_Timer()
Dim xy As POINTAPI
Dim ahwnd As Long
GetCursorPos xy
ahwnd = WindowFromPointXY(xy.x, xy.y)
Label1.Caption = "鼠标所在的控件类型:" & ClassName(ahwnd)
Label2.Caption = "获得焦点的控件类型:" & ClassName(GetHwnd)
End Sub
Public Function GetHwnd() As Long
Dim hwnd As Long
Dim PID As Long
Dim TID As Long
Dim hWndFocus As Long
hwnd = GetForegroundWindow
If hwnd Then
TID = GetWindowThreadProcessId(hwnd, PID)
AttachThreadInput App.ThreadID, TID, True
GetHwnd = GetFocus
AttachThreadInput App.ThreadID, TID, False
End If
End Function
Private Function ClassName(nHwnd As Long) As String
Dim str As String
str = Space(255)
GetClassName nHwnd, str, 255
ClassName = str
End Function
自己再扩展下就可以了。
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPointXY Lib "user32" Alias "WindowFromPoint" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetFocus Lib "user32" () As Long
Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Sub Form_Load()
SetWindowPos Me.hwnd, -1, 0, 0, 240, 100, conSwpNoActivate Or conSwpShowWindow
End Sub
Private Sub Timer1_Timer()
Dim xy As POINTAPI
Dim ahwnd As Long
GetCursorPos xy
ahwnd = WindowFromPointXY(xy.x, xy.y)
Label1.Caption = "鼠标所在的控件类型:" & ClassName(ahwnd)
Label2.Caption = "获得焦点的控件类型:" & ClassName(GetHwnd)
End Sub
Public Function GetHwnd() As Long
Dim hwnd As Long
Dim PID As Long
Dim TID As Long
Dim hWndFocus As Long
hwnd = GetForegroundWindow
If hwnd Then
TID = GetWindowThreadProcessId(hwnd, PID)
AttachThreadInput App.ThreadID, TID, True
GetHwnd = GetFocus
AttachThreadInput App.ThreadID, TID, False
End If
End Function
Private Function ClassName(nHwnd As Long) As String
Dim str As String
str = Space(255)
GetClassName nHwnd, str, 255
ClassName = str
End Function
自己再扩展下就可以了。
展开全部
Private Option_Focus_Status As Boolean
Private Sub Option1_GotFocus()
Option_Focus_Status = True
End Sub
Private Sub Option1_LostFocus()
Option_Focus_Status = False
End Sub
定义一个模块儿级别或者全局变量,然后用GotFocus & LostFocus来给变量赋值,通过判断此变量的数值来判断焦点是否失去
Private Sub Option1_GotFocus()
Option_Focus_Status = True
End Sub
Private Sub Option1_LostFocus()
Option_Focus_Status = False
End Sub
定义一个模块儿级别或者全局变量,然后用GotFocus & LostFocus来给变量赋值,通过判断此变量的数值来判断焦点是否失去
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
主要用以下几个API:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName 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 FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName 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 FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询