介绍
本文章介绍了Delphi XE7实现的仿Spy取窗口句柄并得到窗体框架,鼠标点击左上角的图标,然后移动到某一个对象上面,在对象上面,会自动出现一个红色的框框,通过这个程序可以得到指定窗体的信息。
主要代码
unit MainForm;
interface
uses Winapi.Windows,
Winapi.Messages,
System.SysUtils,
System.Variants,
System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.ExtCtrls,
Vcl.ImgList;
type
TForm2 = class(TForm)
Image1: TImage;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Image1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure Image1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2 : TForm2;
img : Tbitmap;
oldCursor : Integer;
g_nHex : boolean;
ico : Hicon;
style, styleex, classstyle: Integer;
SnapHwnd, tempHwnd : HWND;
implementation
{$R *.dfm}
procedure TForm2.FormCreate(Sender: TObject);
begin
screen.cursors[1] := loadcursor(Hinstance, 'Cursor_2');
ico := loadIcon(Hinstance, 'Icon_1');
Image1.Picture.Icon.Handle := ico;
end;
procedure TForm2.Image1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if mbLeft = Button then
begin
ico := loadIcon(Hinstance, 'Icon_2');
Image1.Picture.Icon.Handle := ico;
oldCursor := screen.cursor;
screen.cursor := 1;
Timer1.Enabled := True;
SetCapture(Image1.Parent.Handle);
end;
end;
procedure TForm2.Image1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
DeskHwnd, UnHwnd, grayHwnd: HWND;
DeskDC : HDC;
oldRop2, iTemp, i : Integer;
pnt : TPoint;
tempRc, Rc : TRect;
FindIt : boolean;
strp : array [0 .. 200] of char;
tmpstr : Pchar;
tmpString : string;
tmpPoint : DWord;
h : THandle;
fileName : string;
iLen : Integer;
hMod : HMODULE;
cbNeeded, p : DWord;
begin
if mbLeft = Button then
begin
ico := loadIcon(Hinstance, 'Icon_1');
Image1.Picture.Icon.Handle := ico;
screen.cursor := crDefault;
Timer1.Enabled := false;
ReleaseCapture;
GetCursorPos(pnt);
UnHwnd := WindowFromPoint(pnt);
grayHwnd := GetWindow(UnHwnd, GW_CHILD);
FindIt := false;
while (grayHwnd <> 0) do
begin
GetWindowRect(grayHwnd, tempRc);
If PtInRect(tempRc, pnt) Then
begin
FindIt := true;
break;
end Else begin
grayHwnd := GetWindow(grayHwnd, GW_HWNDNEXT)
end;
end;
If FindIt = true Then
begin
FindIt := false;
SnapHwnd := grayHwnd;
Caption := Integer(SnapHwnd).ToHexString(8);
end Else begin
SnapHwnd := UnHwnd;
Caption := Integer(UnHwnd).ToHexString(8);
end;
end;
end;
procedure TForm2.Timer1Timer(Sender: TObject);
var
DeskHwnd, UnHwnd, SnapHwnd, grayHwnd: HWND;
DeskDC : HDC;
oldRop2 : Integer;
pnt : TPoint;
tempRc, Rc : TRect;
FindIt : boolean;
H0 : COLORREF;
oldPen : HGDIOBJ;
newPen : Hpen;
begin
DeskHwnd := GetDesktopWindow();
DeskDC := GetWindowDC(DeskHwnd);
oldRop2 := SetROP2(DeskDC, 10);
GetCursorPos(pnt);
UnHwnd := WindowFromPoint(pnt);
grayHwnd := GetWindow(UnHwnd, GW_CHILD);
FindIt := false;
while (grayHwnd <> 0) do
begin
GetWindowRect(grayHwnd, tempRc);
If PtInRect(tempRc, pnt) Then
begin
FindIt := True;
break;
end Else begin
grayHwnd := GetWindow(grayHwnd, GW_HWNDNEXT)
end;
end;
If FindIt = True Then
begin
FindIt := false;
SnapHwnd := grayHwnd;
end Else begin
SnapHwnd := UnHwnd;
end;
GetWindowRect(SnapHwnd, Rc);
If Rc.left < 0 Then
Rc.left := 0;
If Rc.top < 0 Then
Rc.top := 0;
// If rc.Right > Screen.Width / 15 Then rc.Right := Screen.Width / 15 ;
// If rc.Bottom > Screen.Height / 15 Then rc.Bottom := Screen.Height / 15;
H0 := RGB(255, 0, 0);
newPen := CreatePen(0, 4, H0); // 建立新画笔,载入DeskDC
oldPen := SelectObject(DeskDC, newPen);
Rectangle(DeskDC, Rc.left, Rc.top, Rc.right, Rc.bottom);
Sleep(400);
Rectangle(DeskDC, Rc.left, Rc.top, Rc.right, Rc.bottom);
SetROP2(DeskDC, oldRop2);
SelectObject(DeskDC, oldPen);
DeleteObject(newPen);
ReleaseDC(DeskHwnd, DeskDC);
DeskDC := 0;
end;
end.