答:根据产生鼠标消息时鼠标光标所处的位置,鼠标消息分为:客户区鼠标消息和非客户区鼠标消息两类。
在应用程序窗口中,用户可以绘图的部分叫做客户区或者用户区,而除此之外的区域叫非客户区。
鼠标在客户区产生的消息叫客户区鼠标消息,在非客户区产生的消息叫非客户区鼠标消息。
答:首先,在主框架窗口类的声明中手工添加非客户消息响应函数的声明,然后在主框架窗口类实现文件的消息映射表中添加消息映射,最后在主框架窗口类的实现文件中,添加鼠标响应函数并实现它。
(1)
//添加数据成员
class CM7_9Doc : public CDocument
{
// Attributes
public:
CPoint p1,p2;
int flag;
};
void CM7_9View::OnDraw(CDC* pDC)
{
CM7_9Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CClientDC dc(this);
p1.x = 300;
p1.y = 300;
if(flag == 0){
pDC->Ellipse(pDoc->p2.x+50,pDoc->p2.y+50,pDoc->p2.x-50,pDoc->p2.y-50);
}
else{
pDC->Ellipse(p1.x+50,p1.y+50,p1.x-50,p1.y-50);
}
}
//单击鼠标左键 圆放大
void CM7_9View::OnLButtonDown(UINT nFlags, CPoint point)
{
CClientDC dc(this);
p1.x = 350;
p1.y = 350;
dc.Ellipse(p1.x+100,p1.y+100,p1.x-100,p1.y-100);
CView::OnLButtonDown(nFlags, point);
}
//单击鼠标右键 圆变小
void CM7_9View::OnRButtonDown(UINT nFlags, CPoint point)
{
InvalidateRect(NULL,TRUE);
CClientDC dc(this);
p1.x = 350;
p1.y = 350;
dc.Ellipse(p1.x+50,p1.y+50,p1.x-50,p1.y-50);
CView::OnRButtonDown(nFlags, point);
}
void CM7_9View::OnMouseMove(UINT nFlags, CPoint point)
{
CM7_9Doc* pDoc = GetDocument();
//按下crtl 同时移动鼠标 圆跟着鼠标移动
if(nFlags&MK_CONTROL){
flag = 0;
pDoc->p2 = point;
InvalidateRect(NULL,true);
}
CView::OnMouseMove(nFlags, point);
}
class CM7_12Doc : public CDocument
{
public:
CRect rect;
};
CM7_12Doc::CM7_12Doc()
{
rect.left = 200;
rect.right = 400;
rect.top = 200;
rect.bottom =300;
}
void CM7_12View::OnDraw(CDC* pDC)
{
CM7_12Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CClientDC dc(this);
dc.Rectangle(pDoc->rect);
// TODO: add draw code for native data here
}
void CM7_12View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CM7_12Doc* pDoc = GetDocument();
CRect client;
GetClientRect(&client);
switch(nChar){
case VK_LEFT:
pDoc->rect.left -=10;
pDoc->rect.right -=10;
break;
case VK_RIGHT:
pDoc->rect.left +=10;
pDoc->rect.right +=10;
break;
case VK_UP:
pDoc->rect.top -=10;
pDoc->rect.bottom -=10;
break;
case VK_DOWN:
pDoc->rect.top +=10;
pDoc->rect.bottom +=10;
break;
case VK_HOME:
pDoc->rect.left -=10;
pDoc->rect.top -=10;
break;
case VK_END:
pDoc->rect.bottom +=10;
pDoc->rect.right +=10;
break;
}
InvalidateRect(NULL,true);
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CM7_12View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CM7_12Doc *pDoc = GetDocument();
pDoc->rect.left = 200;
pDoc->rect.right = 400;
pDoc->rect.top = 200;
pDoc->rect.bottom = 300;
InvalidateRect(NULL,true);
CView::OnLButtonDown(nFlags, point);
}