如何获得同名窗体的句柄
比如我同时打开了两个记事本,如何分别获得这两个窗体的句柄?
--------------------编程问答--------------------
EnumWindows
--------------------编程问答--------------------
--------------------编程问答--------------------
根据窗口标题枚举出来。
'调用方法:Seek_Window(me.hwnd,"无标题 - 记事本")
'寻找指定窗口,Seek_Window返回指定窗口句柄,相关API自己声明!
Public Function Seek_Window(byval hForm As Long,byval FormCaption As String) As Long
Dim RenValue As Long
Dim RenHWND As Long'定义窗口句柄
Dim RetValue As Long'定义成功返回窗口标题文字时之返回值
Dim WindowCaption As String*256
Dim String1 As String,String2 As String
Dim FindNum as integer
On Error Resume Next
Seek_Window=0
RenHWND=GetWindow(hForm,GW_HWNDFIRST)'寻找第一个兄弟(同级)窗口
Do
RetValue=GetWindowText(RenHWND,WindowCaption,256)'取得窗口的标题(Caption)文字
'MsgBox("窗口标题:" & WindowCaption)
If RetValue<>0 Then
String1=Left(WindowCaption,instr(WindowCaption,chr(0))-1)
String2=LCase(FormCaption)'寻找指定窗口标题
If strcomp(String1,String2)=0 Then
FindNum=FindNum+1
debug.print ("找到窗口句柄"& FindNum &":" & RenHWND
Seek_Window=RenHWND
if FindNum>=2 then Exit Function
End If
End If
RenHWND=GetWindow(RenHWND,GW_HWNDNEXT)'继续寻找
Loop Until RenHWND=0
End Function
--------------------编程问答--------------------
上面那个不行,仅限于进程内。用下面这个通用的:
'窗体Form1代码
Option Explicit
'窗体上添加一个命令按钮Command1,一个列表框List1
Private Sub Command1_Click()
EnumWindows AddressOf EnumWindowsProc, ByVal 0&
End Sub
'标准模块
Option Explicit
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
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 GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim windowCaption As String, LengthCaption As Long
LengthCaption = GetWindowTextLength(hwnd)
windowCaption = Space(LengthCaption)
Call GetWindowText(hwnd, windowCaption, LengthCaption + 1)
Form1.List1.AddItem Str$(hwnd) + " " + windowCaption
EnumWindowsProc = True
End Function
--------------------编程问答--------------------
D
--------------------编程问答--------------------
顶个看不懂
--------------------编程问答--------------------
如何分别那一个句柄是那一个窗体呢??
补充:VB , API