1。鼠标落下,记录鼠标的起始位置
void CMyEasyDrawView::On<wbr>LButtonDown(UINT nFlags, CPoint point)<br> {<br> // TODO: 在此添加消息处理程序代码和/或调用默认值<br> //graph->m_nType=dlg-><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->m_nType = m_nGraphType;<br>
graph->m_clrColor =dlg->m_clrColor;<br>
graph->m_nLineType = dlg->m_nType;<br>
graph->m_nLineWidth = dlg->m_lineWide;<br>
graph->m_PtTopLeft = m_PtPress;<br>
graph->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->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->m_nType = m_nGraphType;<br>
graph->m_clrColor =dlg->m_clrColor;<br>
graph->m_nLineType = dlg->m_nType;<br>
graph->m_nLineWidth = dlg->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->m_PtTopLeft = m_PtPress;<br>
graph->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->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->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->m_clrColor;<br>
CPen pen(graph->m_nLineType |PS_GEOMETRIC|PS_ENDCAP_ROUND,<br>
graph->m_nLineWidth, &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->SelectObject(&pen);<br><br>
// 使用透明画刷<br>
CBrush* oldBrush = (CBrush*)pDC->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->m_nType)<br>
{<br>
case EG_RECT:<br>
// 绘制矩形<br>
pDC->Rectangle(graph->m_PtTopLeft.x,<br>
graph->m_PtTopLeft.y,<br>
graph->m_PtBottomRight.x,<br>
graph->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->Ellipse(graph->m_PtTopLeft.x,<br>
graph->m_PtTopLeft.y,<br>
graph->m_PtBottomRight.x,<br>
graph->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->MoveTo(graph->m_PtTopLeft);<br>
pDC->LineTo(graph->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->SelectObject(oldPen);<br>
pDC->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<m_GraphAry.GetCount();i++)<br>
{<br>
graph=(EasyGraphics*)m_GraphAry[i];<br>
DrawGraphics(&memDC,graph);<br>
}<br>
pDC->BitBlt(0,0,rect.Width(),rect.Height(),&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>