再学 GDI+[25]: TGPPen - 宽度、颜色与线帽

本例效果图:

再学 GDI+[25]: TGPPen - 宽度、颜色与线帽

代码文件:

unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, TeCanvas, ComCtrls;



type

  TForm1 = class(TForm)

    ComboBox1: TComboBox;

    ComboBox2: TComboBox;

    TrackBar1: TTrackBar;

    ButtonColor1: TButtonColor;

    procedure FormCreate(Sender: TObject);

    procedure FormPaint(Sender: TObject);

    procedure ComboBox1Change(Sender: TObject);

    procedure ComboBox2Change(Sender: TObject);

    procedure TrackBar1Change(Sender: TObject);

    procedure ButtonColor1Click(Sender: TObject);

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



uses GDIPOBJ,GDIPAPI, TypInfo;



var

  StartCap, EndCap: Integer;

  LineWidth: Single = 2;

  LineColor: TGPColor = aclRed;



procedure TForm1.FormCreate(Sender: TObject);

begin

  with ComboBox1.Items do

  begin

    Add('LineCapFlat');

    Add('LineCapSquare');

    Add('LineCapRound');

    Add('LineCapTriangle');

    Add('LineCapNoAnchor');

    Add('LineCapSquareAnchor');

    Add('LineCapRoundAnchor');

    Add('LineCapDiamondAnchor');

    Add('LineCapArrowAnchor');

    Add('LineCapCustom');

  end;



  ComboBox1.ItemIndex := 0;

  ComboBox2.Items := ComboBox1.Items;

  ComboBox2.ItemIndex := 0;



  TrackBar1.ShowSelRange := False;

  TrackBar1.Min := 1;

  TrackBar1.Max := 50;

  TrackBar1.Position := 2;



  ButtonColor1.SymbolColor := clRed;

end;



procedure TForm1.FormPaint(Sender: TObject);

var

  g: TGPGraphics;

  p: TGPPen;

  x1,y1,x2,y2: Integer;

begin

  g := TGPGraphics.Create(Canvas.Handle);

  p := TGPPen.Create(0);

  g.Clear(MakeColor(255,255,255));



  x1 := 50;

  x2 := ClientWidth - 50;

  y1 := ClientHeight div 2;

  y2 := y1;



  p.SetColor(LineColor);

  p.SetWidth(LineWidth);

  p.SetStartCap(StartCap);

  p.SetEndCap(EndCap);

  g.DrawLine(p, x1, y1, x2, y2);



  g.Free;

  p.Free;

end;



procedure TForm1.ComboBox1Change(Sender: TObject);

var

  i: Integer;

begin

  i := 0;

  case TComboBox(Sender).ItemIndex of

    0 : i := LineCapFlat;

    1 : i := LineCapSquare;

    2 : i := LineCapRound;

    3 : i := LineCapTriangle;

    4 : i := LineCapNoAnchor;

    5 : i := LineCapSquareAnchor;

    6 : i := LineCapRoundAnchor;

    7 : i := LineCapDiamondAnchor;

    8 : i := LineCapArrowAnchor;

    9 : i := LineCapCustom;

    10: i := LineCapAnchorMask;

  end;

  if TComboBox(Sender) = ComboBox1 then StartCap := i;

  if TComboBox(Sender) = ComboBox2 then EndCap := i;

  Repaint;

end;



procedure TForm1.ComboBox2Change(Sender: TObject);

begin

  ComboBox1Change(Sender);

end;



procedure TForm1.TrackBar1Change(Sender: TObject);

begin

  LineWidth := TrackBar1.Position;

  Repaint;

end;



procedure TForm1.ButtonColor1Click(Sender: TObject);

begin

  LineColor := ColorRefToARGB(ButtonColor1.SymbolColor);

  Repaint;

end;



end.


 
   
窗体文件:

object Form1: TForm1

  Left = 0

  Top = 0

  Caption = 'Form1'

  ClientHeight = 206

  ClientWidth = 315

  Color = clBtnFace

  Font.Charset = DEFAULT_CHARSET

  Font.Color = clWindowText

  Font.Height = -11

  Font.Name = 'Tahoma'

  Font.Style = []

  OldCreateOrder = False

  Position = poDesktopCenter

  OnCreate = FormCreate

  OnPaint = FormPaint

  PixelsPerInch = 96

  TextHeight = 13

  object ComboBox1: TComboBox

    Left = 8

    Top = 8

    Width = 145

    Height = 21

    ItemHeight = 13

    TabOrder = 0

    Text = 'ComboBox1'

    OnChange = ComboBox1Change

  end

  object ComboBox2: TComboBox

    Left = 162

    Top = 8

    Width = 145

    Height = 21

    ItemHeight = 13

    TabOrder = 1

    Text = 'ComboBox2'

    OnChange = ComboBox2Change

  end

  object TrackBar1: TTrackBar

    Left = 3

    Top = 182

    Width = 223

    Height = 20

    TabOrder = 2

    OnChange = TrackBar1Change

  end

  object ButtonColor1: TButtonColor

    Left = 232

    Top = 177

    Caption = 'ButtonColor1'

    TabOrder = 3

    OnClick = ButtonColor1Click

  end

end


 
   
附线帽样式常数:

LineCapFlat          = 0;   {指定平线帽}

LineCapSquare        = 1;   {指定方线帽}

LineCapRound         = 2;   {指定圆线帽}

LineCapTriangle      = 3;   {指定三角线帽}

LineCapNoAnchor      = $10; {指定没有锚}

LineCapSquareAnchor  = $11; {指定方锚头帽}

LineCapRoundAnchor   = $12; {指定圆锚头帽}

LineCapDiamondAnchor = $13; {指定菱形锚头帽}

LineCapArrowAnchor   = $14; {指定箭头状锚头帽}

LineCapCustom        = $ff; {指定自定义线帽}



LineCapAnchorMask    = $f0; {定用于检查线帽是否为锚头帽的掩码}


 
   

你可能感兴趣的:(DI)