实验八 MFC 鼠标做图编程实验

  • 格式:docx
  • 大小:150.51 KB
  • 文档页数:7

实验八 MFC 鼠标做图编程实验
一、实验目的
(1) 熟悉Visual C++ 6.0 开发环境;
(2) 掌握应用MFC 类库编写鼠标绘图程序的方法;
(3) 掌握MFC 环境中绘图函数的使用方法。

二、实验内容
创建一个单文档应用程序,实现鼠标的绘图功能。

要求:
(1) 创建一个工具栏,有线段、矩形、椭圆三个按钮;
(2) 绘图前,选择工具栏上的按钮,确定图形的形状。

按下鼠标左键,开始绘图,结合鼠标的光标坐标值,来确定图形的形状和大小,并随着鼠标的移动在屏幕上实时绘制图形,放开鼠标左键,确定最后的图形,绘制在屏幕上。

三、程序代码
在work8View.h中声明:
class CWork8View : public CView
{protected:
CWork8View();
DECLARE_DYNCREATE(CWork8View)
// Attributes
public:
CWork8Doc* GetDocument();
DWORD m_i;
int m_X0;
int m_X1;
int m_Y0;
int m_Y1;
bool m_down0;
bool m_down1;
bool m_down2;
boolm_show;
// Operations
在work8View.h中的相关代码:
CWork8View::CWork8View()
{// TODO: add construction code here
m_down0=FALSE;
m_down1=FALSE;
m_down2=FALSE;
m_show=TRUE;
}
CWork8View::~CWork8View()
{}
BOOL CWork8View::PreCreateWindow(CREATESTRUCT&cs)
{// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
returnCView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CWork8View drawing
void CWork8View::OnDraw(CDC* pDC)
{
CWork8Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
pDC->SetTextColor(RGB(0,0,255));
pDC->TextOut(10,10,"点击相应按钮画对应的图形,点击“清除”清空窗口"); }
void CWork8View::OnOperEllipse() //
{// TODO: Add your command handler code here m_i=1;
m_down1=TRUE;
m_down0=FALSE;
m_down2=FALSE;
m_show=TRUE;
Invalidate();
}
void CWork8View::OnOperLine()
{
// TODO: Add your command handler code here m_i=0;
m_down0=TRUE;
m_down1=FALSE;
m_down2=FALSE;
m_show=TRUE;
Invalidate();
}
void CWork8View::OnOperRectangle()
{
// TODO: Add your command handler code here m_i=2;
m_down2=TRUE;
m_down0=FALSE;
m_down1=FALSE;
m_show=TRUE;
Invalidate();
}
void CWork8View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default m_X0=m_X1=point.x;
m_Y0=m_Y1=point.y;
CView::OnLButtonDown(nFlags, point);
}
void CWork8View::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default CView::OnMouseMove(nFlags, point);
CClientDCdc(this);
dc.SelectStockObject(WHITE_PEN);
dc.SelectStockObject(WHITE_BRUSH);
if((nFlags& MK_LBUTTON)&&m_show)
{ if(m_i==1)
dc.Ellipse(m_X0,m_Y0,m_X1,m_Y1);
else if(m_i==0)
{dc.MoveTo(m_X0,m_Y0);
dc.LineTo(m_X1,m_Y1);}
else if(m_i==2)
dc.Rectangle(m_X0,m_Y0,m_X1,m_Y1);}
m_X1 = point.x;
m_Y1 = point.y;
dc.SelectStockObject(BLACK_PEN);
dc.SelectStockObject(GRAY_BRUSH);
if((nFlags& MK_LBUTTON )&&m_show)
{
if(m_i==1)
dc.Ellipse(m_X0,m_Y0,m_X1,m_Y1);
else if(m_i==0)
{dc.MoveTo(m_X0,m_Y0);
dc.LineTo(m_X1,m_Y1);}
else if(m_i==2)
dc.Rectangle(m_X0,m_Y0,m_X1,m_Y1);
}
}
void CWork8View::OnUpdateOperEllipse(CCmdUI* pCmdUI) {
// TODO: Add your command update UI handler code here pCmdUI->SetCheck(m_down1);
}
void CWork8View::OnUpdateOperLine(CCmdUI* pCmdUI) {
// TODO: Add your command update UI handler code here pCmdUI->SetCheck(m_down0);
}
void CWork8View::OnUpdateOperRectangle(CCmdUI* pCmdUI) {
// TODO: Add your command update UI handler code here pCmdUI->SetCheck(m_down2);
}
void CWork8View::OnOperShow()
{
// TODO: Add your command handler code here
m_show=!m_show;
m_down0=FALSE;
m_down1=FALSE;
m_down2=FALSE;
Invalidate();
}
四、运行结果
五、实验总结。