//TPen 的主要属性有四: Color、Width、Style、Mode {Color: 颜色} {Width: 宽度; 默认是 1; 如果赋予 <= 0 的值, 会使用默认值} {Style: 样式; Delphi 定义了笔样式枚举 TPenStyle, 包含以下样式:} psSolid = 0; {实线} psDash = 1; {段线; 要求笔宽<=1} psDot = 2; {点线; 要求笔宽<=1} psDashDot = 3; {线、点; 要求笔宽<=1} psDashDotDot = 4; {线、点、点; 要求笔宽<=1} psClear = 5; {不可见} psInsideFrame = 6; {实线; 但笔宽是向里扩展} psUserStyle = 7; {自定义, Delphi.Net 支持} psAlternate = 8; {交替, Delphi.Net 支持}
//下面的例子依次展示了各种样式: var i,x,y,n: Integer; begin x := 10; y := 15; n := ClientWidth - 2*x; Canvas.Pen.Color := clRed; for i := 0 to 8 do begin Canvas.Pen.Style := TPenStyle(i); Canvas.MoveTo(x, y); Canvas.LineTo(x + n, y); y := y + 15; end; end;
//效果图:
{Mode: 画笔与背景的颜色混合模式; Delphi 定义了模式枚举 TPenMode, 包含以下模式:} pmBlack {总是黑色} pmWhite {总是白色} pmNop {画笔无效} pmNot {背景色取反} pmCopy {画笔设置的颜色} pmNotCopy {画笔颜色取反} pmMergePenNot {画笔颜色与背景颜色的反色的组合} pmMaskPenNot {画笔颜色与背景公共色的反色的组合} pmMergeNotPen {画笔颜色取反后与背景颜色的组合} pmMaskNotPen {画笔的反色与背景的公共色的组合} pmMerge {画笔颜色与背景颜色的组合} pmNotMerge {pmMerge 的反色} pmMask {画笔与背景公共颜色的组合} pmNotMask {pmMask 的反色} pmXor {画笔颜色与背景颜色的 Xor 组合} pmNotXor {pmXor 的反色}
//颜色模式测试程序: var i,x,y,w,n: Integer; begin {先画竖的颜色背景} x := 0; y := 0; w := ClientWidth div 18; for i := 0 to 18 do begin Canvas.Brush.Color := PaletteIndex(i); Canvas.FillRect(Rect(x, y, x+w, ClientHeight)); x := x + w; end; {再按不同模式依次画 16 条红色的横线} x := 0; y := 20; n := ClientWidth; Canvas.Pen.Width := 4; Canvas.Pen.Color := clRed; for i := 0 to 15 do begin Canvas.Pen.Mode := TPenMode(i); Canvas.MoveTo(x, y); Canvas.LineTo(x + n, y); y := y + 20; end; end;
//效果图: