如何获取任何窗口中焦点控件的hwnd?(online)

解决方案 »

  1.   

    在任何程序界面,光标在一文本框上,按某一快捷键运行一个小程序,要求这个小程序能够获取原活动窗体,活动文本框的hwnd,并通过程序修改其文本内容修改文本框文本是否用sendmessage实现?
    但是如何获取活动文本框的hwnd?
      

  2.   

    Declare Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow" () As Long 
    说明 
    获得前台窗口的句柄。这里的“前台窗口”是指前台应用程序的活动窗口 
    返回值 
    Long,前台窗口的句柄 
    注解 
    windows nt支持多个桌面,它们相互间是独立的。每个桌面都有自己的前台窗
     
      

  3.   

    Option ExplicitPrivate Type POINTAPI
        X As Long
        Y As Long
    End Type
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As LongPrivate Sub Timer1_Timer()
        Dim P As POINTAPI, h As Long
        
        '获取鼠标当前位置的坐标,以屏幕为坐标系
        GetCursorPos P
        
        '获取当前鼠标所在点所属窗口(控件)的句柄
        h = WindowFromPoint(P.X, P.Y)
    End Sub
      

  4.   

    我晕,按楼主的意思并不是上面的答案,虽然结帖了,但还是用这个比较好:'------------声明-------------
    Private Declare Function GetFocus Lib "user32" () As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function AttachThreadInput Lib "user32.dll" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
    Private Declare Function GetForegroundWindow Lib "user32.dll" () As LongPrivate Type POINTAPI
            x As Long
            y As Long
    End Type
    Dim idAttach As Long, hWnd As Long
    idAttach = GetWindowThreadProcessId(GetForegroundWindow(), 0&)
    If idAttach <> App.ThreadID Then AttachThreadInput idAttach, App.ThreadID, True
    hWnd = GetFocus()  'hWnd就是获取的活动控件
    AttachThreadInput idAttach, App.ThreadID, False