VC画图用到的主要方法

1。鼠标落下,记录鼠标的起始位置

void CMyEasyDrawView::On<wbr>LButtonDown(UINT nFlags, CPoint point)<br> {<br> // TODO: 在此添加消息处理程序代码和/或调用默认值<br> //graph-&gt;m_nType=dlg-&gt;<br> m_bStartDraw = true;<br> m_PtPress = m_PtLast = point;</wbr>

<wbr></wbr>

CView::On<wbr>LButtonDown(nFlags, point);<br> }</wbr>

<wbr><p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> 2.鼠标抬起,画图结束,记录下画图的所有参数</p> <p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> oid CMyEasyDrawView::On<wbr>LButtonUp(UINT nFlags, CPoint point)<br> {</wbr></p> <wbr><p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> DrawDialog *dlg=CMainFrame::GetDrawTool();<br> if (m_bStartDraw)<br> {<br> // 创建新图形对象<br> EasyGraphics* graph = new EasyGraphics;</p> <p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> // 对图形属性赋值<br> graph-&gt;m_nType = m_nGraphType;<br> graph-&gt;m_clrColor =dlg-&gt;m_clrColor;<br> graph-&gt;m_nLineType = dlg-&gt;m_nType;<br> graph-&gt;m_nLineWidth = dlg-&gt;m_lineWide;<br> graph-&gt;m_PtTopLeft = m_PtPress;<br> graph-&gt;m_PtBottomRight = point;</p> <p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> // 添加到图形容器中<br> m_GraphAry.Add(graph);</p> <p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> // 一个绘制结束<br> m_bStartDraw = false;</p> <p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> // 让视图重绘<br> Invalidate();<br> }</p> <p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> CView::On<wbr>LButtonUp(nFlags, point);<br> }</wbr></p> <wbr><p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> 3.鼠标移动产生的动感</p> <p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> void CMyEasyDrawView::On<wbr>MouseMove(UINT nFlags, CPoint point)<br> {<br> if (m_bStartDraw)<br> {<br> CDC* pDC = GetDC();<br> int nDrawMode = pDC-&gt;SetROP2(R2_NOTXORPEN);// 设置为“异或模式”<br> DrawDialog* dlg = CMainFrame::GetDrawTool();<br> EasyGraphics* graph = new EasyGraphics;// 临时绘制对象</wbr></p> <wbr><p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> // 对图形属性赋值;<br> graph-&gt;m_nType = m_nGraphType;<br> graph-&gt;m_clrColor =dlg-&gt;m_clrColor;<br> graph-&gt;m_nLineType = dlg-&gt;m_nType;<br> graph-&gt;m_nLineWidth = dlg-&gt;m_lineWide;</p> <p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> graph-&gt;m_PtTopLeft = m_PtPress;<br> graph-&gt;m_PtBottomRight = m_PtLast;<br> DrawGraphics(pDC, graph);// 擦除上次绘制图形</p> <p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> graph-&gt;m_PtBottomRight = point;<br> DrawGraphics(pDC, graph);// 绘制新图形</p> <p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> m_PtLast = point;// 保存当前鼠标坐标<br> pDC-&gt;SetROP2(nDrawMode);<br> if(graph!=NULL)<br> {<br> delete graph;<br> graph=NULL;<br> }<br> ReleaseDC(pDC);<br> }</p> <p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> CView::On<wbr>MouseMove(nFlags, point);<br> }</wbr></p> <wbr><p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> 4.画图操作函数</p> <p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> void CMyEasyDrawView::DrawGraphics(CDC* pDC, EasyGraphics* graph)<br> {<br> LOGBRUSH logBrush;<br> logBrush.lbStyle = BS_SOLID;<br> logBrush.lbColor = graph-&gt;m_clrColor;<br> CPen pen(graph-&gt;m_nLineType |PS_GEOMETRIC|PS_ENDCAP_ROUND,<br> graph-&gt;m_nLineWidth, &amp;logBrush);</p> <p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> // 选中新的笔对象<br> CPen*oldPen = pDC-&gt;SelectObject(&amp;pen);<br><br> // 使用透明画刷<br> CBrush* oldBrush = (CBrush*)pDC-&gt;SelectStockObject(NULL_BRUSH);</p> <p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> switch (graph-&gt;m_nType)<br> {<br> case EG_RECT:<br> // 绘制矩形<br> pDC-&gt;Rectangle(graph-&gt;m_PtTopLeft.x,<br> graph-&gt;m_PtTopLeft.y,<br> graph-&gt;m_PtBottomRight.x,<br> graph-&gt;m_PtBottomRight.y);<br> break;</p> <p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> case EG_CIRCLE:<br> // 绘制椭圆<br> pDC-&gt;Ellipse(graph-&gt;m_PtTopLeft.x,<br> graph-&gt;m_PtTopLeft.y,<br> graph-&gt;m_PtBottomRight.x,<br> graph-&gt;m_PtBottomRight.y);<br> break;</p> <p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> default:<br> // 绘制直线<br> pDC-&gt;MoveTo(graph-&gt;m_PtTopLeft);<br> pDC-&gt;LineTo(graph-&gt;m_PtBottomRight);<br> }</p> <p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> pDC-&gt;SelectObject(oldPen);<br> pDC-&gt;SelectObject(oldBrush);<br> }</p> <p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> 5.初始设置</p> <p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> void CMyEasyDrawView::On<wbr>Draw(CDC* pDC/*pDC*/)<br> {<br> CMyEasyDrawDoc* pDoc = GetDocument();<br> ASSERT_VALID(pDoc);<br> if (!pDoc)<br> return;</wbr></p> <wbr><p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> // TODO: 在此处为本机数据添加绘制代码<br> CRect rect;<br> GetClientRect(rect);</p> <p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> CDC memDC;<br> CBitmap MemBitmap;<br> //设备描述表初始话<br> memDC.CreateCompatibleDC(NULL);<br> //建立与屏幕显示兼容的内存显示设备<br> MemBitmap.CreateCompatibleBitmap(pDC,rect.Width(),rect.Height());<br> //选取空白位图<br> memDC.SelectObject(MemBitmap);<br> memDC.FillSolidRect(100,0,rect.Width(),rect.Height(),RGB(255,0,0));<br> EasyGraphics* graph=NULL;<br> for(INT_PTR i=0;i&lt;m_GraphAry.GetCount();i++)<br> {<br> graph=(EasyGraphics*)m_GraphAry[i];<br> DrawGraphics(&amp;memDC,graph);<br> }<br> pDC-&gt;BitBlt(0,0,rect.Width(),rect.Height(),&amp;memDC,0,0,SRCCOPY);</p> <p style="line-height:25px; margin:0px 0px 10px; padding:0px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> }</p> </wbr></wbr></wbr></wbr></wbr></wbr>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值