form1窗体: Option ExplicitPrivate Sub Command1_Click() Form2.Show End Subform2窗体 Option ExplicitPrivate Sub Command1_Click() MsgBox Form2.hWnd End Sub
上面那个写错窗体了。form1窗体: Option ExplicitPrivate Sub Command1_Click() Form2.Show End Subform2窗体 Option ExplicitPrivate Sub Command1_Click() MsgBox Form1.hWnd End Sub
我问错了, 不是本身和MID窗体阿, 是其它程序运行的窗体.
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Longdim lhwnd as long lhwnd = findwindow(vbnullstring,"窗体的名称")
可以的.如果是VB本身的窗体,当然好办,但是,如果不是VB本身的窗体,必须引用API. 下面这句API就是取前台窗口句柄的. Private Declare Function GetForegroundWindow Lib "user32" () As Long
可以的。 module ============ Option ExplicitDeclare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean Declare Function IsWindowVisible Lib "user32" _ (ByVal hwnd As Long) As LongDeclare Function IsWindowEnabled Lib "user32" (ByVal hwnd As Long) As LongPublic Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean Dim sSave As String, Ret As Long Ret = GetWindowTextLength(hwnd) sSave = Space(Ret) GetWindowText hwnd, sSave, Ret + 1 Form1.List1.AddItem sSave EnumWindowsProc = True End Function ======== form1 ======== EnumWindows AddressOf EnumWindowsProc, ByVal 0&
上面的程序可以列出所有进程的hwnd.
对了,声明漏了一些,应该是下面这些 Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long Declare Function IsWindowVisible Lib "user32" _ (ByVal hwnd As Long) As Long Declare Function GetParent Lib "user32" _ (ByVal hwnd As Long) As LongDeclare Function IsWindowEnabled Lib "user32" (ByVal hwnd As Long) As Long
Option ExplicitPrivate Sub Command1_Click()
Form2.Show
End Subform2窗体
Option ExplicitPrivate Sub Command1_Click()
MsgBox Form2.hWnd
End Sub
Option ExplicitPrivate Sub Command1_Click()
Form2.Show
End Subform2窗体
Option ExplicitPrivate Sub Command1_Click()
MsgBox Form1.hWnd
End Sub
lhwnd = findwindow(vbnullstring,"窗体的名称")
下面这句API就是取前台窗口句柄的.
Private Declare Function GetForegroundWindow Lib "user32" () As Long
module
============
Option ExplicitDeclare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
Declare Function IsWindowVisible Lib "user32" _
(ByVal hwnd As Long) As LongDeclare Function IsWindowEnabled Lib "user32" (ByVal hwnd As Long) As LongPublic Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim sSave As String, Ret As Long
Ret = GetWindowTextLength(hwnd)
sSave = Space(Ret)
GetWindowText hwnd, sSave, Ret + 1 Form1.List1.AddItem sSave
EnumWindowsProc = True
End Function
========
form1
========
EnumWindows AddressOf EnumWindowsProc, ByVal 0&
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Declare Function IsWindowVisible Lib "user32" _
(ByVal hwnd As Long) As Long
Declare Function GetParent Lib "user32" _
(ByVal hwnd As Long) As LongDeclare Function IsWindowEnabled Lib "user32" (ByVal hwnd As Long) As Long