随笔 - 2146  文章 - 97 评论 - 11765 trackbacks - 253

本例效果图:



代码文件:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    LabeledEdit1: TLabeledEdit;
    RadioGroup1: TRadioGroup;
    procedure FormCreate(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
    procedure LabeledEdit1Change(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses GDIPOBJ, GDIPAPI;

var
  arr1: array[0..5] of Single; {用于均分线复合画笔; 数组大小没有限制, 可用动态数组}
  arr2: array[0..3] of Single; {用于粗细线复合画笔; 数组大小没有限制, 可用动态数组}

procedure TForm1.FormCreate(Sender: TObject);
begin
  {均分线复合画笔; 6 个元素决定 3 条子线; 数组元素为百分比, 应在 0-1 之间}
  arr1[0] := 0.00; {第一线起点}
  arr1[1] := 0.10; {第一线终点}
  arr1[2] := 0.45; {第二线起点}
  arr1[3] := 0.55; {第二线终点}
  arr1[4] := 0.90; {第三线起点}
  arr1[5] := 1.00; {第三线终点}

  {粗细线复合画笔; 4 个元素决定 2 条子线; }
  arr2[0] := 0.00; {第一线起点}
  arr2[1] := 0.50; {第一线终点}
  arr2[2] := 0.90; {第二线起点}
  arr2[3] := 1.00; {第二线终点}

  LabeledEdit1.EditLabel.Caption := '笔宽';
  LabeledEdit1.Text := '10';
  RadioGroup1.Caption := '线型';
  RadioGroup1.Items.CommaText := '单实线,均分线,粗细线';
  RadioGroup1.ItemIndex := 0;
end;

procedure TForm1.FormPaint(Sender: TObject);
var
  g: TGPGraphics;
  p: TGPPen;
  r: TRect;
begin
  g := TGPGraphics.Create(Canvas.Handle);
  g.Clear($FFFFFFFF);
  p := TGPPen.Create(aclCoral);

  p.SetWidth(StrToFloatDef(LabeledEdit1.Text, 10));

  case RadioGroup1.ItemIndex of
    1: p.SetCompoundArray(@arr1, Length(arr1));
    2: p.SetCompoundArray(@arr2, Length(arr2));
  end;

  g.DrawLine(p, 0, 0, ClientWidth, ClientHeight);
  r := ClientRect;
  g.DrawEllipse(p, MakeRect(r));
  InflateRect(r, -ClientWidth div 4, -ClientHeight div 4);
  g.DrawLine(p, 0, 0, ClientWidth, ClientHeight);
  g.DrawRectangle(p, MakeRect(r));

  p.Free;
  g.Free;
end;

procedure TForm1.LabeledEdit1Change(Sender: TObject);
begin
  Repaint;
end;

procedure TForm1.RadioGroup1Click(Sender: TObject);
begin
  Repaint;
end;

end.

窗体文件:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 206
  ClientWidth = 339
  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 LabeledEdit1: TLabeledEdit
    Left = 264
    Top = 19
    Width = 67
    Height = 21
    EditLabel.Width = 61
    EditLabel.Height = 13
    EditLabel.Caption = 'LabeledEdit1'
    TabOrder = 0
    OnChange = LabeledEdit1Change
  end
  object RadioGroup1: TRadioGroup
    Left = 264
    Top = 46
    Width = 67
    Height = 66
    Caption = 'RadioGroup1'
    TabOrder = 1
    OnClick = RadioGroup1Click
  end
end

posted on 2008-06-13 13:51 万一 阅读(...) 评论(...) 编辑 收藏