程序显示为何不对???
想做一个程序.让光标限制沿着y=x 的直线.并且画个十字线.
程序如下: 为何十字线显示不对???请指点.
void CtestmoveView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CRect client;
GetClientRect(&client);
ClientToScreen(&client);
::ClipCursor(&client);//锁定鼠标移动范围在客户区
SetCursorPos(client.left+point.x,client.top+point.x);
point.y=point.x;
pointLast=point;
Invalidate(1);
CView::OnMouseMove(nFlags, point);
}
void CtestmoveView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
pointLast=point;
CView::OnLButtonDown(nFlags, point);
}
void CtestmoveView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CView::OnLButtonUp(nFlags, point);
}
void CtestmoveView::OnPaint()
{
CPaintDC dc(this); // device context for painting
CPoint point;
GetCursorPos(&point);
ScreenToClient(&point);
CRect m_RectCoord;
GetClientRect(&m_RectCoord);
CPen pentmp(PS_SOLID, 1, RGB(0, 0, 0));
CPaintDC *pdc=&dc;
CPen *pOldPentmp=pdc-> SelectObject(&pentmp);
pdc-> MoveTo(point.x, m_RectCoord.top);
pdc-> LineTo(point.x, m_RectCoord.bottom);
pdc-> MoveTo(m_RectCoord.left, point.y);
pdc-> LineTo(m_RectCoord.right, point.y);
pdc-> SelectObject(pOldPentmp);
}
[解决办法]
你在 OnMouseMove 中调用
SetCursorPos(client.left+point.x,client.top+point.x);
该调用导致触发WM_PAINT消息 再次调用 OnMouseMove, 程序进入了一个循环
在该函数的开头加上这么一段就可以了
void CtestMFCView::OnMouseMove(UINT nFlags, CPoint point)
{
static bool a=false;
if(a){a=false; return;}
a=true;