关于CMainFrame的鼠标响应

来源:互联网 发布:淘宝虚拟交易 编辑:程序博客网 时间:2024/04/29 17:30

       今天碰到一个在工具栏处添加坐标显示鼠标当前位置的小问题。

       前辈留下的代码的处理方式,是把Cmainframe中的工具栏权限直接改成了public,而后在CView中的Mousemove添加编辑。很直接的方法,我却觉得更改MFC代码的方式不大合适。

       在网上查找了一些资料。发现如下:

        1.Cmainframe确实是完全可以相应鼠标事件的。例如在View下重写showwindow,

用ShowWindow(SW_MINIMIZE)将view最小化,此时Cmainframe可以捕捉并响应鼠标的各种消息。

        2.见到一些人以空白的工具栏处不能响应Cmainframe的鼠标消息为由,而断定Cmainframe不能响应鼠标事件。看来关于工具栏处对于鼠标事件的处理,还需要研究。

        最终我的解决方式是在CView下重载了Cmainframe中的mousemove消息,并用sendmessage发送给Cmainframe。

       代码如下:

 

void CMainFrame::OnMouseMove(UINT nFlags, CPoint point)
{
          // TODO: 在此添加消息处理程序代码和/或调用默认值
          CString str;
          CMainFrame *pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;
          CStatusBar *pStatus=&pFrame->m_wndStatusBar;
          if(pStatus)
         {
                str.Format(_T("%d,%d"),point.x,point.y);
                pStatus->SetPaneText(2,str);
          }

          CFrameWnd::OnMouseMove(nFlags, point);
}

 

 

void CTestView::OnMouseMove(UINT nFlags, CPoint point)
{
 // TODO: 在此添加消息处理程序代码和/或调用默认值
 CWnd *p = GetParent();
 p->SendMessage(WM_MOUSEMOVE,0,MAKELPARAM(point.x,point.y) );
}

 

 

原创粉丝点击