建好MFC工程
鼠标输入缓存
CSanJiaoFenGeView类 宏定义 #define N 64
CSanJiaoFenGeView类添加公共成员变量
CClientDC *m_pDC;
int m_pNumbers; // polygon input buffer
CPoint m_pAccord[N], m_mousePoint;
CSanJiaoFenGeView类构造函数初始化m_pNumbers。
CSanJiaoFenGeView::CSanJiaoFenGeView()
{
m_pNumbers = 0;
}
鼠标交互控制
重写CCgQBFillPolyView类的鼠标消息处理函数。
弹出类向导对话窗口,添加WM_LBUTTONDOWN、WM_LBUTTONDBLCLK、WM_MOUSEMOVE 和WM_CREATE 四个
消息处理函数
在这里插入图片描述
View类模板的四个消息处理函数中添加如下代码。
void CSanJiaoFenGeView::OnLButtonDown(UINT nFlags, CPoint point)
{
if (m_pNumbers < N) {
m_pAccord[m_pNumbers] = point;
m_pNumbers++;
m_mousePoint = point;
}
CView::OnLButtonDown(nFlags, point);
}
void CSanJiaoFenGeView::OnMouseMove(UINT nFlags, CPoint point)
{
if ( m_pNumbers) {
m_pDC->SetROP2(2);
m_pDC->MoveTo(m_pAccord[m_pNumbers - 1]);
m_pDC->LineTo(m_mousePoint);
m_mousePoint = point;
m_pDC->MoveTo(m_pAccord[m_pNumbers - 1]);
m_pDC->LineTo(m_mousePoint);
}
CView::OnMouseMove(nFlags, point);
}
int CSanJiaoFenGeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
m_pDC = new CClientDC(this);
return 0;
}
void CSanJiaoFenGeView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
m_pDC->MoveTo(m_pAccord[m_pNumbers - 1]);
m_pDC->LineTo(m_pAccord[0]);
m_pAccord[m_pNumbers] = m_pAccord[0];
m_pNumbers++;
m_pNumbers = 0;
CView::OnLButtonDblClk(nFlags, point);
}