这个用hook实现,你看看吧,我是改自我的一个涂鸦程序,你可以去掉好多没用的东西。
unit tymain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls;
type
TForm1 = class(TForm)
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
GroupBox1: TGroupBox;
GroupBox3: TGroupBox;
SpeedButton3: TSpeedButton;
Panel1: TPanel;
Image1: TImage;
Panel2: TPanel;
Panel3: TPanel;
SpeedButton4: TSpeedButton;
Panel4: TPanel;
Panel5: TPanel;
Panel6: TPanel;
Panel7: TPanel;
Panel8: TPanel;
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Image1DblClick(Sender: TObject);
procedure SpeedButton3Click(Sender: TObject);
procedure Panel2DblClick(Sender: TObject);
procedure Image1Click(Sender: TObject);
procedure SpeedButton4Click(Sender: TObject);
procedure Panel5Click(Sender: TObject);
procedure Panel6Click(Sender: TObject);
procedure Panel7Click(Sender: TObject);
procedure Panel8Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
pencolor:tcolor;
mycanvas:tcanvas;
screendc:hdc;
pensize,hook,mm:integer;
can:boolean=false;
implementation
{$R *.dfm}
{$R mycursor.res}
function hookproc(icode:integer;wparam:wparam;lparam:lparam):lresult;stdcall;
var
evtmsg:teventmsg;
begin
evtmsg:=peventmsg(lparam)^;
form1.Left:=mouse.CursorPos.X; ///////这里被改动了,你也可以改的更精确。
form1.Top:=mouse.CursorPos.Y; ///////
if evtmsg.message=WM_LBUTTONDOWN then
begin
can:=true;
mycanvas.moveto(mouse.CursorPos.X,mouse.CursorPos.Y);
end;
if evtmsg.message=WM_LBUTTONUP then
can:=false;
if can=true then
begin
if evtmsg.message=WM_MOUSEMOVE then
begin
with mycanvas do
begin
Pen.Color:=form1.Panel1.Color;
lineto(mouse.CursorPos.X,mouse.CursorPos.Y);
end;
end;
end;
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
screendc:=getdc(0);
mycanvas:=tcanvas.Create;
mycanvas.Handle:=screendc;
end;
procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
InvalidateRect(0, nil, true);
close;
end;
procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
pencolor:=image1.Picture.Bitmap.Canvas.Pixels[X,Y];
panel3.Color:=pencolor;
end;
procedure TForm1.Image1DblClick(Sender: TObject);
var
colordial:tcolordialog;
begin
colordial:=tcolordialog.Create(self);
colordial.Color:=pencolor;
colordial.Execute;
panel2.Color:=colordial.Color;
end;
procedure TForm1.SpeedButton3Click(Sender: TObject);
var
scr:hicon;
begin
panel2.SetFocus;
scr:=loadcursor(hinstance,'mycur');
setsystemcursor(scr,ocr_normal);
mycanvas.Pen.Color:=panel1.Color;
hook:=setwindowshookex(WH_JOURNALRECORD,hookproc,hinstance,0);
end;
procedure TForm1.Panel2DblClick(Sender: TObject);
var
mycolor:tcolordialog;
begin
mycolor:=tcolordialog.Create(self);
mycolor.Color:=panel2.Color;
mycolor.Execute;
panel2.Color:=mycolor.Color;
end;
procedure TForm1.Image1Click(Sender: TObject);
begin
panel2.Color:=panel3.Color;
end;
procedure TForm1.SpeedButton4Click(Sender: TObject);
begin
systemparametersinfo(spi_setcursors,0,nil,spif_sendchange);
unhookwindowshookex(hook);
end;
procedure TForm1.Panel5Click(Sender: TObject);
begin
pensize:=panel5.Height;
if panel5.BorderStyle=bssingle then
begin
panel5.BorderStyle:=bsnone;
panel5.BevelInner:=bvnone;
panel5.BevelOuter:=bvnone;
end
else
begin
panel5.BorderStyle:=bssingle;
panel5.BevelInner:=bvlowered;
panel5.BevelOuter:=bvraised;
end;
end;
procedure TForm1.Panel6Click(Sender: TObject);
begin
pensize:=panel6.Height;
if panel6.BorderStyle=bssingle then
begin
panel6.BorderStyle:=bsnone;
panel6.BevelInner:=bvnone;
panel6.BevelOuter:=bvnone;
end
else
begin
panel6.BorderStyle:=bssingle;
panel6.BevelInner:=bvlowered;
panel6.BevelOuter:=bvraised;
end;
end;
procedure TForm1.Panel7Click(Sender: TObject);
begin
pensize:=panel7.Height;
if panel7.BorderStyle=bssingle then
begin
panel7.BorderStyle:=bsnone;
panel7.BevelInner:=bvnone;
panel7.BevelOuter:=bvnone;
end
else
begin
panel7.BorderStyle:=bssingle;
panel7.BevelInner:=bvlowered;
panel7.BevelOuter:=bvraised;
end;
end;
procedure TForm1.Panel8Click(Sender: TObject);
begin
pensize:=panel8.Height;
if panel8.BorderStyle=bssingle then
begin
panel8.BorderStyle:=bsnone;
panel8.BevelInner:=bvnone;
panel8.BevelOuter:=bvnone;
end
else
begin
panel8.BorderStyle:=bssingle;
panel8.BevelInner:=bvlowered;
panel8.BevelOuter:=bvraised;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
screendc:=getdc(0);
mycanvas:=tcanvas.Create;
mycanvas.Handle:=screendc;
end;
end.