程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> GdiPlus[2]: 獲取繪圖表面(Graphics)

GdiPlus[2]: 獲取繪圖表面(Graphics)

編輯:Delphi

 繪圖表面(Graphics, 這在 VCL 體系中叫 Canvas), 在 GdiPlus 中有四種獲取方法:

  1、通過窗口句柄獲取;

  2、通過窗口的 Canvas.Handle 獲取;

  3、通過 GdiPlus 利用 Helper 技術給部分 VCL 對象添加的 ToGPGraphics 方法獲取;

  4、通過圖像對象獲取.

  本例效果圖:

GdiPlus[2]: 獲取繪圖表面(Graphics)

unit Unit1; 
 
interface 
 
uses 
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
 Dialogs, StdCtrls; 
 
type 
 TForm1 = class(TForm) 
  Button1: TButton; 
  Button2: TButton; 
  Button3: TButton; 
  Button4: TButton; 
  procedure Button1Click(Sender: TObject); 
  procedure Button2Click(Sender: TObject); 
  procedure Button3Click(Sender: TObject); 
  procedure Button4Click(Sender: TObject); 
 end; 
 
var 
 Form1: TForm1; 
 
implementation 
 
{$R *.dfm}  
 
uses GdiPlus, GdiPlusHelpers; 
 
//從窗口句柄獲取 IGPGraphics 
procedure TForm1.Button1Click(Sender: TObject); 
var 
 Graphics: IGPGraphics; 
 Pen: IGPPen; 
begin 
 Graphics := TGPGraphics.Create(Handle); 
 Pen := TGPPen.Create(TGPColor.Red, 2); 
 Graphics.DrawEllipse(Pen, 20, 10, 150, 40); 
end; 
 
//從 HDC 獲取 IGPGraphics 
procedure TForm1.Button2Click(Sender: TObject); 
var 
 Graphics: IGPGraphics; 
 Pen: IGPPen; 
begin 
 Graphics := TGPGraphics.Create(Canvas.Handle); 
 Pen := TGPPen.Create(TGPColor.Green, 2); 
 Graphics.DrawEllipse(Pen, 20, 40, 150, 40); 
end; 
 
//使用 GdiPlusHelpers 為 Canvas 添加的 ToGPGraphics 方法獲取 IGPGraphics 
procedure TForm1.Button3Click(Sender: TObject); 
var 
 Graphics: IGPGraphics; 
 Pen: IGPPen; 
begin 
 Graphics := Canvas.ToGPGraphics; 
 Pen := TGPPen.Create(TGPColor.Blue, 2); 
 Graphics.DrawEllipse(Pen, 20, 70, 150, 40); 
end; 
 
//從圖像建立 IGPGraphics 
procedure TForm1.Button4Click(Sender: TObject); 
var 
 GraphicsImg: IGPGraphics; 
 Pen: IGPPen; 
 Image: IGPImage; 
begin 
 Image := TGPBitmap.Create(152, 42); 
 GraphicsImg := TGPGraphics.Create(Image); 
 Pen := TGPPen.Create(TGPColor.Fuchsia, 2); 
 GraphicsImg.DrawEllipse(Pen, 0, 0, Image.Width-2, Image.Height-2); 
 
 Canvas.ToGPGraphics.DrawImage(Image, 20, 100); 
end; 
 
end. 


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