程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 再學GDI+[25]: TGPPen - 寬度、顏色與線帽

再學GDI+[25]: TGPPen - 寬度、顏色與線帽

編輯:Delphi

本例效果圖:

代碼文件: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 p 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.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved