程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> WinAPI: FlattenPath、WidenPath

WinAPI: FlattenPath、WidenPath

編輯:Delphi

 不管什麼曲線命令, 到來路徑中都會變成 Bezier 線; 也就是說路徑中只有直線和 BezIEr 線.

  FlattenPath 和 WidenPath 都能夠把路徑中的 BezIEr 線轉換為近似的直線; 不同的是: 用 WidenPath 轉換後貌似加寬了線, 其實它是轉換成了一個包圍路徑的新路徑(類似區域).

  本例效果圖:

WinAPI: FlattenPath、WidenPath

  代碼文件:

unit Unit1; 
 
interface 
 
uses 
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
 Dialogs, StdCtrls, ExtCtrls; 
 
type 
 TForm1 = class(TForm) 
  RadioGroup1: TRadioGroup; 
  procedure FormPaint(Sender: TObject); 
  procedure FormCreate(Sender: TObject); 
  procedure RadioGroup1Click(Sender: TObject); 
 end; 
 
var 
 Form1: TForm1; 
 
implementation 
 
{$R *.dfm} 
 
procedure TForm1.FormCreate(Sender: TObject); 
begin 
 RadioGroup1.Items.CommaText := 'Path,FlattenPath,WidenPath'; 
 RadioGroup1.ItemIndex := 0; 
end; 
 
procedure TForm1.FormPaint(Sender: TObject); 
type 
 TPArr = array[0..0] of TPoint; 
 TTArr = array[0..0] of Byte; 
var 
 pts: ^TPArr; 
 types: ^TTArr; 
 count: Integer; 
 i,x,y: Integer; 
begin 
 Canvas.Font.Size := 150; 
 Canvas.Font.Style := [fsBold]; 
 SetBkMode(Canvas.Handle, TRANSPARENT); 
 
 BeginPath(Canvas.Handle); 
 Canvas.TextOut(50, 0, 'D'); 
 Canvas.Arc(20, 20, 220, 220, 120, 120, 20, 120); 
 EndPath(Canvas.Handle); 
 
 Canvas.Pen.Width := 6; 
 
 if RadioGroup1.ItemIndex = 1 then FlattenPath(Canvas.Handle); 
 if RadioGroup1.ItemIndex = 2 then WidenPath(Canvas.Handle); 
 
 Canvas.Pen.Color := clWhite; 
 count := GetPath(Canvas.Handle, pts^, types^, 0); 
 
 GetMem(pts, count*SizeOf(TPoint)); 
 GetMem(types, count); 
 
 count := GetPath(Canvas.Handle, pts^, types^, count); 
 Text := '路徑中點的總數是: ' + IntToStr(count); 
 
 StrokePath(Canvas.Handle); 
 
 Canvas.Brush.Color := clRed; 
 for i := 0 to count - 1 do 
 begin 
  x := pts^[i].X; 
  y := pts^[i].Y; 
  Canvas.FillRect(Rect(x-1,y-1,x+1,y+1)); 
 end; 
 
 FreeMem(pts); 
 FreeMem(types); 
end; 
 
procedure TForm1.RadioGroup1Click(Sender: TObject); 
begin 
 Repaint; 
end; 
 
end. 

  窗體文件:

object Form1: TForm1 
 Left = 352 
 Top = 227 
 Caption = 'Form1' 
 ClIEntHeight = 215 
 ClIEntWidth = 339 
 Color = clBtnFace 
 Font.Charset = DEFAULT_CHARSET 
 Font.Color = clWindowText 
 Font.Height = -11 
 Font.Name = 'Tahoma' 
 Font.Style = [] 
 OldCreateOrder = False 
 Position = poDesigned 
 OnCreate = FormCreate 
 OnPaint = FormPaint 
 PixelsPerInch = 96 
 TextHeight = 13 
 object RadioGroup1: TRadioGroup 
  Left = 240 
  Top = 80 
  Width = 91 
  Height = 127 
  Caption = 'RadioGroup1' 
  TabOrder = 0 
  OnClick = RadioGroup1Click 
 end 
end 

  關於描繪路徑中的點, 參見: WinAPI: GetPath - 獲取路徑中的點


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