Delphi 绘图TCanvas类[3] TPen类参数及介绍
Delphi 绘图TCanvas类[3] TPen类参数及介绍
1、TPen(用于在画布上绘制线条或轮廓形状)
单元:Graphics
TPen = class(TGraphicsObject)
private
FMode: TPenMode;
procedure GetData(var PenData: TPenData);
procedure SetData(const PenData: TPenData);
protected
function GetColor: TColor;
procedure SetColor(Value: TColor);
function GetHandle: HPen;
procedure SetHandle(Value: HPen);
procedure SetMode(Value: TPenMode);
function GetStyle: TPenStyle;
procedure SetStyle(Value: TPenStyle);
function GetWidth: Integer;
procedure SetWidth(Value: Integer);
public
constructor Create;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
property Handle: HPen read GetHandle write SetHandle;
published
property Color: TColor read GetColor write SetColor default clBlack;
property Mode: TPenMode read FMode write SetMode default pmCopy;
property Style: TPenStyle read GetStyle write SetStyle default psSolid;
property Width: Integer read GetWidth write SetWidth default 1;
end;
TPen 主要属性: Color、Width、Style、Mode
2、TPenStyle(确定画笔绘制线条的样式)
- psSolid = 0; {实线.}
- psDash = 1; {段线. 由一系列破折号组成的线,要求笔宽<=1}
- psDot = 2; {点线. 由一系列点组成的线,要求笔宽<=1}
- psDashDot = 3; {线和点. 由交替的破折号和点组成的线,要求笔宽<=1}
- psDashDotDot = 4; {线点点. 由一系列虚线-点-点组合组成的线,要求笔宽<=1}
- psClear = 5; {不绘制线条(用于省略使用当前笔绘制轮廓的形状周围的线条)} {不可见}
- psInsideFrame = 6; {实线,但如果宽度大于1,则可能使用抖动颜色. 笔宽是向里扩展}
- psUserStyle = 7; {自定义, Delphi.Net 支持}
- psAlternate = 8; {交替, Delphi.Net 支持}
示例1:红色实线
Canvas.Pen.Color := clRed;
Canvas.Pen.Style := psSolid ;
//或:Canvas.Pen.Style := TPenStyle(0);
示例2:(在最大化的窗体上绘制许多大小和颜色各异的矩形)
var
qX, qY: Integer;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
Timer1.Enabled:=not Timer1.Enabled;
if Timer1.Enabled then begin
WindowState := wsMaximized;
Timer1.Interval := 50;
Randomize;
end else begin
WindowState := wsNormal;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
qX := Random(Screen.Width - 10);
qY := Random(Screen.Height - 10);
Canvas.Pen.Color := Random(65535);
case Random(5) of
0: Canvas.Pen.Style := psSolid;
1: Canvas.Pen.Style := psDash;
2: Canvas.Pen.Style := psDot;
3: Canvas.Pen.Style := psDashDot;
4: Canvas.Pen.Style := psDashDotDot;
end;
Canvas.Rectangle(qX, qY, qX + Random(400), qY + Random(400));
end;
3、TPenMode(指示笔的颜色如何与画布的颜色交互)
单元:Graphics 或 QGraphics
type TPenMode = (pmBlack, pmWhite, pmNop, pmNot, pmCopy, pmNotCopy, pmMergePenNot, pmMaskPenNot, pmMergeNotPen, pmMaskNotPen, pmMerge, pmNotMerge, pmMask, pmNotMask, pmXor, pmNotXor);
- pmBlack {总是黑色的}
- pmWhite {总是白色的}
- pmNop {不变的,画笔无效}
- pmNot {画布背景色的反转,背景色取反}
- pmCopy {颜色属性中指定的笔颜色,画笔设置的颜色}
- pmNotCopy {画笔颜色取反}
- pmMergePenNot {画笔颜色与画布背景颜色的反色的组合}
- pmMaskPenNot {画笔颜色与画布背景公共色的反色的组合}
- pmMergeNotPen {画笔颜色取反后与画布背景颜色的组合}
- pmMaskNotPen {画笔的反色与画布背景的公共色的组合}
- pmMerge {画笔颜色与画布背景颜色的组合}
- pmNotMerge {pmMerge的反向:笔色和画布背景色的组合}
- pmMask {画笔与背景公共颜色的组合}
- pmNotMask {pmMask 的反色}
- pmXor {画笔颜色与背景颜色的 Xor 组合}
- pmNotXor {pmXor 的反色}
例如:
Canvas.Pen.Mode := pmBlack;
//或:Canvas.Pen.Mode := TPenMode(0);
创建时间:2020.02,01 更新时间:2022.04.12
博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你有所帮助,谢谢!