107上位机VC MFC设备上下文本CDC实现画图程序
107上位机VC MFC设备上下文本CDC实现画图程序 功能展示 窗口的设备上下文本类CDC是我们编程上位机程序,极为常用的类,我们当前例程通过编写简单的绘图程序,学习与熟悉CDC相关的绘图函数,效果如图; 要点提示 任何的窗口,都有设备上下文供我们在窗口上绘制图文,我们当前例程通过客服区上下文实现绘图功能CClientDC dc(this);画点可用函数SetPixel()画线可用MoveTo(), LineTo()画圆椭圆可用Ellipse(),画矩形可用Rectangle(); 通过鼠标的点击,放开,移动,三个消息就可以快速的绘制图形,具体实现我们看例程代码 实现功能 1.新建基于单文档的应用程序 2.在视窗类中添加变量private: BOOL m_bFreeDraw; BOOLm_bDrawPoint; BOOL m_bDrawCircle; BOOL m_bDrawRect; BOOL m_bDrawLine; CPointm_StartPt; CPoint m_EndPt; 用于标识当前绘制何种图形及起点坐标点,终点坐标点; 并在构造函数中初始化 m_bFreeDraw =FALSE; m_bDrawPoint= FALSE; m_bDrawCircle= FALSE; m_bDrawRect= FALSE;m_bDrawLine= FALSE; 3.在菜单中添加自由绘制,点,线,圆,矩形菜单项,用于选择绘制何种图形,并实现响应函数及刷新函数,如我们当前例程 - void CGkbc8View::OnFreedraw()
- {
- m_bFreeDraw = TRUE;
- m_bDrawPoint= FALSE;
- m_bDrawCircle= FALSE;
- m_bDrawRect= FALSE;
- m_bDrawLine= FALSE;
- }
- void CGkbc8View::OnUpdateFreedraw(CCmdUI* pCmdUI)
- {
- pCmdUI->SetCheck(m_bFreeDraw);
- }
复制代码- void CGkbc8View::OnLint()
- {
- m_bFreeDraw = FALSE;
- m_bDrawPoint= FALSE;
- m_bDrawCircle= FALSE;
- m_bDrawRect= FALSE;
- m_bDrawLine= TRUE;
- }
- void CGkbc8View::OnUpdateLint(CCmdUI* pCmdUI)
- {
- pCmdUI->SetCheck(m_bDrawLine);
- }
- void CGkbc8View::OnPoint()
- {
- m_bFreeDraw = FALSE;
- m_bDrawPoint= TRUE;
- m_bDrawCircle= FALSE;
- m_bDrawRect= FALSE;
- m_bDrawLine= FALSE;
- }
- void CGkbc8View::OnUpdatePoint(CCmdUI* pCmdUI)
- {
- pCmdUI->SetCheck(m_bDrawPoint);
- }
- void CGkbc8View::OnRect()
- {
- m_bFreeDraw = FALSE;
- m_bDrawPoint= FALSE;
- m_bDrawCircle= FALSE;
- m_bDrawRect= TRUE;
- m_bDrawLine= FALSE;
- }
复制代码- void CGkbc8View::OnUpdateRect(CCmdUI* pCmdUI)
- {
- pCmdUI->SetCheck(m_bDrawRect);
- }
- void CGkbc8View::OnCircle()
- {
- m_bFreeDraw = FALSE;
- m_bDrawPoint= FALSE;
- m_bDrawCircle= TRUE;
- m_bDrawRect= FALSE;
- m_bDrawLine= FALSE;
- }
- void CGkbc8View::OnUpdateCircle(CCmdUI* pCmdUI)
- {
- pCmdUI->SetCheck(m_bDrawCircle);
- }
- <div style="text-align: center;"><span style="font-size: large; line-height: 1.5;">4.添加鼠标点击,放开,移动消息,实现各种图形的绘制</span></div>
- void CGkbc8View::OnLButtonDown(UINT nFlags, CPoint point)
- {
- if(m_bFreeDraw)//任意绘制
- {
- m_StartPt = point;
- }
- if(m_bDrawPoint)//绘制点
- {
- CClientDC dc(this);
- dc.SetPixel(point,RGB(255,0,0));//不明显,可以加下行代码
- dc.Ellipse(point.x-2,point.y-2,point.x+2,point.y+2);
- }
- if(m_bDrawPoint||m_bDrawRect||m_bDrawCircle)
- {
- m_StartPt = point;
- m_EndPt = point;
- SetCapture();
- }
-
复制代码- if(m_bDrawLine)//绘制线
- {
- m_StartPt = point;
- CClientDC dc(this);
- dc.MoveTo(m_StartPt);
- dc.LineTo(m_EndPt);
- if(m_StartPt != m_EndPt)
- m_EndPt = m_StartPt;
- }
- CView::OnLButtonDown(nFlags, point);
- }
- void CGkbc8View::OnLButtonUp(UINT nFlags, CPoint point)
- {
- ReleaseCapture();
- CView::OnLButtonUp(nFlags, point);
- }
- void CGkbc8View::OnMouseMove(UINT nFlags, CPoint point)
- {
- CClientDC dc(this);
- if(m_bFreeDraw&&(nFlags&&MK_LBUTTON))//自由绘制
- {
- dc.MoveTo(m_StartPt);
- dc.LineTo(point);
- m_StartPt = point;
- }
- if(m_bDrawLine&&(nFlags&&MK_LBUTTON))
- {
- }
- if(m_bDrawRect&&(nFlags&&MK_LBUTTON))//绘制矩形
- {
- CGdiObject*object = dc.SelectStockObject(NULL_BRUSH);
- int mdoe = dc.GetROP2();
- dc.SetROP2(R2_NOTCOPYPEN);
- dc.Rectangle(m_EndPt.x,m_EndPt.y,m_StartPt.x,m_StartPt.y);//擦除旧矩形
- dc.SetROP2(mdoe);
- dc.Rectangle(m_StartPt.x,m_StartPt.y,point.x,point.y);//绘制新矩形
- dc.SelectObject(object);
- m_EndPt = point;
- }
-
复制代码- if(m_bDrawCircle&&(nFlags&&MK_LBUTTON))
- {
- CGdiObject*object = dc.SelectStockObject(NULL_BRUSH);
- int mdoe = dc.GetROP2();
- dc.SetROP2(R2_NOTCOPYPEN);
- dc.Ellipse(m_EndPt.x,m_EndPt.y,m_StartPt.x,m_StartPt.y);
- dc.SetROP2(mdoe);
- dc.Ellipse(m_StartPt.x,m_StartPt.y,point.x,point.y);
- dc.SelectObject(object);
- m_EndPt = point;
- }
- CView::OnMouseMove(nFlags, point);
- }
- <div style="text-align: center;"><span style="font-size: large; line-height: 1.5;">我们来演示一步步实现功能的过程</span></div>
复制代码
|