当鼠标不在我的程序框架或视图区域内, 我如何能捕获鼠标的相关消息并确定鼠标的位置.
具体讲就是:  当鼠标在系统桌面或其它程序的窗口区域内发生 LButtonDown LButtonUp  或 MouseMove消息时,我怎么能够截获这些消息并在我自己的程序里进行处理?

解决方案 »

  1.   

    做钩子,还讲简单!有没有搞错啊,这可不是闹着玩的。
    我见过,在LButtonDwon时用SetCapture()打开一个什么,然后在MouseMove中用
    if(GetCapture() != this)来判断是否在本窗口,最后用ReleaseCapture()释放监视的一种用法,它可以有效的控制鼠标在本窗口中的移动,出了本窗口就不知道还能不能用了!
    不知楼主到底要实现什么功能啊,就屏幕取色之类的东西吗?为什么要截其它程序窗口区域内的鼠标消息呢?
      

  2.   

    if(GetCapture() != this)//需要吗
      

  3.   

    coolstar14(寒星溪月)
    你的方法才叫难呢?我都有些听不懂,考虑的东西太多了,要是用全局HOOK也就30行代码吧
      

  4.   

    SetCapture() will restrain the mouse move within the client area of your current view. You can define a global hook in a dll, that's not complex. Refer to the MSDN for further information.
      

  5.   

    hook吧
    如果有两个程序都用SetCapture
    其中一个会失效的
    不是很保险
      

  6.   

    SetCapture(hwnd);
    ...
    ReleaseCapture();
      

  7.   

    hook?  怎么搞定?是不是太难了点吧?
    我只是想试着做一个涂鸦程序,实现能在屏幕上的任何地方用鼠标乱写乱画的小东东。
    有没有更简单的办法呀?多谢各位大虾的指点。
      

  8.   

    难者不会,会者不难.
    使用hook会有一些新概念的,
    对于一般的人,使用API应该简单一些.
    不要误人子弟,代码少并不表示简单,一般的代码多反而容易理解.
    (当然有些人喜欢追求代码的少甚于容易理解,整天叫你看别人的代码,相信你会有些感触的)我的软件中的部分代码,希望你能解决问题.
    (没有写注释,请包含)
    void CControlHwndDlg::OnLButtonDown(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
    CWnd* pWnd = ChildWindowFromPoint(point);
    if (NULL!=pWnd && pWnd->GetSafeHwnd()==m_staticFind.GetSafeHwnd())
    {
    SetCapture();
    SetCursor(m_hcurFind);
    m_staticFind.SetIcon(m_hiconBlank); m_bIsCapturing = TRUE;
    } CDialog::OnLButtonDown(nFlags, point);
    }void CControlHwndDlg::OnLButtonUp(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
    StopCapture(); CDialog::OnLButtonUp(nFlags, point);
    }void CControlHwndDlg::OnMouseMove(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
    if (m_bIsCapturing)
    {
    ClientToScreen(&point);
    CString s;
    s.Format("%4d", point.x);
    SetDlgItemText(IDC_CONTROLHWND_MOUSEX, s);
    s.Format("%4d", point.y);
    SetDlgItemText(IDC_CONTROLHWND_MOUSEY, s); HWND h  = ::WindowFromPoint(point);
    if (IsWindow(h))
    {
    POINT ptChild = point;
    ::ScreenToClient(h, &ptChild);
    HWND hChild  = ::ChildWindowFromPoint(h, ptChild);
    if (NULL != hChild)
    {
    h = hChild;
    } if (::GetWindowThreadProcessId(GetSafeHwnd(), NULL) != ::GetWindowThreadProcessId(h, NULL))
    {
    UpdateWndInformation(h); if (NULL == m_hOldWnd)
    {
    m_hOldWnd = h;
    DrawBorder(h);
    }
    else
    {
    if (h != m_hOldWnd)
    {
    DrawBorder(m_hOldWnd);
    m_hOldWnd = h;
    DrawBorder(h);
    }
    }
    }
    }
    } CDialog::OnMouseMove(nFlags, point);
    }