void CEditorView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CString str;
CMainFrame *pFrame=(CMainFrame *)AfxGetApp( )->m_pMainWnd;
CStatusBar *pStatus=&pFrame->m_wndStatusBar; if(pStatus)
{
str.Format("x= %d",point.x);
pStatus->SetPaneText(1,str);
str.Format("y= %d",point.y);
pStatus->SetPaneText(2,str);
}
CEditView::OnMouseMove(nFlags, point);
}
可以实现制定鼠标x,y的坐标,但是如果要实现显示文本当前行和列怎么做
{
// TODO: Add your message handler code here and/or call default
CString str;
CMainFrame *pFrame=(CMainFrame *)AfxGetApp( )->m_pMainWnd;
CStatusBar *pStatus=&pFrame->m_wndStatusBar; if(pStatus)
{
str.Format("x= %d",point.x);
pStatus->SetPaneText(1,str);
str.Format("y= %d",point.y);
pStatus->SetPaneText(2,str);
}
CEditView::OnMouseMove(nFlags, point);
}
可以实现制定鼠标x,y的坐标,但是如果要实现显示文本当前行和列怎么做
先用getsystemmatric()得到每行的高度.再根据鼠标坐标就可以知道是第几行了.
用gettextextent()等到字符宽度,再根据鼠标坐标得到离坐标最近字符的位置就可以了.可以参考mfc windows程序设计中的Visual KB例程.
{
// TODO: Add your message handler code here and/or call default
CString str;
TEXTMETRIC tm;
CDC* dc;
dc = GetDC();
dc->GetTextMetrics(&tm);
ReleaseDC(dc);
long height,width;
width = tm.tmAveCharWidth;
height = tm.tmHeight;
CMainFrame *pFrame=(CMainFrame *)
AfxGetApp( )->m_pMainWnd;
CStatusBar *pStatus=&pFrame->m_wndStatusBar;
if(pStatus)
{
str.Format("x= %d",point.x/height+1);
pStatus->SetPaneText(1,str);
str.Format("y= %d",point.y/width+1);
pStatus->SetPaneText(2,str);
}
CEditView::OnMouseMove(nFlags, point);
}
请问下我这样做对不对,我没有用到GetTextExtent()函数
我这个是可以运行,但是确实数字是不对的所以正在改正当中void CEditorView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CString str;
int lHeight;
int cWidth;
CDC *pDC=GetDC(); //取得当前窗口的设备场景指针并存放在PDC中
TEXTMETRIC tm;
pDC->GetTextMetrics(&tm);
lHeight=tm.tmHeight+tm.tmExternalLeading;
cWidth=tm.tmAveCharWidth;
CMainFrame *pFrame=(CMainFrame *)
AfxGetApp( )->m_pMainWnd;
CStatusBar *pStatus=&pFrame->m_wndStatusBar;
if(pStatus)
{
str.Format("x= %d",point.x/lHeight+1);
pStatus->SetPaneText(1,str);
str.Format("y= %d",point.y/cWidth+1);
pStatus->SetPaneText(2,str);
}
CEditView::OnMouseMove(nFlags, point);
}这个为什么也不行,我已经取了当前场景但是貌似运行出来的坐标还是不是行和列呢郁闷中。。
这个要编写比较多的代码,还是看看书中的例子吧