GdiPlus[33]: 基本绘图与填充命令
GdiPlus[33]: 基本绘图与填充命令
涂孟超 发表于4年前
GdiPlus[33]: 基本绘图与填充命令
  • 发表于 4年前
  • 阅读 4
  • 收藏 0
  • 点赞 0
  • 评论 0

移动开发云端新模式探索实践 >>>   


DrawLine();
DrawLines();
DrawArc();
DrawBezier();
DrawBeziers();
DrawRectangle();
DrawRectangles();
DrawEllipse();
DrawPie();
DrawPolygon();
DrawCurve();
DrawClosedCurve();

FillRectangle();
FillRectangles();
FillPolygon();
FillEllipse();
FillPie();
FillClosedCurve();

 
 
 
 
 

 

 

  

DrawLines、DrawPolygon、FillPolygon 测试图:



DrawLines、DrawPolygon、FillPolygon 测试代码:

uses GdiPlus;

procedure TForm1.FormPaint(Sender: TObject);
const
  Pts: array[0..3] of TGPPointF = ((X:20; Y:80), (X:60; Y:20), (X:100; Y:80), (X:60; Y:140));
var
  Graphics: IGPGraphics;
  Pen: IGPPen;
  Brush: IGPSolidBrush;
  Font: IGPFont;
  i: Integer;
begin
  Graphics := TGPGraphics.Create(Handle);
  Pen := TGPPen.Create($FFFF0000, 2);
  Brush := TGPSolidBrush.Create($FF000000);
  Font := TGPFont.Create(Canvas.Handle);

  Graphics.DrawLines(Pen, Pts);
  for i := 0 to Length(Pts) - 1 do Graphics.DrawString(IntToStr(i), Font, Pts[i], Brush);

  Graphics.TranslateTransform(110, 0);
  Graphics.DrawPolygon(Pen, Pts);

  Brush.Color := $FFFFFF00;
  Graphics.TranslateTransform(110, 0);
  Graphics.FillPolygon(Brush, Pts);

  Graphics.TranslateTransform(110, 0);
  Graphics.FillPolygon(Brush, Pts);
  Graphics.DrawPolygon(Pen, Pts);
end;

 
 
 
 
 

 

 

  

DrawRectangles、FillRectangles 测试图:



DrawRectangles、FillRectangles 测试代码:

uses GdiPlus;

procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  Pen: IGPPen;
  Brush: IGPSolidBrush;
  Rects: array[0..2] of TGPRect;
begin
  Rects[0].Initialize(40, 20, 20, 20);
  Rects[1].Initialize(30, 40, 40, 40);
  Rects[2].Initialize(10, 80, 80, 80);

  Graphics := TGPGraphics.Create(Handle);
  Pen := TGPPen.Create($FFFF0000, 2);
  Brush := TGPSolidBrush.Create($FFC0C0C0);

  Graphics.DrawRectangles(Pen, Rects);

  Graphics.TranslateTransform(110, 0);
  Graphics.FillRectangles(Brush, Rects);

  Graphics.TranslateTransform(110, 0);
  Graphics.FillRectangles(Brush, Rects);
  Graphics.DrawRectangles(Pen, Rects);
end;

 
 
 
 
 

 

 

  

DrawArc、DrawPie、FillPie 测试图:



DrawArc、DrawPie、FillPie 测试代码:

uses GdiPlus;

procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  Pen: IGPPen;
  Brush: IGPSolidBrush;
  Rect: TGPRect;
begin
  Rect.Initialize(15, 15, 160, 80);

  Graphics := TGPGraphics.Create(Handle);
  Pen := TGPPen.Create($FFFF0000, 2);
  Brush := TGPSolidBrush.Create($FFB0C4DE);

  Graphics.FillEllipse(Brush, Rect);
  Graphics.DrawArc(Pen, Rect, 0, 45);
  Graphics.TranslateTransform(0, Rect.Y + Rect.Height);

  Graphics.FillEllipse(Brush, Rect);
  Graphics.DrawPie(Pen, Rect, 0, 45);
  Graphics.TranslateTransform(0, Rect.Y + Rect.Height);

  Graphics.FillEllipse(Brush, Rect);
  Brush.Color := $FFFFD700;
  Graphics.FillPie(Brush, Rect, 0, 45);
  Graphics.TranslateTransform(0, Rect.Y + Rect.Height);

  Brush.Color := $FFB0C4DE;
  Graphics.FillEllipse(Brush, Rect);
  Brush.Color := $FFFFD700;
  Graphics.FillPie(Brush, Rect, 0, 45);
  Graphics.DrawPie(Pen, Rect, 0, 45);
end;

 
 
 
 
 

 

 

  

DrawCurve、DrawClosedCurve、FillClosedCurve 测试图:



DrawCurve、DrawClosedCurve、FillClosedCurve 测试代码:

uses GdiPlus;

const
  Pts: array [0..5] of TGPPoint = (
    (X: 10;  Y:  40),
    (X: 100; Y:  60),
    (X: 150; Y:  20),
    (X: 130; Y: 100),
    (X: 70;  Y:  80),
    (X: 30;  Y: 140));

procedure TForm1.FormPaint(Sender: TObject);
const
  m = 180;
  n = 160;
var
  Graphics: IGPGraphics;
  Pen: IGPPen;
  BrushBak, Brush: IGPBrush;
begin
  Graphics := TGPGraphics.Create(Handle);
  Pen := TGPPen.Create($FFFF0000, 2);
  BrushBak := TGPSolidBrush.Create($FFD8BFD8);
  Brush := TGPSolidBrush.Create($FFFFD700);

  with Graphics do
  begin
    FillPolygon(BrushBak, Pts);
    DrawCurve(Pen, Pts);
    TranslateTransform(m, 0);

    FillPolygon(BrushBak, Pts);
    DrawCurve(Pen, Pts, 1.5);
    TranslateTransform(m, 0);

    FillPolygon(BrushBak, Pts);
    DrawCurve(Pen, Pts, 0.5);
    TranslateTransform(m, 0);

    FillPolygon(BrushBak, Pts);
    DrawCurve(Pen, Pts, 0);

    //
    TranslateTransform(-Transform.OffsetX, 150);

    FillPolygon(BrushBak, Pts);
    DrawClosedCurve(Pen, Pts);
    TranslateTransform(m, 0);

    FillPolygon(BrushBak, Pts);
    DrawClosedCurve(Pen, Pts, 1.5);
    TranslateTransform(m, 0);

    FillPolygon(BrushBak, Pts);
    DrawClosedCurve(Pen, Pts, 0.5);
    TranslateTransform(m, 0);

    FillPolygon(BrushBak, Pts);
    DrawClosedCurve(Pen, Pts, 0);

    //
    TranslateTransform(-Transform.OffsetX, n);

    FillPolygon(BrushBak, Pts);
    FillClosedCurve(Brush, Pts);
    DrawClosedCurve(Pen, Pts);
    TranslateTransform(m, 0);

    FillPolygon(BrushBak, Pts);
    FillClosedCurve(Brush, Pts, FillModeAlternate, 1.5);
    DrawClosedCurve(Pen, Pts, 1.5);
    TranslateTransform(m, 0);

    FillPolygon(BrushBak, Pts);
    FillClosedCurve(Brush, Pts, FillModeAlternate, 0.5);
    DrawClosedCurve(Pen, Pts, 0.5);
    TranslateTransform(m, 0);

    FillPolygon(BrushBak, Pts);
    FillClosedCurve(Brush, Pts, FillModeAlternate, 0);
    DrawClosedCurve(Pen, Pts, 0);
  end;
end;

 
 
 
 
 

 

 

  

DrawBezier 测试图:



DrawBezier 测试代码:

uses GdiPlus;

//一条 Bezier 线需要四个点: 两个端点、两个控制点
procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  Pen: IGPPen;
  P1,C1,C2,P2: TGPPoint;
begin
  P1.Initialize(30, 100);
  C1.Initialize(120, 10);
  C2.Initialize(170, 150);
  P2.Initialize(220, 80);

  Graphics := TGPGraphics.Create(Handle);
  Pen := TGPPen.Create($FF0000FF, 2);

  Graphics.DrawBezier(Pen, P1, C1, C2, P2);

  Pen.Width := 1;
  Pen.Color := $FFFF0000;
  Graphics.DrawRectangle(Pen, P1.X-3, P1.Y-3, 6, 6);
  Graphics.DrawRectangle(Pen, P2.X-3, P2.Y-3, 6, 6);
  Graphics.DrawEllipse(Pen, C1.X-3, C1.Y-3, 6, 6);
  Graphics.DrawEllipse(Pen, C2.X-3, C2.Y-3, 6, 6);

  Pen.Color := $FFC0C0C0;
  Graphics.DrawLine(Pen, P1, C1);
  Graphics.DrawLine(Pen, P2, C2);
end;

//把一条当作多条画(DrawBeziers), 结果同上
procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  Pen: IGPPen;
  Pts: array[0..3] of TGPPoint;
begin
  Pts[0].Initialize(30, 100);
  Pts[1].Initialize(120, 10);
  Pts[2].Initialize(170, 150);
  Pts[3].Initialize(220, 80);

  Graphics := TGPGraphics.Create(Handle);
  Pen := TGPPen.Create($FF0000FF, 2);

  Graphics.DrawBeziers(Pen, Pts);

  Pen.Width := 1;
  Pen.Color := $FFFF0000;
  Graphics.DrawRectangle(Pen, Pts[0].X-3, Pts[0].Y-3, 6, 6);
  Graphics.DrawRectangle(Pen, Pts[3].X-3, Pts[3].Y-3, 6, 6);
  Graphics.DrawEllipse(Pen, Pts[1].X-3, Pts[1].Y-3, 6, 6);
  Graphics.DrawEllipse(Pen, Pts[2].X-3, Pts[2].Y-3, 6, 6);

  Pen.Color := $FFC0C0C0;
  Graphics.DrawLine(Pen, Pts[0], Pts[1]);
  Graphics.DrawLine(Pen, Pts[3], Pts[2]);
end;

 
 
 
 
 

 

 

  

DrawBeziers 测试图:



DrawBeziers 测试代码:

uses GdiPlus;

const
  Pts: array[0..6] of TGPPoint = (
    (X: 24; Y: 32), // Pt1
    (X: 16; Y: 88), // C1
    (X: 36; Y:132), // C2
    (X:120; Y: 20), // Pt2
    (X:180; Y:150), // C3
    (X:210; Y: 90), // C4
    (X:210; Y: 20)  // Pt3
  );

//两条 Bezier 线需要 7 个点, 而不是 8 个; 因为 Bezier1 的终点即是 Bezier2 的起点;
//多条 Bezier 线需要的点数 = 3 * 线数 + 1;
//数组 ID 能被 3 整除的点是端点, 其他是控制点
procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  Pen: IGPPen;
  Brush: IGPBrush;
  Rect: TGPRect;
  i: Integer;
begin
  Graphics := TGPGraphics.Create(Handle);
  Pen := TGPPen.Create($FFFF0000, 2);
  Brush := TGPSolidBrush.Create($800000FF);

  Graphics.DrawBeziers(Pen, Pts);

  for i := 0 to Length(Pts) - 1 do
  begin
    Rect.Initialize(Pts[i].X - 4, Pts[i].Y - 4, 8, 8);
    if i mod 3 = 0 then
      Graphics.FillRectangle(Brush, Rect)
    else
      Graphics.FillEllipse(Brush, Rect);
  end;
end;

 
 
 
 
 

 

 

  
  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 12
博文 2004
码字总数 14107
×
涂孟超
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: