// QQ是有,不过我很少上QQ的,加了也作用不大[
]。不太清楚你的意思,所以便改成了如下:
// Hook Dll不用改了。直接改Demo就行了。
//红色为添加部分。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
const
USER_WM_MOUSEDOWN = WM_USER + 1000;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Button1: TButton;
Button2: TButton;
[red]Image1: TImage;[/red]
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
[red]procedure FormMouseDown(Sender: TObject; Button: TMouseButton;[/red]
Shift: TShiftState; X, Y: Integer);
private
procedure UserWmMouseDown(var Msg:TMessage); message USER_WM_MOUSEDOWN;
{ Private declarations }
public
{ Public declarations }
end;
Function OnHook:LResult; external 'Project2.dll';
Function UnHook:Boolean; external 'Project2.dll';
var
Form1: TForm1;
[red]IsCapture:Boolean; // 设置全局鼠标捕获标志[/red]
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.UserWmMouseDown(var Msg: TMessage);
var Point:TPoint;
hWindow:HWND;
begin
GetCursorPos(Point);
{ 显示Screen屏幕座标 }
Label3.Caption := Format('X: %u - Y: %u',[Point.X,Point.Y]);
hWindow := WindowFromPoint(Point);
windows.ScreenToClient(hWindow,point);
// 显示窗口座标
{ 注:窗口座标是相对于鼠标所指向的子窗口的座标。
如鼠标是指向某个窗口的Toolbar控件,那窗口座标就是相对于Toolbar控件的}
Label4.Caption := Format('X: %u - Y: %u',[Point.X,Point.Y]);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
SetWindowPos(handle,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE OR SWP_NOSIZE);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
OnHook();
[red]if Not(IsCapture) then
begin
// 开始区域抓图,先最小化自己的窗口。
SetCapture(Handle);
ShowWindow(Handle,SW_MINIMIZE);
IsCapture := TRUE;
end;[/red]
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
UnHook();
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
UnHook();
end;
[red]
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var Bitmap:TBitmap;
Rect1:TRect;
Point:TPoint;
Canvas:TCanvas;
begin
// 如果是点击鼠标右键,则取消抓图
if (Shift = [ssRight]) and (IsCapture) then
begin
IsCapture := FALSE;
ShowWindow(Handle,SW_RESTORE);
SetForegroundWindow(Handle);
ReleaseCapture;
// 如果是点击鼠标左键,则开始抓图
end else if (Shift = [ssLeft]) and (IsCapture) then
begin
IsCapture := FALSE;
Bitmap := TBitmap.Create;
Canvas := TCanvas.Create;
try
// 获取当前鼠标座标,并计算出以鼠标为中心的区域大小(这里是200*200的大小)
GetCursorPos(Point);
if Point.X > 100 then
Rect1.Left := Point.X - 100
else
Rect1.Left := Point.X;
if Point.Y > 100 then
Rect1.Top := Point.Y - 100
else
Rect1.Top := Point.Y;
Rect1.Right := Rect1.Left + 200;
Rect1.Bottom := Rect1.Top + 200;
Canvas.Handle := GetDc(0);
Bitmap.Width := 200;
Bitmap.Height := 200;
// 区域抓取图像
Bitmap.Canvas.CopyRect(Rect(0,0,200,200),Canvas,Rect1);
Image1.Picture.Bitmap.Assign(Bitmap);
finally
ReleaseCapture;
ShowWindow(Handle,SW_RESTORE);
SetForegroundWindow(Handle);
ReleaseDC(0,Canvas.Handle);
Bitmap.Free;
Canvas.Free;
UnHook;
end;
end;
end;
[/red]
end.