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

Delphi 設備描述表 ;TCanvas...

編輯:Delphi

設備描述表與TCanvas(Device Contexts and TCanvas)
Windows使用術語設備描述表(device context,後面簡稱為DC)描述用戶可在其上畫圖的畫布。DC可用來在許多表面上畫圖:

窗口客戶區或框架(To a window's client area or frame)
桌面(To the desktop)
內存(To memory)
打印機或其他輸出設備(To a printer or other output device)
這些僅僅是一些列子,還有許多其他的不太為人所知的DC(如菜單),但是以上列舉都是用戶非常感興趣的DC。

在API級別處理DC要復雜些。首先必須從Windows中的到DC處理程序。然後選擇各種對象進入DC中(筆pen、刷子brush、字體font),然後便可在DC中畫圖,畫圖完畢後,必須確保進入DC的各種對象在清除之後再刪除DC。如果忘記清除DC中的各種對象,應用程序將使內存溢出。

幸運的是VCL提供有TCanvas類以便較容易地處理DC。舉一個簡單的例子,下面的程序使用Windows API在屏幕上繪制圓,內圓為紅色,邊框為藍色。

 

[html]
procedure TForm1.Button1Click(Sender: TObject);  
var 
  DC: HDC; 
  Brush, OldBrush: HBRUSH; 
  Pen, OldPen: HPEN; 
begin 
  DC := GetDC(Handle); 
  Brush := CreateSolidBrush(RGB(255, 0, 0)); 
  Pen := CreatePen(PS_SOLID, 1, RGB(0, 0, 255)); 
  OldBrush := SelectObject(DC, Brush); 
  OldPen := SelectObject(DC, Pen); 
  Ellipse(DC, 20, 20, 120, 120); 
  SelectObject(DC, OldBrush); 
  SelectObject(dc, OldPen); 
  ReleaseDC(Handle, DC); 
end; 

procedure TForm1.Button1Click(Sender: TObject);
var
  DC: HDC;
  Brush, OldBrush: HBRUSH;
  Pen, OldPen: HPEN;
begin
  DC := GetDC(Handle);
  Brush := CreateSolidBrush(RGB(255, 0, 0));
  Pen := CreatePen(PS_SOLID, 1, RGB(0, 0, 255));
  OldBrush := SelectObject(DC, Brush);
  OldPen := SelectObject(DC, Pen);
  Ellipse(DC, 20, 20, 120, 120);
  SelectObject(DC, OldBrush);
  SelectObject(dc, OldPen);
  ReleaseDC(Handle, DC);
end;

以上代碼是不是太差,但是當處理完DC之後,使人容易忘記恢復對象,如果忘記了,其應用程序將會資源洩露。下面的代碼與上面代碼等價,但是用VCL寫的:

 

[delphi]
procedure TForm1.Button2Click(Sender: TObject);  
begin  
  Canvas.Brush.Color := clRed;  
  Canvas.Pen.Color := clBlue;  
  Canvas.Ellipse(20, 20, 120, 120);  
end;  

procedure TForm1.Button2Click(Sender: TObject);
begin
  Canvas.Brush.Color := clRed;
  Canvas.Pen.Color := clBlue;
  Canvas.Ellipse(20, 20, 120, 120);
end;

 

該代碼不但短小易讀,而且也很健壯。

TCanvas根據需要負責釋放資源,因此不必擔心資源問題。

TCanvas是一種比使用API更簡單有效的方法。

TCanvas繼承關系如下:

 

TCanvas類有許多屬性和方法。之後的講解中將會使用一些屬性和方法,下面的表格中列出了TCanvas主要的屬性和方法。

表1 TCanvas主要屬性
屬性 描述
Brush 刷子顏色或圖案,用於填圖。(Determines the color and pattern for filling graphical shapes and backgrounds..)

Delphi syntax: property Brush: TBrush;
 
ClipRect 畫布的當前剪貼矩形,任何圖畫均被限制在該矩形中。該屬性為只讀屬性。(Specifies the boundaries of the clipping rectangle.)

Delphi syntax: property ClipRect: TRect;
 
CopyMode 決定圖畫如何表現(正圖、反圖等)(Specifies how a graphical image is copied onto the canvas. )

Delphi syntax: property CopyMode: TCopyMode;
 
Font 為繪制文本畫布使用的字體(Specifies the font to use when writing text on the image.)

Delphi syntax: property Font: TFont;
 
Handle 畫布的處理程序,用於直接訪問Windows API的時候使用。(Specifies the handle for this canvas.)

Delphi syntax: property Handle: HDC;
 
Pen 決定畫布上所畫線條的類型和顏色。(Specifies the kind of pen the canvas uses for drawing lines and outlining shapes.)

Delphi syntax: property Pen: TPen;
 
PenPos 在X,Y坐標系中的當前圖畫位置。(Specifies the current drawing position of the Pen. )

Delphi syntax: property PenPos: TPoint;
 
Pixels 數組畫布的像素(Specifies the color of the pixels within the current ClipRect.)

Delphi syntax: property Pixels[X, Y: Integer]: TColor;
 

 

表2 TCanvas主要方法
方法 描述
Arc 利用當前筆在畫布上畫弧。Draws an arc on the image along the perimeter of the ellipse bounded by the specified rectangle.
Delphi syntax: procedure Arc(X1, Y1, X2, Y2, X3, Y3, X4, Y4: Integer);
 
BrushCopy 顯示具有透明背景的位圖。Copies a portion of a bitmap onto a rectangle on the canvas, replacing one of the colors of the bitmap with the brush of the canvas.
Delphi syntax: procedure BrushCopy(const Dest: TRect; Bitmap: TBitmap; const Source: TRect; Color: TColor);
 
CopyRect 拷貝圖像某一部分到畫布上。Copies part of an image from another canvas into the canvas.
Delphi syntax: procedure CopyRect(const Dest: TRect; Canvas: TCanvas; const Source: TRect);
 
Draw 把圖像從內存中拷貝到畫布上。Renders the graphic specified by the Graphic parameter on the canvas at the location given by the coordinates (X, Y).
Delphi syntax: procedure Draw(X, Y: Integer; Graphic: TGraphic);
 
Ellipse 利用當前畫筆畫橢圓並在畫布上以當前刷子填滿。Draws the ellipse defined by a bounding rectangle on the canvas.
Delphi syntax:

procedure Ellipse(X1, Y1, X2, Y2: Integer); overload;
procedure Ellipse(const Rect: TRect); overload;
 
FloodFill 用當前刷子填充畫布的一個區域。Fills an area of the canvas using the current brush.
Delphi syntax: procedure FloodFill(X, Y: Integer; Color: TColor; FillStyle: TFillStyle);
 
LineTo 從當前圖畫位置到X,Y位置參數指定的位置畫線。Draws a line on the canvas from PenPos to the point specified by X and Y, and sets the pen position to (X, Y).
Delphi syntax: procedure LineTo(X, Y: Integer);
 
MoveTo 設置當前圖畫位置。Changes the current drawing position to the point (X,Y).
Delphi syntax: procedure MoveTo(X, Y: Integer);
 
Pie 在畫布上畫圓餅形。Draws a pie-shaped the section of the ellipse bounded by the rectangle (X1, Y1) and (X2, Y2) on the canvas.
Delphi syntax: procedure Pie(X1, Y1, X2, Y2, X3, Y3, X4, Y4: Integer);
 
Polygon 根據一組點畫菱形於畫布上,並以當前刷子填充。Draws a series of lines on the canvas connecting the points passed in and closing the shape by drawing a line from the last point to the first point.
Delphi syntax: procedure Polygon(Points: array of TPoint);
 
PolyLine 使用當前筆,根據一組點在畫布上畫線,這些點不是自動封閉的。Draws a series of lines on the canvas with the current pen, connecting each of the points passed to it in Points.
Delphi syntax: procedure Polyline(Points: array of TPoint);
 
Rectangle 畫矩形於畫布上,周圍使用當前筆,矩形內使用當前刷子填充。Draws a rectangle on the canvas.
Delphi syntax:

procedure Rectangle(X1, Y1, X2, Y2: Integer); overload;
procedure Rectangle(const Rect: TRect); overload;
 
RoundRect 圓角矩形。Draws a rectangle with rounded corners on the canvas.
Delphi syntax: procedure RoundRect(X1, Y1, X2, Y2, X3, Y3: Integer);
 
StretchDraw 將位圖從內存中拷貝到畫布上,位圖根據目標矩形大小擴大或縮小。Draws the graphic specified by the Graphic parameter in the rectangle specified by the Rect parameter.
Delphi syntax: procedure StretchDraw(const Rect: TRect; Graphic: TGraphic);
 
TextExtent 返回文本參數中輸入的字符串像素的寬度和高度,利用當前畫布字體計算其寬度。Returns the width and height, in pixels, of a string rendered in the current font.
Delphi syntax: function TextExtent(const Text: string): TSize;
 
TextHeight 返回文本參數中輸入的字符串像素的高度,其寬度利用當前畫布字體計算。Returns the height, in pixels, of a string rendered in the current font.
Delphi syntax: function TextHeight(const Text: string): Integer;
 
TextOut 利用當前字體在畫布指定位置上繪制文本。Writes a string on the canvas, starting at the point (X,Y), and then updates the PenPos to the end of the string.
Delphi syntax: procedure TextOut(X, Y: Integer; const Text: string);
 
TextRect 在剪貼矩形框中繪制文本。Writes a string inside a clipping rectangle.
Delphi syntax: procedure TextRect(Rect: TRect; X, Y: Integer; const Text: string);
 

信不信由你,這些屬性和方法僅僅只是體現了Windows設備描述環境的一小部分功能。好在這些屬性和方法覆蓋了處理圖像時需要完成工作的80%。然後,在詳細論述TCanvas類之前,需要論述用於Windows程序設計中的圖形對象。

 

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