http://expert.csdn.net/Expert/topic/2681/2681922.xml?temp=.4731562这一贴中我发贴时头脑发昏,竟然把Index选项卡认为是Search选项卡.结果问了这个问题.Search选项卡中的EDIT框是可以通过鼠标捕获得到的.还可以通过FindWindowEx得到.但是关键字输入的却不能通过鼠标捕获得到.

解决方案 »

  1.   

    没看出有什么本质区别:
    Option ExplicitPrivate Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Private Declare Function putFocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd 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 SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    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 ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    Private Declare Function GetFocus Lib "user32" () As Long
    Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
    Private Const WM_SETTEXT = &HCPrivate Sub Command1_Click()
        Dim s As String
        s = "VB技巧" '换成你要查找的窗口标题
        Dim mhwnd As Long
        mhwnd = FindWindow("HH Parent", s)
        '显示找到的窗口
        ShowWindow mhwnd, vbNormalFocus
        '切换到索引页
        SendKeys "%N", 1
        Dim texthwnd As Long
        '使编辑框获得输入焦点
        SendKeys "%w"
        '获得编辑框的句柄,texthwnd即为所求
        texthwnd = GetFocus
        '设置编辑框的内容
        SendMessage texthwnd, WM_SETTEXT, 0&, ByVal "this is test"
        '以下程序没大用,验证用的,获得编辑框的类名
        Dim ss As String * 256
        GetClassName texthwnd, ss, 256
        Debug.Print Left(ss, InStr(1, ss, Chr(0)) - 1)
        
        
    End Sub
    窗体上只能有一个按钮(如果有其它控件的话,要临时改变他们的Enabled属性)
      

  2.   

    '切换到索引页
    SendKeys "%N", 1这是和http://expert.csdn.net/Expert/topic/2681/2681922.xml?temp=.3117029中我的回复中唯一有区别的地方
      

  3.   

    不行的GetFocus()只对目标线程有效
      

  4.   

    //GetFocus()只对目标线程有效根据微软的解释,上面的结论应该是正确的(GetFocus:如果调用GetFocus函数的线程没有具有焦点的窗口,GetFocus将返回NULL。在Win16下,该函数将返回具有焦点的窗口,而无论哪个任务拥有该窗口。)可是,我在win2k下确实调用成功了(希望有win2k的朋友帮助测试一下(窗体上只有一个按钮,代码见上面的帖子),不会是我的win2k中邪了吧)事实上,调用GetFocus得到其他应用程序的键盘光标所在窗口句柄,一般需要调用attachThreadInput()函数AttachThreadInput()函数的原形如下: 
    BOOL AttachThreadInput( 
    DWORD idAttach, // 需要附加的线程ID 
    DWORD idAttachTo, // 附加到的线程ID 
    BOOL fAttach // true 附加 false 取消 
    ); 可是问题是,我没用attachThreadInput就可以通过GetFocus获得其它线程的窗口句柄,奇怪!不解!
      

  5.   

    Win98下调试通过Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Private Declare Function GetDlgCtrlID Lib "user32.dll" (ByVal hwnd As Long) As LongPrivate Declare Function GetFocus Lib "user32" () As LongPrivate Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Const WM_LBUTTONDOWN As Long = &H201
    Private Const WM_LBUTTONUP As Long = &H202
    Private Const MK_LBUTTON As Long = &H1Private Const WM_SETTEXT = &HC
    Private Sub Command1_Click()
        Dim hWndMain As Long
        Dim hWndChild As Long
        Dim hWndTab As Long
        Dim hWndEdit As Long
        
        '得到主窗体的句柄
        hWndMain = FindWindow("HH Parent", vbNullString)
        'Debug.Print "Main:"; hWndMain
        
        '得到Tab控件的句柄
        hWndChild = 0
        Do
            hWndChild = FindWindowEx(hWndMain, hWndChild, "HH Child", vbNullString)
            If hWndChild = 0 Then Exit Do
            hWndTab = FindWindowEx(hWndChild, 0, "SysTabControl32", vbNullString)
            If hWndTab <> 0 Then Exit Do
        Loop
        'Debug.Print "Tab:"; hWndTab
        
        '切换到“索引”页
        Call SendMessage(hWndTab, WM_LBUTTONDOWN, MK_LBUTTON, ByVal &H80050) '&H80050是坐标(80,8)
        
        '得到索引文本框的句柄
        hWndEdit = FindWindowEx(hWndChild, 0, "Edit", vbNullString)
        'Debug.Print "Edit:"; hWndEdit
        
        '设置文字
        Call SendMessage(hWndEdit, WM_SETTEXT, 0, ByVal "这是“索引”文本框")
        
    End Sub