视图:
在类视图找到CCvsdView,右击添加CPoint变量m_ptc,
添加鼠标移动消息,添加如下代码:
void CCvsdView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CRect 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 her
CString 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 default
CClientDC 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);
}