对话框中有CListCtrl控件时,在CListCtrl控件上移动鼠标,对话框收不到鼠标移动消息怎么办?

解决方案 »

  1.   

    继承 CListCtrl 生成 CMyLiStCtrl 类
    在 CMyLiStCtrl 重载虚函数 PreTranslateMessage
    BOOL CMyLiStCtrl::PreTranslateMessage(MSG* pMsg) 
    {
      if(pMsg->message == WM_MOUSEMOVE)//鼠标移动消息
      {
        CWnd *pWnd = GetParent(); //获取父窗口指针
        if(pWnd && pWnd->GetSafeHwnd()) //父窗口指针有效
        {
          //计算此控件与父窗口的位置偏差
          CRect this_rect;
          GetWindowRect(&this_rect);
          CRect parent_rect;
          pWnd->GetWindowRect(&parent_rect);
          CPoint offset(this_rect.left - parent_rect.left,this_rect.top - parent_rect.top);
      
          //当前窗口中的鼠标位置
          CPoint point(LOWORD(pMsg->lParam),HIWORD(pMsg->lParam));      //换算到父窗口位置
          CPoint parentPoint = point + offset;      //传递消息给父窗口
          ::PostMessage(pWnd->m_hWnd,WM_MOUSEMOVE,pMsg->wParam,MAKELONG(parentPoint.x,parentPoint.y));
        }    
      } return CListCtrl::PreTranslateMessage(pMsg);
    }在对话框头文件中 #include "MyLiStCtrl.h"
    把 CListCtrl 修改为 CMyLiStCtrl 即可
      

  2.   

    其实也不用扩展类,直接在dlg中截获消息
    BOOL CMouseMoveOnLstCtlDlg::PreTranslateMessage(MSG* pMsg) 
    {
    // TODO: Add your specialized code here and/or call the base class
    if (pMsg->message == WM_MOUSEMOVE && GetFocus()->GetDlgCtrlID() == IDC_LIST1)
    {
    AfxMessageBox("MOVE ON LISTCTRL");
    }
    return CDialog::PreTranslateMessage(pMsg);
    }但有个缺点是只有在listctrl获得焦点时才会弹出消息框
    不过可以将GetFocus()->GetDlgCtrlID() == IDC_LIST1条件变为判断当前坐标点是否在clistctrl的矩形区域内 再&& pMsg->message == WM_MOUSEMOVE 应该可以
      

  3.   

    其实也不用扩展类,直接在dlg中截获消息
    BOOL CMouseMoveOnLstCtlDlg::PreTranslateMessage(MSG* pMsg) 
    {
    // TODO: Add your specialized code here and/or call the base class
    if (pMsg->message == WM_MOUSEMOVE && GetFocus()->GetDlgCtrlID() == IDC_LIST1)
    {
    AfxMessageBox("MOVE ON LISTCTRL");
    }
    return CDialog::PreTranslateMessage(pMsg);
    }但有个缺点是只有在listctrl获得焦点时才会弹出消息框
    不过可以将GetFocus()->GetDlgCtrlID() == IDC_LIST1条件变为判断当前坐标点是否在clistctrl的矩形区域内 再&& pMsg->message == WM_MOUSEMOVE 应该可以
      

  4.   

    其实也不用扩展类,直接在dlg中截获消息
    BOOL CMouseMoveOnLstCtlDlg::PreTranslateMessage(MSG* pMsg) 
    {
    // TODO: Add your specialized code here and/or call the base class
    if (pMsg->message == WM_MOUSEMOVE && GetFocus()->GetDlgCtrlID() == IDC_LIST1)
    {
    AfxMessageBox("MOVE ON LISTCTRL");
    }
    return CDialog::PreTranslateMessage(pMsg);
    }但有个缺点是只有在listctrl获得焦点时才会弹出消息框
    不过可以将GetFocus()->GetDlgCtrlID() == IDC_LIST1条件变为判断当前坐标点是否在clistctrl的矩形区域内 再&& pMsg->message == WM_MOUSEMOVE 应该可以
      

  5.   

    二楼的做法挺好的,又学习了,我想要的是:一个对话上只有一个 CListCtrl,当对话框启动的时候,设置定时器4秒后关闭对话框,如果鼠标移动到对话框上了就取消定时器,问题是鼠标移动消息不能获取。
      

  6.   

    这个可以不用管对话框中的控件 OnInitDialog 中加入
    SetTimer(1,4000,NULL); //启动4秒钟定时器加入 WM_TIMER 定时器消息映射
    OnTimer( UINT nIDEvent )
    {
      if(nIDEvent == 1)//为4秒钟关闭对话框定时器
      {
        KillTimer(1); //关闭定时器
        EndDialog(IDCANCEL); 结束对话框
        return;    
      }
    }重载 PreTranslateMessage
    PreTranslateMessage(MSG* pMsg) 
    {
      // TODO: Add your specialized code here and/or call the base class
      //
      if(pMsg->message == WM_MOUSEFIRST) //鼠标开始移动
      {
        KillTimer(1); //关闭定时器
      }
    }//在规定的时间中有鼠标在对话框上移动就关闭定时器