如何使用findwindow找窗口名为“第XXX号选项的内容”的窗口句柄?
其中XXX是可变的 比如第245号选项的内容、第114号选项的内容
窗口的类型已知 只是这个XXX不确定我只需要找到窗口名中 带有 “第”和“号选项的内容”的窗口句柄就行
也就是说 不管其中的XXX是多少 都能准确找到这个窗口(经证实,同一时刻只会有一个这样的窗口)如何准确找到我想要找的那个窗口的句柄?
好心的大哥们帮帮忙

解决方案 »

  1.   

    用enumwindow枚举吧,在回调中调用getwindowtext获得窗口的标题,然后根据标题进行判断
      

  2.   

    呵呵:)
    一个窗体一个模块:
    窗体上一个按钮,代码如下:
    Option ExplicitPrivate Sub Command1_Click()
        'call the Enumwindows-function
        EnumWindows AddressOf EnumWindowsProc, ByVal 0&
        Debug.Print FindHwnd '所求
    End Sub
    模块代码如下:
    Option Explicit
    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, lpString As Byte, ByVal cch As Long) As Long
    Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    Public FindHwnd As Long
    Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
        Dim sSave As String, Ret As Long, Buff() As Byte
        Ret = GetWindowTextLength(hwnd)
        If Ret > 7 Then'这个7是专门针对你的问题的设定,它可以使程序更快一些
            ReDim Buff(Ret - 1)
            GetWindowText hwnd, Buff(0), Ret + 1
            sSave = StrConv(Buff, vbUnicode)
            If Left(sSave, 1) = "第" And Right(sSave, 6) = "号选项的内容" Then
                FindHwnd = hwnd
                Exit Function
            End If
        End If
        'continue enumeration
        EnumWindowsProc = True
    End Function需要注意的是我修改了GetWindowText的声明,以及我程序中GetWindowText的用法,个人感觉这种做法更科学一些
      

  3.   

    请问 怎么样给分的啊 ? 
    我新人 不懂 貌似我没有分。。
    完了 死定了。 
    给Q币吧 HOHO
      

  4.   

    Private Declare Function FindWindow Lib "user32" Alias _
    "FindWindowA" (ByVal lpClassName As String, _
    ByVal lpWindowName As String) As LongPrivate Sub Command1_Click()
        Dim FindHwnd As Long
        Dim i As Integer
        Dim s
        For i = 0 To Forms.Count - 1
            s = Forms(i).Caption
            If InStrRev(s, "第") > 1 And InStrRev(s, "号") > 1 Then
                FindHwnd = FindWindow(vbNullString, s)
            End If
       Next i
    End Sub请教rainstormmaster(暴风雨 v2.0) :这样对不对?