0
我想创建一个MFC应用程序,通过按住鼠标左键并移动到新位置来移动客户区域中的矩形。但是,尽管我慢慢移动鼠标,矩形移动得太快。 我的代码在这里:MFC:在客户区域移动一个矩形
class CEmpWnd : public CFrameWnd
{
protected:
POINT lpStart, rpStart, oPoint;
bool clicked;
public:
CEmpWnd(void);
virtual ~CEmpWnd(void);
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnPaint();
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP()
};
int CEmpWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
lpStart.x = 0;
lpStart.y = 0;
rpStart.x = 100;
rpStart.y = 100;
clicked = false;
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
SetTimer(ID_TIMER, 250, NULL);
SetWindowText(L"Phùng Khánh Hiên ");
return 0;
}
void CEmpWnd::OnPaint()
{
CPaintDC dc(this); // device context for painting
Rectangle(dc, lpStart.x, lpStart.y, rpStart.x, rpStart.y); //Draw a rectangle
}
void CEmpWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
if(point.x >= lpStart.x && point.x <= rpStart.x && point.y >= lpStart.y && point.y <= rpStart.y) //If click on rectangle area
{
clicked = true;
oPoint = point;
}
}
void CEmpWnd::OnMouseMove(UINT nFlags, CPoint point)
{
if(clicked == true)
{
lpStart.x += (point.x - oPoint.x); //Increase left-top and right-bot Points
rpStart.x += (point.x - oPoint.x);
lpStart.y += (point.y - oPoint.y);
rpStart.y += (point.y - oPoint.y);
lpStart.x = max(0, lpStart.x); //Keep current size of rectangle.
lpStart.y = max(0, lpStart.y);
rpStart.x = max(100, rpStart.x);
rpStart.y = max(100, rpStart.y);
Invalidate(); //Eraze old Rectangle and redraw new one.
}
}
void CEmpWnd::OnLButtonUp(UINT nFlags, CPoint point)
{
clicked = false; //Release drawing.
}
我是新的MFC。有人能帮助我吗 ?谢谢大家。