GDI+学习之------ 画线、区域填充、写字
《精通GDI編程》里的代碼。在學(xué)習(xí)過程中對它加以總結(jié),以防以后用到,全部代碼都是在MFC 單文檔中實(shí)現(xiàn)的,寫在View::OnDraw(CDC */*pDC*/)中
畫線/邊框(Pen)
1、畫單線-------DrawLine
[cpp]view
plaincopy
Penpen(Color(255,0,0,0),3);
PointFL_PTStart(0,0);
PointFL_PTEnd(100,10);
graphics.DrawLine(&pen,L_PTStart,L_PTEnd);
2、連接線--------DrawLines
[cpp]view
plaincopy
PenblackPen(Color(255,0,0,0),3);
PointFpoint1(10.0f,10.0f);
PointFpoint2(10.0f,100.0f);
PointFpoint3(200.0f,50.0f);
PointFpoint4(250.0f,80.0f);
PointFpoints[4]={point1,point2,point3,point4};
PointF*pPoints=points;
graphics.DrawLines(&blackPen,pPoints,4);
解說:points數(shù)組中的每一個(gè)點(diǎn)都是連接線上的轉(zhuǎn)折點(diǎn),DrawLines會把它們依照順序一個(gè)個(gè)連接起來
3、畫矩形-----DrawRectangle,僅僅畫邊框。不畫背景色
[cpp]view
plaincopy
PenblackPen(Color(255,255,0,0),3);
Rectrect(0,0,100,100);
graphics.DrawRectangle(&blackPen,rect);
4、一次畫多個(gè)矩形----DrawRectangles
[cpp]view
plaincopy
PenblackPen(Color(255,0,255,0),3);
//定義三個(gè)矩形
RectFrect1(0.0f,0.0f,50.0f,60.0f);
RectFrect2(60.0f,70.0f,70.0f,100.0f);
RectFrect3(100.0f,0.0f,50.0f,50.0f);
RectFrects[]={rect1,rect2,rect3};
//RectF是對Rect的封裝
graphics.DrawRectangles(&blackPen,rects,3);
5、畫曲線-----DrawCurve
[cpp]view
plaincopy
PengreenPen(Color::Green,3);
PointFpoint1(100.0f,100.0f);
PointFpoint2(200.0f,50.0f);
PointFpoint3(400.0f,10.0f);
PointFpoint4(500.0f,100.0f);
PointFcurvePoints[4]={
point1,
point2,
point3,
point4};
PointF*pcurvePoints=curvePoints;
//畫曲線
graphics.DrawCurve(&greenPen,curvePoints,4);
//畫連接點(diǎn)和直線連接線
SolidBrushredBrush(Color::Red);
graphics.FillEllipse(&redBrush,Rect(95,95,10,10));//畫連接點(diǎn)
graphics.FillEllipse(&redBrush,Rect(195,45,10,10));
graphics.FillEllipse(&redBrush,Rect(395,5,10,10));
graphics.FillEllipse(&redBrush,Rect(495,95,10,10));
PenredPen(Color::Red,2);
graphics.DrawLines(&redPen,curvePoints,4);//畫連接線
注意:這里為了比較畫曲線與畫直線連接線的差別。我用綠色畫的曲線,用紅色畫的直線連接線。同一時(shí)候畫出了連接點(diǎn),大家能夠比較一下。
6、畫閉合曲線
[cpp]view
plaincopy
PengreenPen(Color::Green,3);
PointFpoint1(100.0f,100.0f);//開始點(diǎn)
PointFpoint2(200.0f,50.0f);
PointFpoint3(400.0f,10.0f);
PointFpoint4(500.0f,100.0f);
PointFpoint5(600.0f,200.0f);
PointFpoint6(700.0f,400.0f);
PointFpoint7(500.0f,500.0f);//結(jié)束點(diǎn)
PointFcurvePoints[7]={
point1,
point2,
point3,
point4,
point5,
point6,
point7};
PointF*pcurvePoints=curvePoints;
//畫閉合曲線
graphics.DrawClosedCurve(&greenPen,curvePoints,7);
//畫連接點(diǎn)
SolidBrushredBrush(Color::Red);
SolidBrushstartBrush(Color::Blue);
SolidBrushendBrush(Color::Black);
graphics.FillEllipse(&startBrush,Rect(95,95,10,10));
graphics.FillEllipse(&redBrush,Rect(495,95,10,10));
graphics.FillEllipse(&redBrush,Rect(195,45,10,10));
graphics.FillEllipse(&redBrush,Rect(395,5,10,10));
graphics.FillEllipse(&redBrush,Rect(595,195,10,10));
graphics.FillEllipse(&redBrush,Rect(695,395,10,10));
graphics.FillEllipse(&endBrush,Rect(495,495,10,10));
注意:藍(lán)色點(diǎn)是開始點(diǎn),黑色點(diǎn)是結(jié)束點(diǎn)
7、畫多邊形-----DrawPolygon,既然能畫閉合的曲線。肯定也有閉合的直線。當(dāng)然閉合的直線也就是所謂的多邊形
[cpp]view
plaincopy
PenblackPen(Color(255,0,0,0),3);
//創(chuàng)建點(diǎn)數(shù)組,DrawPolygon會按這些點(diǎn)的順序逐個(gè)連接起來
PointFpoint1(100.0f,100.0f);
PointFpoint2(200.0f,130.0f);
PointFpoint3(150.0f,200.0f);
PointFpoint4(50.0f,200.0f);
PointFpoint5(0.0f,130.0f);
PointFpoints[5]={point1,point2,point3,point4,point5};
PointF*pPoints=points;
//畫多邊形,也就是閉合直線
graphics.DrawPolygon(&blackPen,pPoints,5);
8、畫弧線----DrawArc
[cpp]view
plaincopy
PenredPen(Color::Red,3);
RectFellipseRect(0,0,200,100);
REALstartAngle=0.0f;//起始度數(shù)
REALsweepAngle=90.0f;//結(jié)尾時(shí)的度數(shù)
//畫弧線
graphics.DrawArc(&redPen,ellipseRect,startAngle,sweepAngle);
//畫出邊框,做為參考
PengreenPen(Color::Green,1);
graphics.DrawRectangle(&greenPen,ellipseRect);
9、畫扇形----DrawPie
[cpp]view
plaincopy
PenblackPen(Color(255,0,255,0),3);
//定義橢圓。然后在里面截一部分作為終于的扇形
RectFellipseRect(0,0,200,100);
REALstartAngle=40.0f;
REALsweepAngle=100.0f;
//畫扇形
graphics.DrawPie(&blackPen,ellipseRect,startAngle,sweepAngle);
先出效果圖:
這里要對它兩上名詞解說一下,什么叫startAngle(開始度數(shù)),什么叫sweepAngle(范圍度數(shù)也能叫掃過度數(shù),我譯的。嘿嘿)
看下MSDN里對DrawPie函數(shù)的解說就會懂了,里面有這個(gè)圖,給大家看一下
填充區(qū)域(SolidBrush)
1、填充閉合區(qū)域----FillClosedCurve,邊框相應(yīng):DrawClosedCurve
[cpp]view
plaincopy
SolidBrushblackBrush(Color(255,0,0,0));
PointFpoint1(100.0f,100.0f);
PointFpoint2(200.0f,50.0f);
PointFpoint3(250.0f,200.0f);
PointFpoint4(50.0f,150.0f);
PointFpoints[4]={point1,point2,point3,point4};
//填充閉合區(qū)域
graphics.FillClosedCurve(&blackBrush,points,4);
//為閉合區(qū)域畫邊框
PencurPen(Color::Green,3);
graphics.DrawClosedCurve(&curPen,points,4);
注意:從結(jié)果圖中也能夠看出填充區(qū)域(背景)和邊框是分離的,用FillClosedCurve來填充背景色,用DrawClosedCurve來畫邊框
2、填充橢圓---FillEllipse。邊框相應(yīng):DrawEllipse
[cpp]view
plaincopy
SolidBrushblackBrush(Color(255,0,0,0));
RectFellipseRect(0.0f,0.6f,200.8f,100.9f);
//填充橢圓
graphics.FillEllipse(&blackBrush,ellipseRect);
//畫邊框,當(dāng)然也能夠不畫
PenborderPen(Color::Green,3);
graphics.DrawEllipse(&borderPen,ellipseRect);
還有類似的幾個(gè)函數(shù)。這里就不一 一解說了
它們是:
[cpp]view
plaincopy
FillPie(Brush*brush,RectF&rect,REALstartAngle,REALsweepAngle)//填充扇形,相應(yīng)DrawPie
FillPolygon(Brush*brush,PointF*points,INTcount)//填充多邊形。相應(yīng)DrawPolygon
FillRectangle(Brush*brush,RectF&rect)//填充矩形,相應(yīng)DrawRectangle
FillRectangles(Brush*brush,RectF*rects,INTcount)//同一時(shí)候填充多個(gè)矩形。相應(yīng)DrawRectangles
還有是關(guān)于路徑和區(qū)域的,先記下,后面再說
[cpp]view
plaincopy
StatusFillPath(constBrush*brush,constGraphicsPath*path);
StatusFillRegion(constBrush*brush,constRegion*region);
寫字(SolidBrush)
形式一:Status DrawString( const WCHAR*string, INTlength, const Font*font,
const PointF&origin, const Brush*brush);
[cpp]view
plaincopy
Graphicsgraphics(this->GetDC()->m_hDC);
SolidBrushbrush(Color(255,0,0,255));
FontFamilyfontfamily(L"宋體");
Fontfont(&fontfamily,24,FontStyleRegular,UnitPixel);
PointFpointf(0,0);//PointF類對點(diǎn)進(jìn)行了封裝。這里是指定寫字的開始點(diǎn)
graphics.DrawString(L"GDI寫字",-1,&font,pointf,&brush);
//DrawString還有另外兩個(gè)重載形式,能實(shí)現(xiàn)更強(qiáng)大的功能
形式二:Status DrawString( const WCHAR*string, INTlength, const Font*font,
const RectF&layoutRect, const StringFormat*stringFormat, const Brush*brush);
[cpp]view
plaincopy
WCHARstring[256];
wcscpy(string,L"SampleText");
//Initializearguments.
FontmyFont(L"Arial",16);//字體
RectFlayoutRect(0.0f,0.0f,200.0f,50.0f);//矩形
//設(shè)定字體格式
StringFormatformat;
format.SetAlignment(StringAlignmentCenter);//水平方向的對齊方式,這里設(shè)置為水平居中
format.SetLineAlignment(StringAlignmentFar);//垂直方向的對齊方式,這里設(shè)置為垂直居下
SolidBrushblackBrush(Color(255,0,0,0));
//畫矩形邊框
graphics.DrawRectangle(&Pen(Color::Green,3),layoutRect);
//填充矩形背景
graphics.FillRectangle(&SolidBrush(Color(255,255,0,0)),layoutRect);
//DrawString,一定要先畫背景再寫字,要不然,字會被背景覆蓋
graphics.DrawString(
string,
wcslen(string),
&myFont,
layoutRect,
&format,
&blackBrush);
形式三:Status DrawString( const WCHAR*string, INTlength, const Font*font,
const PointF&origin, const StringFormat*stringFormat, const Brush*brush);
這樣的形式是形式一與形式二的結(jié)合,指定寫字開始點(diǎn)和字體格式,這里就不舉例了。
總結(jié)
以上是生活随笔為你收集整理的GDI+学习之------ 画线、区域填充、写字的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 长脸男生短发发型(长脸额头大男生适合什么
- 下一篇: crashRpt用法