首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

再學GDI+[62]: 路徑漸層筆刷中心點控制 - SetCenterPoint、GetCenterPoint 滑鼠互動拖曳

     編輯:Delphi

前言

路徑漸層筆刷的中心點決定漸層輻射的原點位置。SetCenterPoint 允許將中心點移動至任意座標(甚至路徑外部),配合滑鼠拖曳可實現直觀的漸層中心控制介面。

中心點控制機制

  • 預設位置:路徑幾何中心(外接矩形中心)
  • SetCenterPoint:接受 TGPPointF,座標為世界座標系
  • GetCenterPoint:讀取目前中心點座標
  • 超出路徑:中心點可在路徑外,產生偏心漸層、切線光效果

滑鼠拖曳互動實作

按下滑鼠記錄位置 → 移動時更新中心點 → 釋放停止更新,全程即時重繪。

var
  rect: TGPRect;
  pt: TGPPoint;      {目前中心點}
  pb: TGPPathGradientBrush;

procedure TForm1.FormCreate(Sender: TObject);
var
  path: TGPGraphicsPath;
  ColorSurround: TGPColor;
  num: Integer;
begin
  rect := MakeRect(10, 10, ClientWidth-20, ClientHeight-20);
  path := TGPGraphicsPath.Create;
  path.AddEllipse(rect);
  pb := TGPPathGradientBrush.Create(path);
  pb.SetCenterColor(aclLime);
  pb.GetCenterPoint(pt);  {取得預設中心點}
  ColorSurround := aclBlack;
  num := 1;
  pb.SetSurroundColors(@ColorSurround, num);
  path.Free;
  DoubleBuffered := True;
end;

procedure TForm1.FormMouseDown(Sender: TObject; ...);
begin
  pt.X := X; pt.Y := Y; Repaint;
end;

procedure TForm1.FormPaint(Sender: TObject);
var
  g: TGPGraphics;
  p: TGPPen;
begin
  g := TGPGraphics.Create(Canvas.Handle);
  p := TGPPen.Create(aclRed);
  g.FillEllipse(pb, rect);
  pb.SetCenterPoint(pt);  {套用新中心點}
  g.DrawRectangle(p, pt.X-3, pt.Y-3, 6, 6);  {標示中心點位置}
  p.Free; g.Free;
end;

執行效果預覽

中心點拖曳效果

視覺效果分析

中心點位置視覺效果應用場景
幾何中心對稱輻射漸層標準按鈕、面板背景
偏向邊緣單側亮、對側暗按鈕按下狀態、3D 凸起感
路徑外部類似方向光照射日光、舞台燈光模擬
動態移動跟隨滑鼠/焦點互動式 UI 回饋、遊戲光標

表單定義檔

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 164
  ClientWidth = 208
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesktopCenter
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  OnMouseDown = FormMouseDown
  OnMouseUp = FormMouseUp
  OnPaint = FormPaint
  PixelsPerInch = 96
  TextHeight = 13
end
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved