GDI+學習及代碼總結之------畫線、區域填充、寫字

轉自:http://blog.csdn.net/harvic880925/article/details/9023329


《精通GDI編程》裏的代碼,在學習過程中對它加以總結,以防以後用到,所有代碼都是在MFC 單文檔中實現的,寫在View::OnDraw(CDC */*pDC*/)中

畫線/邊框(Pen)

1、畫單線-------DrawLine

[cpp] view plain copy
  1. Pen pen(Color(255,0,0,0),3);  
  2. PointF L_PTStart(0,0);  
  3. PointF L_PTEnd(100,10);  
  4. graphics.DrawLine(&pen,L_PTStart,L_PTEnd);  

2、連接線--------DrawLines

[cpp] view plain copy
  1. Pen blackPen(Color(255, 0, 0, 0), 3);  
  2.   
  3. PointF point1(10.0f, 10.0f);  
  4. PointF point2(10.0f, 100.0f);  
  5. PointF point3(200.0f, 50.0f);  
  6. PointF point4(250.0f, 80.0f);  
  7.   
  8. PointF points[4] = {point1, point2, point3, point4};  
  9. PointF* pPoints = points;  
  10.   
  11. graphics.DrawLines(&blackPen, pPoints, 4);  

講解:points數組中的每個點都是連接線上的轉折點,DrawLines會把它們按照順序一個個連接起來


3、畫矩形-----DrawRectangle,只畫邊框,不畫背景色

[cpp] view plain copy
  1. Pen blackPen(Color(255,255, 0, 0), 3);  
  2. Rect rect(0, 0, 100, 100);  
  3. graphics.DrawRectangle(&blackPen, rect);  

4、一次畫多個矩形----DrawRectangles

[cpp] view plain copy
  1. Pen blackPen(Color(255, 0, 255, 0), 3);  
  2. // 定義三個矩形  
  3. RectF rect1(0.0f, 0.0f, 50.0f, 60.0f);  
  4. RectF rect2(60.0f, 70.0f, 70.0f, 100.0f);  
  5. RectF rect3(100.0f, 0.0f, 50.0f, 50.0f);  
  6. RectF rects[] = {rect1, rect2, rect3};  
  7. //RectF是對Rect的封裝  
  8. graphics.DrawRectangles(&blackPen, rects, 3);  

5、畫曲線-----DrawCurve

[cpp] view plain copy
  1. Pen greenPen(Color::Green, 3);  
  2. PointF point1(100.0f, 100.0f);  
  3. PointF point2(200.0f, 50.0f);  
  4. PointF point3(400.0f, 10.0f);  
  5. PointF point4(500.0f, 100.0f);  
  6.   
  7. PointF curvePoints[4] = {  
  8.     point1,  
  9.     point2,  
  10.     point3,  
  11.     point4};  
  12.   
  13.     PointF* pcurvePoints = curvePoints;  
  14.   
  15.     // 畫曲線  
  16.     graphics.DrawCurve(&greenPen, curvePoints, 4);  
  17.   
  18.     //畫連接點和直線連接線  
  19.     SolidBrush redBrush(Color::Red);  
  20.     graphics.FillEllipse(&redBrush, Rect(95, 95, 10, 10));//畫連接點  
  21.     graphics.FillEllipse(&redBrush, Rect(195, 45, 10, 10));  
  22.     graphics.FillEllipse(&redBrush, Rect(395, 5, 10, 10));  
  23.     graphics.FillEllipse(&redBrush, Rect(495, 95, 10, 10));  
  24.   
  25.     Pen redPen(Color::Red, 2);  
  26.     graphics.DrawLines(&redPen,curvePoints,4);//畫連接線  

注意:這裏爲了比較畫曲線與畫直線連接線的區別,我用綠色畫的曲線,用紅色畫的直線連接線,同時畫出了連接點,大家可以比較一下。

6、畫閉合曲線

[cpp] view plain copy
  1. Pen greenPen(Color::Green, 3);  
  2. PointF point1(100.0f, 100.0f);//開始點  
  3. PointF point2(200.0f, 50.0f);  
  4. PointF point3(400.0f, 10.0f);  
  5. PointF point4(500.0f, 100.0f);  
  6. PointF point5(600.0f, 200.0f);  
  7. PointF point6(700.0f, 400.0f);  
  8. PointF point7(500.0f, 500.0f);//結束點  
  9.   
  10. PointF curvePoints[7] = {  
  11.     point1,  
  12.     point2,  
  13.     point3,  
  14.     point4,  
  15.     point5,  
  16.     point6,  
  17.     point7};  
  18.   
  19.     PointF* pcurvePoints = curvePoints;  
  20.   
  21.     //畫閉合曲線  
  22.     graphics.DrawClosedCurve(&greenPen, curvePoints, 7);  
  23.   
  24.     //畫連接點  
  25.     SolidBrush redBrush(Color::Red);  
  26.     SolidBrush startBrush(Color::Blue);  
  27.     SolidBrush endBrush(Color::Black);  
  28.     graphics.FillEllipse(&startBrush, Rect(95, 95, 10, 10));  
  29.     graphics.FillEllipse(&redBrush, Rect(495, 95, 10, 10));  
  30.     graphics.FillEllipse(&redBrush, Rect(195, 45, 10, 10));  
  31.     graphics.FillEllipse(&redBrush, Rect(395, 5, 10, 10));  
  32.     graphics.FillEllipse(&redBrush, Rect(595, 195, 10, 10));  
  33.     graphics.FillEllipse(&redBrush, Rect(695, 395, 10, 10));  
  34.     graphics.FillEllipse(&endBrush, Rect(495, 495, 10, 10));  

注意:藍色點是開始點,黑色點是結束點
7、畫多邊形-----DrawPolygon,既然能畫閉合的曲線,肯定也有閉合的直線,當然閉合的直線也就是所謂的多邊形

[cpp] view plain copy
  1. Pen blackPen(Color(255, 0, 0, 0), 3);  
  2. //創建點數組,DrawPolygon會按這些點的順序逐個連接起來  
  3. PointF point1(100.0f, 100.0f);  
  4. PointF point2(200.0f, 130.0f);  
  5. PointF point3(150.0f, 200.0f);  
  6. PointF point4(50.0f, 200.0f);  
  7. PointF point5(0.0f, 130.0f);  
  8. PointF points[5] = {point1, point2, point3, point4, point5};  
  9. PointF* pPoints = points;  
  10. // 畫多邊形,也就是閉合直線  
  11. graphics.DrawPolygon(&blackPen, pPoints, 5);  

8、畫弧線----DrawArc

[cpp] view plain copy
  1. Pen redPen(Color::Red, 3);  
  2. RectF ellipseRect(0, 0, 200, 100);  
  3. REAL startAngle = 0.0f;//起始度數  
  4. REAL sweepAngle = 90.0f;//結尾時的度數  
  5. // 畫弧線  
  6. graphics.DrawArc(&redPen, ellipseRect, startAngle, sweepAngle);  
  7. //畫出邊框,做爲參考  
  8. Pen greenPen(Color::Green, 1);  
  9. graphics.DrawRectangle(&greenPen,ellipseRect);  

9、畫扇形----DrawPie

[cpp] view plain copy
  1. Pen blackPen(Color(255, 0, 255, 0), 3);  
  2.   
  3. // 定義橢圓,然後在裏面截一部分作爲最終的扇形  
  4. RectF ellipseRect(0, 0, 200, 100);  
  5. REAL startAngle = 40.0f;  
  6. REAL sweepAngle = 100.0f;  
  7.   
  8. //畫扇形  
  9. graphics.DrawPie(&blackPen, ellipseRect, startAngle, sweepAngle);  
先出效果圖:

這裏要對它兩上名詞講解一下,什麼叫startAngle(開始度數),什麼叫sweepAngle(範圍度數也能叫掃過度數,我譯的,嘿嘿)

看下MSDN裏對DrawPie函數的講解就會懂了,裏面有這個圖,給大家看一下


填充區域(SolidBrush)

1、填充閉合區域----FillClosedCurve,邊框對應:DrawClosedCurve

[cpp] view plain copy
  1. SolidBrush blackBrush(Color(255, 0, 0, 0));  
  2.   
  3. PointF point1(100.0f, 100.0f);  
  4. PointF point2(200.0f, 50.0f);  
  5. PointF point3(250.0f, 200.0f);  
  6. PointF point4(50.0f, 150.0f);  
  7. PointF points[4] = {point1, point2, point3, point4};  
  8.   
  9. //填充閉合區域  
  10. graphics.FillClosedCurve(&blackBrush, points, 4);  
  11. //爲閉合區域畫邊框  
  12. Pen curPen(Color::Green,3);  
  13. graphics.DrawClosedCurve(&curPen,points,4);  

注意:從結果圖中也可以看出填充區域(背景)和邊框是分離的,用FillClosedCurve來填充背景色,用DrawClosedCurve來畫邊框

2、填充橢圓---FillEllipse,邊框對應:DrawEllipse

[cpp] view plain copy
  1. SolidBrush blackBrush(Color(255, 0, 0, 0));  
  2. RectF ellipseRect(0.0f, 0.6f, 200.8f, 100.9f);  
  3. //填充橢圓  
  4. graphics.FillEllipse(&blackBrush, ellipseRect);  
  5. //畫邊框,當然也可以不畫  
  6. Pen borderPen(Color::Green,3);  
  7. graphics.DrawEllipse(&borderPen,ellipseRect);  

還有類似的幾個函數,這裏就不一 一講解了

它們是:

[cpp] view plain copy
  1. FillPie(Brush* brush, RectF& rect, REAL startAngle, REAL sweepAngle)    //填充扇形,對應DrawPie  
  2.   
  3. FillPolygon(Brush* brush, PointF* points, INT count)                       //填充多邊形,對應DrawPolygon  
  4.   
  5. FillRectangle(Brush* brush, RectF& rect)                                          //填充矩形,對應DrawRectangle  
  6.   
  7. FillRectangles(Brush* brush, RectF* rects, INT count)                   //同時填充多個矩形,對應DrawRectangles  

還有是關於路徑和區域的,先記下,後面再說

[cpp] view plain copy
  1. Status FillPath( const Brush* brush, const GraphicsPath*path);  
  2.   
  3. Status FillRegion( const Brush* brush, const Region*region);  

寫字(SolidBrush)

形式一:Status DrawString( const WCHAR*string, INTlength, const Font* font, const PointF&origin, const Brush*brush);

[cpp] view plain copy
  1. Graphics graphics(this->GetDC()->m_hDC);  
  2.   
  3. SolidBrush brush(Color(255,0,0,255));  
  4.   
  5. FontFamily fontfamily(L"宋體");  
  6. Font font(&fontfamily,24,FontStyleRegular,UnitPixel);  
  7.   
  8. PointF  pointf(0,0);//PointF類對點進行了封裝,這裏是指定寫字的開始點  
  9.   
  10. graphics.DrawString(L"GDI寫字",-1,&font,pointf,&brush);  
  11. //DrawString還有另外兩個重載形式,能實現更強大的功能  


形式二:Status DrawString( const WCHAR*string, INT length, const Font*font, const RectF&layoutRect, const StringFormat*stringFormat, const Brush*brush);

[cpp] view plain copy
  1. WCHAR string[256];  
  2. wcscpy(string, L"Sample Text");  
  3.   
  4. // Initialize arguments.  
  5. Font myFont(L"Arial", 16);//字體  
  6. RectF layoutRect(0.0f, 0.0f, 200.0f, 50.0f);//矩形  
  7.   
  8. //設定字體格式  
  9. StringFormat format;  
  10. format.SetAlignment(StringAlignmentCenter); //水平方向的對齊方式,這裏設置爲水平居中  
  11. format.SetLineAlignment(StringAlignmentFar);//垂直方向的對齊方式,這裏設置爲垂直居下  
  12. SolidBrush blackBrush(Color(255, 0, 0, 0));  
  13.   
  14. //畫矩形邊框  
  15. graphics.DrawRectangle(&Pen(Color::Green, 3), layoutRect);  
  16. //填充矩形背景  
  17. graphics.FillRectangle(&SolidBrush(Color(255,255,0,0)),layoutRect);  
  18. //DrawString,一定要先畫背景再寫字,要不然,字會被背景覆蓋  
  19. graphics.DrawString(  
  20.     string,  
  21.     wcslen(string),  
  22.     &myFont,  
  23.     layoutRect,  
  24.     &format,  
  25.     &blackBrush);  

形式三:Status DrawString( const WCHAR*string, INTlength, const Font* font, const PointF&origin, const StringFormat*stringFormat, const Brush* brush);

這種形式是形式一與形式二的結合,指定寫字開始點和字體格式,這裏就不舉例了。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章