Delphi 设备描述表 ;TCanvas...
设备描述表与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 返回文本参数中输入的字符串像素的宽度和高
补充:软件开发 , Delphi ,