Direct2D (2) : 基本图形命令测试
生活随笔
收集整理的這篇文章主要介紹了
Direct2D (2) : 基本图形命令测试
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??
代碼:
unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls, ExtCtrls, ComCtrls, TypInfo, Direct2D;typeTForm1 = class(TForm)ListBox1: TListBox;Panel1: TPanel;GroupBox1: TGroupBox;CheckBox1: TCheckBox;Edit1: TEdit;UpDown1: TUpDown;ColorBox1: TColorBox;GroupBox2: TGroupBox;CheckBox2: TCheckBox;ColorBox2: TColorBox;PaintBox1: TPaintBox;procedure FormCreate(Sender: TObject);procedure PaintBox1Paint(Sender: TObject);procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);procedure PaintBox1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);procedure ListBox1Click(Sender: TObject);end;varForm1: TForm1;implementation{$R *.dfm}typeTEnumDraw = (eEllipse,eLine,eRectangle,eRoundRect,eFrameRect,ePie,eArc,eChord,ePolyLine,ePolygon,ePolyBezier);TPointArr4 = array[0..3] of TPoint; varptss: array[TEnumDraw] of TPointArr4; //點數(shù)組的數(shù)組ppts: ^TPointArr4; //某個圖形需要的點數(shù)組的指針ppt: PPoint; //某個點的指針enum: TEnumDraw; //表示當(dāng)前選擇的要繪制的圖形類型flag: Boolean; //判斷鼠標(biāo)是否按在操控點上{初始化數(shù)據(jù)} procedure TForm1.FormCreate(Sender: TObject); vare: TEnumDraw; begin{初始化點數(shù)組}ptss[eEllipse][0] := Point(100,50);ptss[eEllipse][1] := Point(200,150);ptss[eEllipse][2] := Point(MaxInt,MaxInt);ptss[eEllipse][3] := Point(MaxInt,MaxInt);ptss[eRectangle] := ptss[eEllipse];ptss[eLine] := ptss[eEllipse];ptss[eRoundRect] := ptss[eEllipse];ptss[eFrameRect] := ptss[eEllipse];ptss[ePie][0] := Point(100,50);ptss[ePie][1] := Point(200,150);ptss[ePie][2] := Point(150,50);ptss[ePie][3] := Point(100,150);ptss[eArc] := ptss[ePie];ptss[eChord] := ptss[ePie];ptss[ePolyLine][0] := Point(100,50);ptss[ePolyLine][1] := Point(200,50);ptss[ePolyLine][2] := Point(200,150);ptss[ePolyLine][3] := Point(100,150);ptss[ePolygon] := ptss[ePolyLine];ptss[ePolyBezier] := ptss[ePolyLine];{填充 ListBox1}for e := Low(TEnumDraw) to High(TEnumDraw) dobeginListBox1.Items.Add(GetEnumName(TypeInfo(TEnumDraw), ord(e)));end;ListBox1.ItemIndex := 0;{初始化控件}Panel1.Caption := '';UpDown1.Associate := Edit1;Edit1.NumbersOnly := True;Edit1.Alignment := taCenter;UpDown1.Associate := Edit1;UpDown1.Min := 1;CheckBox1.Checked := True;CheckBox2.Checked := True;ColorBox1.Selected := clBlue;ColorBox2.Selected := clLime;{事件共享}CheckBox1.OnClick := ListBox1.OnClick;CheckBox2.OnClick := ListBox1.OnClick;ColorBox1.OnChange := ListBox1.OnClick;ColorBox2.OnChange := ListBox1.OnClick;Edit1.OnChange := ListBox1.OnClick; end;procedure TForm1.ListBox1Click(Sender: TObject); beginenum := TEnumDraw(ListBox1.ItemIndex);ppts := @ptss[enum];PaintBox1.Invalidate; end;procedure TForm1.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); beginflag := PaintBox1.Cursor = crCross; end;procedure TForm1.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); vari: Integer; beginif flag thenbeginppt^ := Point(X, Y);PaintBox1.Invalidate;Exit;end;{判斷鼠標(biāo)是否在控制點上}for i := 0 to Length(ppts^) - 1 dobeginif (ppts^[i].X <> MaxInt) and PtInRect(Rect(ppts^[i].X-4, ppts^[i].Y-4, ppts^[i].X+4, ppts^[i].Y+4), Point(X,Y)) thenbeginPaintBox1.Cursor := crCross;ppt := @ppts^[i]; //哪個控制點Exit;end elsePaintBox1.Cursor := crDefault;end; end;procedure TForm1.PaintBox1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); beginflag := False; end;{繪制} procedure TForm1.PaintBox1Paint(Sender: TObject); varpts: TPointArr4;pt: TPoint; beginwith TDirect2DCanvas.Create(PaintBox1.Canvas, PaintBox1.ClientRect) dobeginBeginDraw;Pen.Color := ColorBox1.Selected;Pen.Width := StrToIntDef(Edit1.Text, 1);Brush.Color := ColorBox2.Selected;if not CheckBox1.Checked then Pen.Width := 0;if not CheckBox2.Checked then Brush.Style := bsClear;{繪制圖形}pts := ppts^;case enum ofeEllipse : Ellipse(Rect(pts[0], pts[1]));eLine : begin MoveTo(pts[0].X, pts[0].Y); LineTo(pts[1].X, pts[1].Y); end;eRectangle : Rectangle(Rect(pts[0], pts[1]));eRoundRect : RoundRect(Rect(pts[0], pts[1]), 25, 25);eFrameRect : FrameRect(Rect(pts[0], pts[1]));ePie : Pie(pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, pts[2].X, pts[2].Y, pts[3].X, pts[3].Y);eArc : Arc(pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, pts[2].X, pts[2].Y, pts[3].X, pts[3].Y);eChord : Chord(pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, pts[2].X, pts[2].Y, pts[3].X, pts[3].Y);ePolyLine : Polyline(pts);ePolygon : Polygon(pts);ePolyBezier: PolyBezier(pts);end;{繪制控制點}Brush.Style := bsSolid;Brush.Color := clRed;for pt in pts do if pt.X <> MaxInt then FillRect(Rect(pt.X-4, pt.Y-4, pt.X+4, pt.Y+4));EndDraw;Free;end; end;end.
窗體:
object Form1: TForm1Left = 0Top = 0Caption = 'Form1'ClientHeight = 348ClientWidth = 476Color = clBtnFaceFont.Charset = DEFAULT_CHARSETFont.Color = clWindowTextFont.Height = -11Font.Name = 'Tahoma'Font.Style = []OldCreateOrder = FalseOnCreate = FormCreatePixelsPerInch = 96TextHeight = 13object ListBox1: TListBoxLeft = 0Top = 0Width = 129Height = 348Align = alLeftItemHeight = 13TabOrder = 0OnClick = ListBox1Clickendobject Panel1: TPanelLeft = 129Top = 0Width = 347Height = 348Align = alClientCaption = 'Panel1'Padding.Top = 10TabOrder = 1object PaintBox1: TPaintBoxLeft = 1Top = 129Width = 345Height = 218Align = alClientOnMouseDown = PaintBox1MouseDownOnMouseMove = PaintBox1MouseMoveOnMouseUp = PaintBox1MouseUpOnPaint = PaintBox1PaintExplicitLeft = 208ExplicitTop = 136ExplicitWidth = 105ExplicitHeight = 105endobject GroupBox1: TGroupBoxLeft = 1Top = 11Width = 345Height = 62Align = alTopCaption = 'Pen'TabOrder = 0object Edit1: TEditLeft = 256Top = 24Width = 48Height = 21TabOrder = 0Text = 'Edit1'endobject UpDown1: TUpDownLeft = 303Top = 22Width = 17Height = 23TabOrder = 1endobject ColorBox1: TColorBoxLeft = 112Top = 24Width = 114Height = 22TabOrder = 2endobject CheckBox1: TCheckBoxLeft = 16Top = 26Width = 97Height = 17Caption = 'CheckBox1'TabOrder = 3endendobject GroupBox2: TGroupBoxLeft = 1Top = 73Width = 345Height = 56Align = alTopCaption = 'Brush'TabOrder = 1object ColorBox2: TColorBoxLeft = 112Top = 22Width = 114Height = 22TabOrder = 0endobject CheckBox2: TCheckBoxLeft = 16Top = 24Width = 97Height = 17Caption = 'CheckBox2'TabOrder = 1endendend end
效果圖:
轉(zhuǎn)載于:https://my.oschina.net/hermer/blog/320037
總結(jié)
以上是生活随笔為你收集整理的Direct2D (2) : 基本图形命令测试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VMware Workstation中L
- 下一篇: 常用 API 函数(3): 文件处理函数