//奇妙的异或运算, 就这些简单的代码, 反复点击按钮... procedure TForm1.Button1Click(Sender: TObject); begin Canvas.Pen.Color := clYellow; {指定笔颜色为黄色} Canvas.Pen.Width := 20; {笔宽 20} Canvas.Pen.Mode := pmXor; {指定画笔模式为: 画笔色与背景色的异或运算} {画线} Canvas.MoveTo(0,0); Canvas.LineTo(ClientWidth,ClientHeight); end;
//效果图:
//做一个橡皮线的示例: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); procedure FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); end; var Form1: TForm1; implementation {$R *.dfm} var x1,y1,x2,y2: Integer; {直线两个端点的坐标} f: Boolean; {判断鼠标是否点下} procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin x1 := X; y1 := Y; x2 := X; y2 := Y; Canvas.Pen.Color := clRed; Canvas.Pen.Width := 2; Canvas.Pen.Mode := pmXor; f := True; end; procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); begin if f then begin {擦除} Canvas.MoveTo(x1, y1); Canvas.LineTo(x2, y2); x2 := X; y2 := y; {重绘} Canvas.MoveTo(x1, y1); Canvas.LineTo(x2, y2); end; end; procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin if f then begin {画出确定的线} Canvas.Pen.Mode := pmCopy; Canvas.MoveTo(x1, y1); Canvas.LineTo(x2, y2); f := not f; end; end; end.
//效果图: