怎么在在视图中显示鼠标位置 mfc

来源:互联网 发布:钢筋工程量计算软件 编辑:程序博客网 时间:2024/04/30 20:28

视图:

在类视图找到CCvsdView,右击添加CPoint变量m_ptc,

添加鼠标移动消息,添加如下代码:

void CCvsdView::OnMouseMove(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultCRect rect ; CPoint ptPrev = m_ptc ; m_ptc = point ; rect.SetRect(m_ptc.x - 100, m_ptc.y - 100, m_ptc.x + 100, m_ptc.y + 100) ; InvalidateRect(&rect) ; rect.SetRect(ptPrev.x - 100, ptPrev.y - 100, ptPrev.x + 100, ptPrev.y + 100 ) ; InvalidateRect(&rect) ; CView::OnMouseMove(nFlags, point);}

重写OnDraw()函数:

void CCvsdView::OnDraw(CDC* pDC){CCvsdDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);if (!pDoc) return;// TODO: add draw code for native data herCString str,str1,str2 ; str1="x=",str2=",y=";str.Format( "%d ", m_ptc.x) ;str1+=str;str.Format( "%d ", m_ptc.y) ;str2+=str;str=str1+str2;pDC-> TextOut(m_ptc.x+10, m_ptc.y-8, str) ; }


状态栏:

 在字符串表中加入ID_INDICATOR_POINT项;

在MainFrm.cpp文件中找到indicators[]数组,添加ID_INDICATOR_POINT项;

添加鼠标移动消息,添加如下代码:

void CCvsdView::OnMouseMove(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultCClientDC   dc(this);  CMainFrame *pFrame=(CMainFrame *)AfxGetApp()->m_pMainWnd;  CStatusBar *pStatusBar=(CStatusBar *)&pFrame->m_wndStatusBar;  // 获取状态栏指针CString   str;  str.Format("X:%d,Y:%d",point.x,point.y);  CSize size=dc.GetTextExtent(str);  int nIndex=pStatusBar->CommandToIndex(ID_INDICATOR_POINT); //获取状态栏位置,即:ID_INDICATOR_POINT 在某数组(indicators[])声明时的位置pStatusBar->SetPaneInfo(nIndex,ID_INDICATOR_POINT,SBPS_NORMAL,size.cx);//状态栏随着输出内容大小而变化  pStatusBar->SetPaneText(nIndex,str); // 将鼠标的指针显示在状态栏中CView::OnMouseMove(nFlags, point);}


原创粉丝点击