我是VC新手,最近在学习钩子,现在碰到一个问题,如何让鼠标经过某窗口的某控件时能及时得到该控件的句柄?也就是说鼠标移动到什么位置,就要及时得到鼠标所在窗口中所指的控件的句柄。在论坛里找了很多相关的提问,可没有一个写的是完整的,对于我这个新手来说实在是有些困难,所以请各位高手们尽量把主要实现代码写完整,同时也写清各变量所使用的类型。谢谢!

解决方案 »

  1.   

    Search MSDN about WindowFromPoint/ChildWindowFromPoint
      

  2.   

    typedef struct {
        POINT pt;
        HWND hwnd;
        UINT wHitTestCode;
        ULONG_PTR dwExtraInfo;
    } MOUSEHOOKSTRUCT, *PMOUSEHOOKSTRUCT;
    LRESULT CALLBACK MouseProc(          int nCode,
        WPARAM wParam,
        LPARAM lParam
    );
    ParametersnCode
    [in] Specifies a code the hook procedure uses to determine how to process the message. If nCode is less than zero, the hook procedure must pass the message to the CallNextHookEx function without further processing and should return the value returned by CallNextHookEx. This parameter can be one of the following values. 
    HC_ACTION
    The wParam and lParam parameters contain information about a mouse message.
    HC_NOREMOVE
    The wParam and lParam parameters contain information about a mouse message, and the mouse message has not been removed from the message queue. (An application called the PeekMessage function, specifying the PM_NOREMOVE flag.)
    wParam
    [in] Specifies the identifier of the mouse message. 
    lParam
    [in] Pointer to a MOUSEHOOKSTRUCT structure. 
    由此可见,MouseHook中已经可以直接取到窗口句柄了。根本不用其它什么方法了。
      

  3.   

    PMOUSEHOOKSTRUCT pmouse= NULL;
    pmouse = (PMOUSEHOOKSTRUCT)lParam;
    HWND hWnd=pmouse->hWnd;
      

  4.   

    有了窗口句柄再枚举,参考
    http://blog.csdn.net/laiyiling/archive/2004/11/21/189305.aspx
      

  5.   

    好像行的通喽,不过有一点我还不太明白,就是上面程序得到的句柄是HWND型的,而你提供的网址里的句柄是CWnd型的,这两种不一样哦,怎么写啊?请赐教!谢谢
      

  6.   

    由HWND得到CWnd,采用下面的函数CWnd::FromHandle  
    static CWnd* PASCAL FromHandle( HWND hWnd );
      

  7.   

    http://www.vckbase.com/vckbase/vckbase12/vc/nonctrls/system_30/1230006.htm
    like spy++ example