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

视图:

在类视图找到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);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值