mfc路径层

转自:http://blog.csdn.net/sergery/article/details/9241521

路径层 CDC::BeginPath,BeginPath
   如何在Device Context中建立了一个路径层:
   1)即调用BeginPath()
   2)再调用其他的GDI绘图函数参数一个区域,如Rectangle生成一个矩形区域
   3)调用EndPath()  产生闭环的路径层(即一个绘图区域) 


           pDC->BeginPath();
  pDC->Rectangle(200,200,200+csz.cx,200+csz.cy);
          //pDC->TextOut(200,200,cstr);//好像路径层上不能输出文本?只能画点,矩形,椭圆等绘图操作?
看来路径层确实只是圈了一块区域,是用点,矩形,椭圆等等图形区域来圈出的路径层(即区域),然后再与客户区进行
图形区域叠加运算
  pDC->EndPath();           
     SelectClipPath(RGN_DIFF);//把路径层的绘图区域与Device Context中的绘图区域进行 OR,XOR,COPY,AND,DIFF运算.
     这5个参数会产生新的不同的绘图区,那么就对下面的画横线和竖线的现实产生影响.


总之: 路径区圈好后,还必须与客户区进行5种SelectClipPath()"裁剪"运算,产生新的绘图区域,在新的绘图区域上的绘图就能显示出路径层的特效.


  1. //------路径层 CDC::BeginPath, EndPath  
  2.       CString cstr;  
  3.        cstr="VC++ 路径层用矩形圈出的绘图区域";  
  4.        pDC->TextOut(100,100,cstr);  
  5.        CSize csz;  
  6.        csz = pDC->GetTextExtent(cstr);  
  7.        pDC->BeginPath();  
  8.        pDC->Rectangle(100,100,100+csz.cx,100+csz.cy);//在路径层上绘制一个矩形:就是字符串cstr的所占的区域  
  9.        pDC->EndPath();  // 如果不取消则下面画的横竖线就看不见,因为还是在路径层上  
  10.        pDC->SelectClipPath(RGN_XOR);//把路径层的绘图与Device Context中的绘图区域进行 OR,XOR,COPY,AND,DIFF运算,产生了新的绘图区  

//

CDC::SelectClipPath

BOOL SelectClipPath( int nMode );

Return Value

Nonzero if the function is successful; otherwise 0.

Parameters

nMode

Specifies the way to use the path. The following values are allowed:

  • RGN_AND   The new clipping region includes the intersection (overlapping areas) of the current clipping region and the current path.

  • RGN_COPY   The new clipping region is the current path.

  • RGN_DIFF   The new clipping region includes the areas of the current clipping region, and those of the current path are excluded.

  • RGN_OR   The new clipping region includes the union (combined areas) of the current clipping region and the current path.

  • RGN_XOR   The new clipping region includes the union of the current clipping region and the current path, but without the overlapping areas


直接用一个矩形 (100,100,200,200)来圈一个路径层,不用上面的一行字符的区域来圈了.

代码:

  1. //------路径层 CDC::BeginPath, EndPath  
  2.       CString cstr;  
  3.        cstr="路径层";  
  4.        pDC->TextOut(150,150,cstr);    //把cstr输出在(150,150)这个点是将要创建的路径层的中心     
  5.        pDC->BeginPath();  
  6.        pDC->Rectangle(100,100,200,200);//在路径层上绘制一个矩形:就是字符串cstr的所占的区域  
  7.        pDC->EndPath();  // 如果不取消则下面画的横竖线就看不见,因为还是在路径层上  
  8.        pDC->SelectClipPath(RGN_OR);//把路径层的绘图与Device Context中的绘图区域进行 OR,XOR,COPY,AND,DIFF运算,产生了新的绘图区  
  9.   
  10.        //下面的横竖线就是在新的绘图区上绘制,     
  11.        for(int i=0;i<300;i+=10)  
  12.        {  
  13.            //画横线  
  14.            pDC->MoveTo(0,i);  
  15.            pDC->LineTo(300,i);  
  16.            //画竖线  
  17.            pDC->MoveTo(i,0);  
  18.            pDC->LineTo(i,300);        
  19.        }    

1. 不进行"裁剪" 也就是注释掉 //  pDC->SelectClipPath(RGN_OR);

   


2. pDC->SelectClipPath(RGN_OR): 客户区与路径区的合集

  


3 pDC->SelectClipPath(RGN_XOR): 客户区与路径区的非交集. 也就是包括了客户区与路径区,但是排除掉他们重叠的部分.本示例中路径区完全位于客户区,所以路径区就是被

排除掉的那个重叠区.



4 pDC->SelectClipPath(RGN_COPY) :只包括当前路径区



5. pDC->SelectClipPath(RGN_AND):原有客户区与路径区的交集



6  pDC->SelectClipPath(RGN_DIFF): 包括原有客户区,不包括路径区




所有源代码:

  1. // TextView.cpp : implementation of the CTextView class  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include "Text.h"  
  6.   
  7. #include "TextDoc.h"  
  8. #include "TextView.h"  
  9.   
  10. #ifdef _DEBUG  
  11. #define new DEBUG_NEW  
  12. #undef THIS_FILE  
  13. static char THIS_FILE[] = __FILE__;  
  14. #endif  
  15.   
  16. /////////////////////////////////////////////////////////////////////////////  
  17. // CTextView  
  18.   
  19. IMPLEMENT_DYNCREATE(CTextView, CView)  
  20.   
  21. BEGIN_MESSAGE_MAP(CTextView, CView)  
  22.     //{{AFX_MSG_MAP(CTextView)  
  23.     ON_WM_CREATE()   //在 Message map tables for Windows messages消息映射表中定义的宏,调用OnCreate函数  
  24.     //}}AFX_MSG_MAP  
  25.     // Standard printing commands  
  26.     ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)  
  27.     ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)  
  28.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)  
  29. END_MESSAGE_MAP()  
  30.   
  31. /////////////////////////////////////////////////////////////////////////////  
  32. // CTextView construction/destruction  
  33.   
  34. CTextView::CTextView()  
  35. {  
  36.     // TODO: add construction code here  
  37.   
  38. }  
  39.   
  40. CTextView::~CTextView()  
  41. {  
  42. }  
  43.   
  44. BOOL CTextView::PreCreateWindow(CREATESTRUCT& cs)  
  45. {  
  46.     // TODO: Modify the Window class or styles here by modifying  
  47.     //  the CREATESTRUCT cs  
  48.   
  49.     return CView::PreCreateWindow(cs);  
  50. }  
  51.   
  52. /////////////////////////////////////////////////////////////////////////////  
  53. // CTextView drawing  
  54.   
  55. void CTextView::OnDraw(CDC* pDC) // 这个函数传入了一个device context指针,因为在客户区画图不用在定义一个DC了.  
  56. {  
  57.     CTextDoc* pDoc = GetDocument();  
  58.     ASSERT_VALID(pDoc);  
  59.     // TODO: add draw code for native data here  
  60.       
  61. //------路径层 CDC::BeginPath, EndPath  
  62.       CString cstr;  
  63.        cstr="路径层";  
  64.        pDC->TextOut(150,150,cstr);    //把cstr输出在(150,150)这个点是将要创建的路径层的中心     
  65.        pDC->BeginPath();  
  66.        pDC->Rectangle(100,100,200,200);//在路径层上绘制一个矩形:就是字符串cstr的所占的区域  
  67.        pDC->EndPath();  // 如果不取消则下面画的横竖线就看不见,因为还是在路径层上  
  68.        pDC->SelectClipPath(RGN_DIFF);//把路径层的绘图与Device Context中的绘图区域进行 OR,XOR,COPY,AND,DIFF运算,产生了新的绘图区  
  69.   
  70.        //下面的横竖线就是在新的绘图区上绘制,     
  71.        for(int i=0;i<300;i+=10)  
  72.        {  
  73.            //画横线  
  74.            pDC->MoveTo(0,i);  
  75.            pDC->LineTo(300,i);  
  76.            //画竖线  
  77.            pDC->MoveTo(i,0);  
  78.            pDC->LineTo(i,300);        
  79.        }    
  80. }  
  81.   
  82. /////////////////////////////////////////////////////////////////////////////  
  83. // CTextView printing  
  84.   
  85. BOOL CTextView::OnPreparePrinting(CPrintInfo* pInfo)  
  86. {  
  87.     // default preparation  
  88.     return DoPreparePrinting(pInfo);  
  89. }  
  90.   
  91. void CTextView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)  
  92. {  
  93.     // TODO: add extra initialization before printing  
  94. }  
  95.   
  96. void CTextView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)  
  97. {  
  98.     // TODO: add cleanup after printing  
  99. }  
  100.   
  101. /////////////////////////////////////////////////////////////////////////////  
  102. // CTextView diagnostics  
  103.   
  104. #ifdef _DEBUG  
  105. void CTextView::AssertValid() const  
  106. {  
  107.     CView::AssertValid();  
  108. }  
  109.   
  110. void CTextView::Dump(CDumpContext& dc) const  
  111. {  
  112.     CView::Dump(dc);  
  113. }  
  114.   
  115. CTextDoc* CTextView::GetDocument() // non-debug version is inline  
  116. {  
  117.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTextDoc)));  
  118.     return (CTextDoc*)m_pDocument;  
  119. }  
  120. #endif //_DEBUG  
  121.   
  122. /////////////////////////////////////////////////////////////////////////////  
  123. // CTextView message handlers  
  124.   
  125. int CTextView::OnCreate(LPCREATESTRUCT lpCreateStruct)   
  126. {  
  127.     if (CView::OnCreate(lpCreateStruct) == -1)  
  128.         return -1;  
  129.       
  130.     // TODO: Add your specialized creation code here  
  131.     // GetSystemMetrics   
  132.   
  133.   
  134. //------创建文本插入符  
  135.     /* 
  136.     CClientDC dc(this); 
  137.     TEXTMETRIC tm; 
  138.     dc.GetTextMetrics(&tm); 
  139.     CreateSolidCaret(tm.tmAveCharWidth/8,tm.tmHeight); 
  140.     ShowCaret(); 
  141.     */  
  142.   
  143.   
  144.   
  145. //------创建图形插入符  
  146.     /* 
  147.       1. 创建一个位图对象CBitmap bitmap,且应作为全局的成员存在,因此把这个对象作为CTextView类的一个私有成员. 
  148.       2. CreateCare()函数来创建一个插入符 
  149.       3. 发现运行后闪烁的位图背景色和前景色均不是我设计的那个颜色,奇怪了... 
  150.      */  
  151.   
  152. //  CBitmap bitmap; 放在这里没有用,OnCreate消息处理完函数返回(return 0)后,这个临时bitmap资源类对象发生了析构,内存中没有了.  
  153.     bitmap.LoadBitmap(IDB_BITMAP1);  
  154.     CClientDC dc(this);  
  155. //  CreateCaret(&bitmap);  
  156. //  ShowCaret();  
  157.   
  158.   
  159.   
  160. //---  
  161.     return 0;  
  162. }  
原文地址:https://www.cnblogs.com/zfluo/p/5131855.html