VB6 根据进程ID(PID)获取窗体句柄

模块代码

Option Explicit

Private Const GW_HWNDNEXT = 2

Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function Getwindow Lib "user32" (ByVal hwnd As Long,ByVal wCmd 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 GetwindowThreadProcessId Lib "user32" (ByVal hwnd As Long,lpdwprocessid 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

Private Function ProcIDFromWnd(ByVal hwnd As Long) As Long
Dim idProc As Long

' Get PID for this HWnd
GetwindowThreadProcessId hwnd,idProc

' Return PID
ProcIDFromWnd = idProc
End Function

Public Function GetWinHandle(hInstance As Long) As Long
Dim tempHwnd As Long

' Grab the first window handle that Windows finds:
tempHwnd = FindWindow(vbNullString,vbNullString)

' Loop until you find a match or there are no more window handles:
Do Until tempHwnd = 0
' Check if no parent for this window
If GetParent(tempHwnd) = 0 Then
' Check for PID match
If hInstance = ProcIDFromWnd(tempHwnd) Then
' Return found handle
GetWinHandle = tempHwnd
' Exit search loop
Exit Do
End If
End If

' Get the next window handle tempHwnd = Getwindow(tempHwnd,GW_HWNDNEXT) Loop End Function

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...