'一個form , 一個CommandBox, 2 labels
Private Type POINTAPI
X As Long
Y As Long
End Type
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 WindowFromPoint Lib "user32" (ByVal xPoint As Long, _
ByVal yPoint 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 OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, _
ByVal uExitCode As Long) 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 FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Const HWND_TOP = 0
Const HWND_BOTTOM = 1
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const PROCESS_QUERY_INFORMATION = &H400
Private TitleName As String
Private ClassName As String
Private hProcess As Long
Private Candisplay As Boolean
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim aa As Long
If Button = vbRightButton Then
'將window設定成在底層
aa = SetWindowPos(Me.hwnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE)
Me.MousePointer = vbArrowQuestion
End If
End Sub
Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single,
Y As Single)
Dim aa As Long
Dim pt As POINTAPI
Dim hwnd5 As Long
Dim str5 As String
Dim len5 As Long
Dim thrid As Long, pid As Long
If Button = vbRightButton Then
'設定window到上層
aa = SetWindowPos(Me.hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE)
Me.MousePointer = vbDefault
'取得mouse目前的座標,不能用mouseup Event的x,y因那是相對於form的座標
aa = GetCursorPos(pt)
'取得pt所在座標,是落在那一個window上面
hwnd5 = WindowFromPoint(pt.X, pt.Y)
len5 = 256
ClassName = String(255, 0)
'取得該window的Class name
aa = GetClassName(hwnd5, ClassName, len5)
ClassName = Left(ClassName, aa)
len5 = 256
TitleName = String(255, 0)
'取得該window的title
aa = GetWindowText(hwnd5, TitleName, len5)
TitleName = Left(TitleName, aa)
'依hwnd取得相對應的threadID(thrid), ProcessID(pid)
thrid = GetWindowThreadProcessId(hwnd5, pid)
'取得Process Handle
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid)
'有了hProcess那可做事可多啦,例如:可偵測 它何時結束,或強制它結束等等
'Call TerminateProcess(hProcess, 38) '最好別這麼做,否則,嘿嘿,你自己試
Label1.Caption = " Title = " + TitleName
Label2.Caption = "Calss Name = " + ClassName
End If
End Sub
|