欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

再学 GDI+[7] DrawLines - 绘制一组直线

程序员文章站 2022-03-31 14:16:22
...

 

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormPaint(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
uses
  GDIPOBJ,GDIPAPI;
var
  i:Integer = 0;
  PtArr:array of TGPPoint;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Canvas.Ellipse(x-2,y-2,x+2,y+2);
  inc(i);
  SetLength(PtArr,i); //动态数组如果不设置大小就使用的话,将会出错;
  PtArr[i-1].X:=x;    //保存好坐标
  PtArr[i-1].Y:=y;
  Text:=IntToStr(i);  //窗口标题栏显示第几个坐标
end;

procedure TForm1.FormPaint(Sender: TObject);
var
  g:TGPGraphics;
  p:TGPPen;
begin
  g:=TGPGraphics.Create(Canvas.Handle);
  g.Clear(aclWhite);
  p:=TGPPen.Create(aclRed,2);

  g.DrawLines(p,PGPPoint(PtArr),Length(PtArr)); //画一组线

  p.Free;
  g.Free;
end;
procedure TForm1.Button1Click(Sender: TObject); //擦除
begin
  i:=0;
  SetLength(PtArr,i);
  Repaint;
  Text:=IntToStr(i);
end;
procedure TForm1.Button2Click(Sender: TObject); //绘制
begin
  Repaint;
end;

end.

转载于:https://www.cnblogs.com/keyvip/archive/2010/12/31/1922798.html