天天看点

在状态栏中显示鼠标位置坐标

(1)在状态栏资源中添加一个窗格。实现的方法是打开MainFrm.cpp文件,找到静态数

组indicators的定义,在第一个数组元素ID_SEPARATOR后面增加一个新的数组元素,即

添加了一个新的窗格,为了表明这个窗格的用途,故命名为

ID_INDICATOR_MOUSE_POS。修改后的代码如下:

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_MOUSE_POS,       //鼠标位置坐标值窗格
	ID_INDICATOR_CAPS,			 // 大写 
	ID_INDICATOR_NUM,			// 数字键 
	ID_INDICATOR_SCRL,		   // 滚动 
};
           

(2) 在ResourceView的String Table中添加ID_INDICATOR_MOUSE_POS, 在Caption

中输入“鼠标的当前坐标”。

在状态栏中显示鼠标位置坐标

(3)添加鼠标移动消息的响应函数。

编辑其代码如下:

void CMyTextOutView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
		CString szCoordinate;
		//获得状态栏的指针
		CStatusBar* pStatusBar=(CStatusBar*)GetParentFrame()->
        GetDescendantWindow(ID_VIEW_STATUS_BAR);
		szCoordinate.Format("(%4d,%4d)",point.x,point.y);
		//在状态栏的第二个窗格中输出当前鼠标位置
		pStatusBar->SetPaneText(1,szCoordinate);   //面板编号从0开始
		CView::OnMouseMove(nFlags, point);
}
           

(4)运行效果如下:

在状态栏中显示鼠标位置坐标