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

DelphiX 實戰:TDIB.FilterLine 與 FilterRect 程序化繪圖濾鏡

     編輯:Delphi

TDIB.FilterLine 與 FilterRect 以指定顏色與濾鏡模式繪製線段與矩形,支援 4 種混合模式,適合程序化 UI 繪製、網格線、選取框等。本文提供循環切換 4 種模式的展示範例。

效果預覽

FilterLine/FilterRect 效果

核心程式碼

unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DIB, StdCtrls;
type
  TForm1 = class(TForm)
    DXPaintBox1: TDXPaintBox;
    Button1: TButton; // 循環切換濾鏡模式
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  end;
var Form1: TForm1;
implementation {$R *.dfm}
uses TypInfo;
var dib: TDIB; f: Integer;

procedure TForm1.Button1Click(Sender: TObject);
const clr = clRed;
var w, h, a, b: Integer;
begin
  DXPaintBox1.DIB.Assign(dib); // 重置原圖
  w := DXPaintBox1.ClientWidth; h := DXPaintBox1.ClientHeight;
  a := w div 7; b := h div 7;
  // 繪製十字網格線
  DXPaintBox1.DIB.FilterLine(0, b, w, b, clr, TFilterMode(f));
  DXPaintBox1.DIB.FilterLine(0, h-b, w, h-b, clr, TFilterMode(f));
  DXPaintBox1.DIB.FilterLine(a, 0, a, h, clr, TFilterMode(f));
  DXPaintBox1.DIB.FilterLine(w-a, 0, w-a, h, clr, TFilterMode(f));
  // 繪製中心矩形
  DXPaintBox1.DIB.FilterRect(w div 5, h div 5, w div 5*3, h div 5*3, clr, TFilterMode(f));
  DXPaintBox1.Repaint;
  // 顯示目前模式名稱
  Caption := GetEnumName(TypeInfo(TFilterMode), f);
  Inc(f); f := f and 3; // 0~3 循環
end;

procedure TForm1.FormCreate(Sender: TObject);
const ImgPath1 = 'C:\Temp\DX.jpg';
begin
  dib := TDIB.Create;
  dib.LoadFromFile(ImgPath1);
  DXPaintBox1.DIB.Assign(dib);
  DXPaintBox1.Width := dib.Width;
  DXPaintBox1.Height := dib.Height;
  DXPaintBox1.Repaint;
end;

procedure TForm1.FormDestroy(Sender: TObject); begin dib.Free; end;
end.

濾鏡模式 TFilterMode 對照

值常數效果
0fmNormal正常覆蓋
1fmMix7575% 混合
2fmMix5050% 混合
3fmMix2525% 混合

方法簽名

FilterLine(X1, Y1, X2, Y2: Integer; Color: TColor; Mode: TFilterMode);
FilterRect(Left, Top, Right, Bottom: Integer; Color: TColor; Mode: TFilterMode);

應用場景

  • **程序化 UI**:不依賴圖片資源,直接用程式碼繪製按鈕邊框、面板背景
  • **選取框/網格**:即時繪製半透明選取區域、對齊網格
  • **除錯視覺化**:碰撞箱、路徑點、視野範圍即時疊加顯示
  • **特效疊加**:掃描線、掃光、能量場邊緣等線條特效

優缺點分析

  • ✅ 無需準備圖片素材,程式碼極簡
  • ✅ 參數化控制,易於動畫化(顏色、粗細、透明度漸變)
  • ❌ 僅支援直線與軸對齊矩形,曲線/圓角需自行實作
  • ❌ CPU 繪製,大量呼叫影響效能
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved