GetForegroundWindow 获取当前置顶的窗口 GetCursorPos( ) 取得鼠标当前所在的坐标判断鼠标是否在这置前的窗体内 ****************************************************************另种方法使用 WindowFromPoint与GetCursorPos 这两个APIPrivate Sub Timer1_Timer() GetCursorPos Pt HwndVal = WindowFromPoint(Pt.X, Pt.Y) If HwndVal = Me.hWnd Then Me.Caption = "鼠标在窗体上" ElseIf HwndVal = Picture1.hWnd Then Me.Caption = "鼠标在图片框内" ElseIf HwndVal = Text1.hWnd Then Me.Caption = "鼠标在文本框内" End If End Sub
'根据窗口句柄得到应用程序的路径 Public Function AppPath(Optional ByVal hwn1) As String Dim dwProcessID As Long, hProcess As Long, hModule As Long Dim nSize As Long If IsMissing(hwn1) Then '如果没写则表示处理当前存储的句柄 If hWnd_ > 0 Then dwProcessID = ProcessID() 'GetWindowThreadProcessId hWnd_, dwProcessID Else dwProcessID = ProcessID(hwn1) End If hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, dwProcessID) AppPath = Space(255) nSize = GetModuleFileNameEx(hProcess, 0, StrPtr(AppPath), 255) AppPath = Mid(AppPath, 1, nSize) Call CloseHandle(hProcess) End Function通过WindowFromPoint得到的句柄(不用管控件还是窗口的)可以直接传入这个函数得到程序路径。相关api函数和常数声明自己补充。 函数是摘自clswindow类,如果懒得找声明可以直接下载,然后调用。w.hWnd = 332302 '自己通过WindowFromPoint得到 MsgBox w.AppName
给你一个参考方法:
1. GetCursorPos( ) 取得鼠标坐标。
2. WindowFromPoint( ) 取得鼠标处的窗口句柄。
注意,获取到的有可能是控件句柄,或者“子窗口”句柄,
这是就要“往上找”父窗口,直到它的“父窗口”是“桌面”为止。
3. 按“顶级窗口”的句柄,用 GetWindowThreadProcessId( )获取到窗口所属进程的PID。 有了“PID”,剩下的事,就有N多种方法处理了,自己搜索一下相关API吧……
****************************************************************另种方法使用 WindowFromPoint与GetCursorPos 这两个APIPrivate Sub Timer1_Timer()
GetCursorPos Pt
HwndVal = WindowFromPoint(Pt.X, Pt.Y)
If HwndVal = Me.hWnd Then
Me.Caption = "鼠标在窗体上"
ElseIf HwndVal = Picture1.hWnd Then
Me.Caption = "鼠标在图片框内"
ElseIf HwndVal = Text1.hWnd Then
Me.Caption = "鼠标在文本框内"
End If
End Sub
Public Function AppPath(Optional ByVal hwn1) As String
Dim dwProcessID As Long, hProcess As Long, hModule As Long
Dim nSize As Long
If IsMissing(hwn1) Then '如果没写则表示处理当前存储的句柄
If hWnd_ > 0 Then dwProcessID = ProcessID() 'GetWindowThreadProcessId hWnd_, dwProcessID
Else
dwProcessID = ProcessID(hwn1)
End If
hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, dwProcessID)
AppPath = Space(255)
nSize = GetModuleFileNameEx(hProcess, 0, StrPtr(AppPath), 255)
AppPath = Mid(AppPath, 1, nSize)
Call CloseHandle(hProcess)
End Function通过WindowFromPoint得到的句柄(不用管控件还是窗口的)可以直接传入这个函数得到程序路径。相关api函数和常数声明自己补充。 函数是摘自clswindow类,如果懒得找声明可以直接下载,然后调用。w.hWnd = 332302 '自己通过WindowFromPoint得到
MsgBox w.AppName