Delphi 的绘图功能.docx
- 文档编号:4309166
- 上传时间:2022-11-29
- 格式:DOCX
- 页数:50
- 大小:300.36KB
Delphi 的绘图功能.docx
《Delphi 的绘图功能.docx》由会员分享,可在线阅读,更多相关《Delphi 的绘图功能.docx(50页珍藏版)》请在冰豆网上搜索。
Delphi的绘图功能
Graphics单元中的类
//Graphics单元中的类
TGraphicsObject
TFont
TPen
TBrush
TFontRecall
TPenRecall
TBrushRecall
TCanvas
TGraphic
TPicture
TMetafileCanvas
TMetafileImage
TMetafile
TBitmapImage
TBitmap
TIconImage
TIcon
TResourceManager
TBrushResourceManager
TFileFormatsList
Delphi的绘图功能[1]-TCanvas的类成员
//过程:
Arc
BrushCopy
Chord
CopyRect
Draw
DrawFocusRect
Ellipse
Ellipse
FillRect
FloodFill
FrameRect
LineTo
Lock
MoveTo
Pie
Polygon
Polyline
PolyBezier
PolyBezierTo
Rectangle
Rectangle
Refresh
RoundRect
StretchDraw
TextOut
TextRect
TextRect
Unlock
//函数:
HandleAllocated
TextExtent
TextHeight
TextWidth
TryLock
//属性:
ClipRect
Handle
LockCount
CanvasOrientation
PenPos
Pixels
TextFlags
Brush
CopyMode
Font
Pen
//事件
OnChange
OnChanging
Delphi的绘图功能[2]-入门
绘图需要有纸、画笔、画刷;Delphi有Canvas、Pen、Brush.
Canvas就是画布,譬如窗体的Canvas属性,就是窗体的画布;
Pen是画笔,可以设置笔色、笔宽等等;
Brush是画刷,可以设置颜色等等.
//举例:
{绘制直线}
procedureTForm1.Button1Click(Sender:
TObject);
begin
Canvas.Pen.Color:
=clRed;{设置画笔颜色}
Canvas.Pen.Width:
=2;{设置画笔宽度}
Canvas.MoveTo(10,10);{直线起点}
Canvas.LineTo(100,100);{直线终点}
{这里的Canvas是简写,也可以写做:
Self.Canvas或Form1.Canvas}
end;
{矩形填充}
procedureTForm1.Button2Click(Sender:
TObject);
begin
Canvas.Brush.Color:
=clYellow;{设置画刷颜色,也就是填充色}
Canvas.FillRect(ClientRect);{填充窗体客户区}
end;
//效果图:
Delphi的绘图功能[3]-矩形类图形
unitUnit1;
interface
uses
Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
Dialogs,StdCtrls;
type
TForm1=class(TForm)
Button1:
TButton;
Button2:
TButton;
Button3:
TButton;
Button4:
TButton;
Button5:
TButton;
procedureFormCreate(Sender:
TObject);
procedureButton1Click(Sender:
TObject);
procedureButton2Click(Sender:
TObject);
procedureButton3Click(Sender:
TObject);
procedureButton4Click(Sender:
TObject);
procedureButton5Click(Sender:
TObject);
end;
var
Form1:
TForm1;
implementation
{$R*.dfm}
var
btn:
TButton;
const
y1=10;
y2=80;
{先设置画笔画刷}
procedureTForm1.FormCreate(Sender:
TObject);
begin
Canvas.Pen.Width:
=4;
Canvas.Pen.Color:
=clRed;
Canvas.Brush.Color:
=clWhite;
end;
{给出左上角和右下角的坐标就可以绘制矩形}
procedureTForm1.Button1Click(Sender:
TObject);
begin
btn:
=TButton(Sender);
Canvas.Rectangle(btn.Left,y1,btn.Left+btn.Width,y2);
end;
{给出一个矩形结构做参数也可以绘制矩形}
procedureTForm1.Button2Click(Sender:
TObject);
var
R:
TRect;{定义一个矩形结构}
begin
btn:
=TButton(Sender);
R:
=Rect(btn.Left,y1,btn.Left+btn.Width,y2);
Canvas.Rectangle(R);
end;
{绘制圆角矩形;最后两个参数是设置圆角的}
procedureTForm1.Button3Click(Sender:
TObject);
begin
btn:
=TButton(Sender);
Canvas.RoundRect(btn.Left,y1,btn.Left+btn.Width,y2,20,20);
end;
{绘制焦点矩形;它不受画笔的影响;它的颜色是和画刷的颜色进行某种运算得来的}
procedureTForm1.Button4Click(Sender:
TObject);
var
R:
TRect;
begin
btn:
=TButton(Sender);
R:
=Rect(btn.Left,y1,btn.Left+btn.Width,y2);
Canvas.DrawFocusRect(R);
end;
{矩形描边,用的不是画笔是画刷}
procedureTForm1.Button5Click(Sender:
TObject);
var
R:
TRect;
begin
btn:
=TButton(Sender);
R:
=Rect(btn.Left,y1,btn.Left+btn.Width,y2);
Canvas.FrameRect(R);
end;
end.
//效果图:
Delphi的绘图功能[4]-圆弧类图形
unitUnit1;
interface
uses
Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
Dialogs,StdCtrls;
type
TForm1=class(TForm)
Button1:
TButton;
Button2:
TButton;
Button3:
TButton;
Button4:
TButton;
Button5:
TButton;
procedureFormCreate(Sender:
TObject);
procedureButton1Click(Sender:
TObject);
procedureButton2Click(Sender:
TObject);
procedureButton3Click(Sender:
TObject);
procedureButton4Click(Sender:
TObject);
procedureButton5Click(Sender:
TObject);
end;
var
Form1:
TForm1;
implementation
{$R*.dfm}
var
btn:
TButton;
const
y1=10;
y2=80;
{先设置画笔画刷}
procedureTForm1.FormCreate(Sender:
TObject);
begin
Canvas.Pen.Width:
=2;
Canvas.Pen.Color:
=clRed;
Canvas.Brush.Color:
=clWhite;
end;
{绘制椭圆;椭圆的参数和矩形是一样的}
procedureTForm1.Button1Click(Sender:
TObject);
begin
btn:
=TButton(Sender);
Canvas.Ellipse(btn.Left,y1,btn.Left+btn.Width,y2);
end;
{根据矩形结构绘制椭圆}
procedureTForm1.Button2Click(Sender:
TObject);
var
R:
TRect;{定义一个矩形结构}
begin
btn:
=TButton(Sender);
R:
=Rect(btn.Left,y1,btn.Left+btn.Width,y2);
Canvas.Ellipse(R);
end;
{绘制扇形}
procedureTForm1.Button3Click(Sender:
TObject);
begin
btn:
=TButton(Sender);
Canvas.Pie(
btn.Left,y1,{矩形区域左上角}
btn.Left+btn.Width,y2,{矩形区域右下角}
btn.Left+btn.Width,y1+(y2-y1)div2,{扇形起点}
btn.Left+btn.Widthdiv2,y1{扇形终点}
);
end;
{绘制弦,参数同上}
procedureTForm1.Button4Click(Sender:
TObject);
begin
btn:
=TButton(Sender);
Canvas.Chord(
btn.Left,y1,
btn.Left+btn.Width,y2,
btn.Left+btn.Width,y1+(y2-y1)div2,
btn.Left+btn.Widthdiv2,y1
);
end;
{绘制弧线,参数同上}
procedureTForm1.Button5Click(Sender:
TObject);
begin
btn:
=TButton(Sender);
Canvas.Arc(
btn.Left,y1,
btn.Left+btn.Width,y2,
btn.Left+btn.Width,y1+(y2-y1)div2,
btn.Left+btn.Widthdiv2,y1
);
end;
end.
//效果图:
Delphi的绘图功能[5]-获取Canvas对象
在窗体上添加Panel1、PaintBox1和Button1,代码如下:
procedureTForm1.Button1Click(Sender:
TObject);
var
cvs:
TCanvas;
begin
{PaintBox1就像窗体一样,也有Canvas属性}
PaintBox1.Canvas.Brush.Color:
=clRed;
PaintBox1.Canvas.FillRect(PaintBox1.ClientRect);
{像Panel1这样没有Canvas属性的控件,也可以绕道获取}
cvs:
=TCanvas.Create;
cvs.Handle:
=GetDC(Panel1.Handle);
cvs.Brush.Color:
=clBlue;
cvs.FillRect(Panel1.ClientRect);
cvs.Free;
end;
//执行前后对比图:
Delphi的绘图功能[6]-Polygon、Polyline
unitUnit1;
interface
uses
Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
Dialogs,StdCtrls;
type
TForm1=class(TForm)
Button1:
TButton;
Button2:
TButton;
procedureFormCreate(Sender:
TObject);
procedureButton1Click(Sender:
TObject);
procedureButton2Click(Sender:
TObject);
end;
var
Form1:
TForm1;
implementation
{$R*.dfm}
procedureTForm1.FormCreate(Sender:
TObject);
begin
Canvas.Pen.Width:
=2;
Canvas.Pen.Color:
=clRed;
Canvas.Brush.Color:
=clYellow;
end;
{绘制多边形;它的参数是一个点数组,这里定义了一个常数数组}
procedureTForm1.Button1Click(Sender:
TObject);
const
pts:
array[0..3]ofTPoint=(
(x:
10;y:
40),
(x:
46;y:
120),
(x:
82;y:
40),
(x:
46;y:
10)
);
begin
Canvas.Polygon(pts);
end;
{绘制连续的一组直线;它的参数也是一个点数组,我只是把上面的平移了一点}
procedureTForm1.Button2Click(Sender:
TObject);
const
pts:
array[0..3]ofTPoint=(
(x:
10+82;y:
40),
(x:
46+82;y:
120),
(x:
82+82;y:
40),
(x:
46+82;y:
10)
);
begin
Canvas.Polyline(pts);
end;
end.
//效果图:
Delphi的绘图功能[7]-PolyBezier、PolyBezierTo
unitUnit1;
interface
uses
Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
Dialogs,StdCtrls;
type
TForm1=class(TForm)
Button1:
TButton;
Button2:
TButton;
procedureButton1Click(Sender:
TObject);
procedureButton2Click(Sender:
TObject);
end;
var
Form1:
TForm1;
implementation
{$R*.dfm}
{PolyBezier至少需要四个点做参数;不改变画笔的当前位置}
procedureTForm1.Button1Click(Sender:
TObject);
var
pts:
array[0..3]ofTPoint;
begin
Canvas.Pen.Width:
=2;
Canvas.Pen.Color:
=clRed;
pts[0].X:
=10;pts[0].Y:
=10;{起点}
pts[1].X:
=60;pts[1].Y:
=10;{控制点1}
pts[2].X:
=10;pts[2].Y:
=100;{控制点2}
pts[3].X:
=60;pts[3].Y:
=100;{终点}
Canvas.PolyBezier(pts);
Canvas.Pen.Width:
=1;
Canvas.Pen.Color:
=clWhite;
Canvas.LineTo(ClientWidth,ClientHeight);
end;
{PolyBezierTo最少只需要需要三个点,它把当前位置当作第一点;会改变画笔的当前位置}
procedureTForm1.Button2Click(Sender:
TObject);
var
pts:
array[1..3]ofTPoint;{从1开始的,就3个元素}
begin
Canvas.Pen.Width:
=2;
Canvas.Pen.Color:
=clRed;
Canvas.MoveTo(10+82,10);{起点}
//pts[0].X:
=10;pts[0].Y:
=10;
pts[1].X:
=60+82;pts[1].Y:
=10;{控制点1}
pts[2].X:
=10+82;pts[2].Y:
=100;{控制点2}
pts[3].X:
=60+82;pts[3].Y:
=100;{终点}
Canvas.PolyBezierTo(pts);
Canvas.Pen.Width:
=1;
Canvas.Pen.Color:
=clBlue;
Canvas.LineTo(ClientWidth,ClientHeight);
end;
end.
//效果图:
//绘制多条贝塞尔线时,每条线的终点也是下条线的起点.
unitUnit1;
interface
uses
Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
Dialogs,StdCtrls;
type
TForm1=class(TForm)
Button1:
TButton;
Button2:
TButton;
procedureButton1Click(Sender:
TObject);
procedureButton2Click(Sender:
TObject);
end;
var
Form1:
TForm1;
implementation
{$R*.dfm}
{PolyBezier绘制多条线时,需要3*x+1个点}
procedureTForm1.Button1Click(Sender:
TObject);
var
pts:
array[0..6]ofTPoint;
begin
Canvas.Pen.Width:
=2;
Canvas.Pen.Color:
=clRed;
pts[0].X:
=10;pts[0].Y:
=10;
pts[1].X:
=60;pts[1].Y:
=10;
pts[2].X:
=10;pts[2].Y:
=100;
pts[3].X:
=40;pts[3].Y:
=100;
pts[4].X:
=100;pts[4].Y:
=10;
pts[5].X:
=55;pts[5].Y:
=99;
pts[6].X:
=70;pts[6].Y:
=10;
Canvas.PolyBezier(pts);
end;
{PolyBezierTo绘制多条线时,需要3*x个点}
procedureTForm1.Button2Click(Sender:
TObject);
var
pts:
array[1..6]ofTPoint;
begin
Canvas.Pen.Width:
=2;
Canvas.Pen.Color:
=clRed;
Canvas.MoveTo(10+82,10);
//pts[0].X:
=10;pts[0].Y:
=10;
pts[1].X:
=60+82;pts[1].Y:
=10;
pts[2].X:
=10+82;pts[2].Y:
=100;
pts[3].X:
=40+82;pts[3].Y:
=100;
pts[4].X:
=100+82;pts[4].Y:
=10;
pts[5].X:
=55+82;pts[5].Y:
=99;
pts[6].X:
=70+82;pts[6].Y:
=10;
Canvas.PolyBezierTo(pts);
end;
end.
//效果图:
Delphi的绘图功能[8]-TextOut、TextWidth、TextHeight
unitUnit1;
interface
uses
Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
Dialogs,StdCtrls;
type
TForm1=class(TForm)
Button1:
TButton;
procedureButton1Click(Sender:
TObject);
end;
var
Form1:
TForm1;
implementation
{$R*.dfm}
procedureTForm1.Button1Click(Sender:
TObject);
const
arr:
array[0..3]ofWideString=(
'白日依山尽',
'黄河入海流',
'欲穷千里目',
'更上一层楼'
);
a=20;{横向间隔}
b=20;{纵向间隔}
var
s:
string;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Delphi 的绘图功能 绘图 功能
![提示](https://static.bdocx.com/images/bang_tan.gif)