VC++之图形设备接口之画笔光栅操作

VC++之图形设备接口之画笔光栅操作

一、创建单文档应用程序


二、编辑菜单资源


三、添加变量、函数

        3.1、添加变量

 BOOL m_drawLines;
 BOOL m_blue;                  //背景蓝色
 BOOL m_green;                //背景绿色


        3.2、添加函数


四、于LineView.cpp文件添加代码

CLineView::CLineView()
{
 // TODO: add construction code here
 m_green=FALSE;
 m_blue=FALSE;
}

void CLineView::OnDraw(CDC* pDC)
{
 CLineDoc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 // TODO: add draw code for native data here
 if(!m_green*!m_blue)
 pDC->FillRect(CRect(0,0, 750, 400), new CBrush(RGB(176,150,33)));

 if(m_green)
 pDC->FillRect(CRect(0,0, 750, 400), new CBrush(RGB(0,255,0)));

 if(m_blue)
 pDC->FillRect(CRect(0,0, 750, 400), new CBrush(RGB(0,0,255)));

 CPen* pOldPen;

 if (m_drawLines)
 {
     CPen redPen(PS_SOLID, 16, RGB(255,0,0));
     pOldPen = pDC->SelectObject(&redPen);

  for (int x=0; x<6; ++x)
  {
   pDC->MoveTo(190+x*50, 10);
   pDC->LineTo(190+x*50, 380);
  }

  pDC->SelectObject(pOldPen);
 }

 CPen newPen(PS_SOLID, 8, RGB(0,0,255));
 pOldPen = pDC->SelectObject(&newPen);

 pDC->TextOut(20, 10, "R2_BLACK");
 pDC->TextOut(500, 10, "画黑线");
 pDC->SetROP2(R2_BLACK);                 //设置光栅操作码
 pDC->MoveTo(170, 20);
 pDC->LineTo(480, 20);
 pDC->TextOut(20, 30, "R2_WHITE");
 pDC->TextOut(500, 30, "画白线");
 pDC->SetROP2(R2_WHITE);
 pDC->MoveTo(170, 40);
 pDC->LineTo(480, 40);
 pDC->TextOut(20, 50, "R2_NOP");
 pDC->TextOut(500, 50, "不画线");
 pDC->SetROP2(R2_NOP);
 pDC->MoveTo(170, 60);
 pDC->LineTo(480, 60);
 pDC->TextOut(20, 70, "R2_NOT");
 pDC->TextOut(500, 70, "画屏幕反色线");
 pDC->SetROP2(R2_NOT);
 pDC->MoveTo(170, 80);
 pDC->LineTo(480, 80);
 pDC->TextOut(20, 90, "R2_COPYPEN");
 pDC->TextOut(500, 90, "画画笔颜色线");
 pDC->SetROP2(R2_COPYPEN);
 pDC->MoveTo(170, 100);
 pDC->LineTo(480, 100);
 pDC->TextOut(20, 110, "R2_NOTCOPYPEN");
 pDC->TextOut(500, 110, "画画笔反色线");
 pDC->SetROP2(R2_NOTCOPYPEN);
 pDC->MoveTo(170, 120);
 pDC->LineTo(480, 120);
 pDC->TextOut(20, 130, "R2_MERGEPENNOT");
 pDC->TextOut(500, 130, "画画笔色与屏幕反色OR线");
 pDC->SetROP2(R2_MERGEPENNOT);
 pDC->MoveTo(170, 140);
 pDC->LineTo(480, 140);
 pDC->TextOut(20, 150, "R2_MASKPENNOT");
 pDC->TextOut(500, 150, "画画笔色与屏幕反色AND线");
 pDC->SetROP2(R2_MASKPENNOT);
 pDC->MoveTo(170, 160);
 pDC->LineTo(480, 160);
 pDC->TextOut(20, 170, "R2_MERGENOTPEN");
 pDC->TextOut(500, 170, "画画笔反色与屏幕色OR线");
 pDC->SetROP2(R2_MERGENOTPEN);
 pDC->MoveTo(170, 180);
 pDC->LineTo(480, 180);
 pDC->TextOut(20, 190, "R2_MASKNOTPEN");
 pDC->TextOut(500, 190, "画画笔反色与屏幕色AND线");
 pDC->SetROP2(R2_MASKNOTPEN);
 pDC->MoveTo(170, 200);
 pDC->LineTo(480, 200);
 pDC->TextOut(20, 210, "R2_MERGEPEN");
 pDC->TextOut(500, 210, "画画笔色与屏幕色OR线");
 pDC->SetROP2(R2_MERGEPEN);
 pDC->MoveTo(170, 220);
 pDC->LineTo(480, 220);
 pDC->TextOut(20, 230, "R2_NOTMERGEPEN");
 pDC->TextOut(500, 230, "画画笔色与屏幕色OR后取反线");
 pDC->SetROP2(R2_NOTMERGEPEN);
 pDC->MoveTo(170, 240);
 pDC->LineTo(480, 240);
 pDC->TextOut(20, 250, "R2_MASKPEN");
 pDC->TextOut(500, 250, "画画笔色与屏幕色AND线");
 pDC->SetROP2(R2_MASKPEN);
 pDC->MoveTo(170, 260);
 pDC->LineTo(480, 260);
 pDC->TextOut(20, 270, "R2_NOTMASKPEN");
 pDC->TextOut(500, 270, "画画笔色与屏幕色AND后取反线");
 pDC->SetROP2(R2_NOTMASKPEN);
 pDC->MoveTo(170, 280);
 pDC->LineTo(480, 280);
 pDC->TextOut(20, 290, "R2_XORPEN");
 pDC->TextOut(500, 290, "画画笔色与屏幕色XOR线");
 pDC->SetROP2(R2_XORPEN);
 pDC->MoveTo(170, 300);
 pDC->LineTo(480, 300);
 pDC->TextOut(20, 310, "R2_NOTXORPEN");
 pDC->TextOut(500, 310, "画画笔色与屏幕色XOR后取反线");
 pDC->SetROP2(R2_NOTXORPEN);
 pDC->MoveTo(170, 320);
 pDC->LineTo(480, 320);

 pDC->SelectObject(pOldPen);
}

void CLineView::OnLButtonDown(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default
 m_drawLines = !m_drawLines;
 Invalidate();
 CView::OnLButtonDown(nFlags, point);
}

void CLineView::OnEditBlue()
{
 // TODO: Add your command handler code here
 m_blue = !m_blue;
 m_green = FALSE;
 Invalidate(); 
}

void CLineView::OnEditGreen()
{
 // TODO: Add your command handler code here
 m_green = !m_green;
 m_blue = FALSE;
 Invalidate(); 
}

void CLineView::OnEditInitialize()
{
 // TODO: Add your command handler code here
 m_green=FALSE;
 m_blue=FALSE;
 Invalidate();
}

五、编译

六、运行


原文:
http://hi.baidu.com/yn_bg/item/d839fe5eb6cb043295eb05ac

你可能感兴趣的:(VC++)