//奇妙的異或運算, 就這些簡單的代碼, 反復點擊按鈕...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.