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

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

編輯:Delphi

本例效果圖:

再學 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; {定用於檢查線帽是否為錨頭帽的掩碼}


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