新建單文檔MFC工程,並在視圖類中定義以下三個變量
private:
BOOL m_startRect;
CPoint m_startPoint;
CPoint m_OldPoint;
在構造函數中進行初始化變量
Calg_projectView::Calg_projectView()
{
// TODO: 在此處添加構造代碼
m_startRect = FALSE;
m_startPoint = 0;
m_OldPoint = 0;
}
void Calg_projectView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息處理程序代碼和/或調用默認值
Cor.left = point.x;
Cor.top = point.y;
m_startRect = TRUE;
m_startPoint = point;
m_OldPoint = point;
CView::OnLButtonDown(nFlags, point);
}
void Calg_projectView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息處理程序代碼和/或調用默認值
CClientDC dc(this);
dc.SetROP2(R2_NOT);
dc.SelectStockObject(NULL_BRUSH);
if(m_startRect)
{
dc.Rectangle(CRect(m_startPoint,m_OldPoint));
dc.Rectangle(CRect(m_startPoint,point));
m_OldPoint = point;
}
CView::OnMouseMove(nFlags, point);
}
void Calg_projectView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息處理程序代碼和/或調用默認值
Cor.right = point.x;
Cor.bottom = point.y;
m_startRect = FALSE;
CClientDC dc(this);
dc.SetROP2(R2_NOT);
dc.SelectStockObject(NULL_BRUSH);
dc.Rectangle(CRect(m_startPoint,m_OldPoint));
CView::OnLButtonUp(nFlags, point);
}