uses tlhelp32;
type
tmainwindow = packed record
processid: thandle;
mainwindow: thandle;
end;
pmainwindow = ^tmainwindow;
function ismainwindow(ahandle: thandle): boolean;
begin
result := (getwindow(ahandle, gw_owner) = 0) and (iswindowvisible(ahandle));
end; { ismainwindow }
function ffindmainwindow(hwnd: thandle; lparam: pmainwindow): bool; stdcall;
var
vprocessid: thandle;
begin
getwindowthreadprocessid(hwnd, vprocessid);
if (lparam^.processid = vprocessid) and ismainwindow(hwnd) then
begin
lparam^.mainwindow := hwnd;
result := false;
end else result := true;
end;
function findmainwindow(aprocessid: thandle): thandle;
var
vmainwindow: tmainwindow;
begin
vmainwindow.processid := aprocessid;
vmainwindow.mainwindow := 0;
enumwindows(@ffindmainwindow, integer(@vmainwindow));
result := vmainwindow.mainwindow;
end; { findmainwindow }
procedure tform1.button1click(sender: tobject);
var
vsnapshot: thandle;
vprocessentry32: tprocessentry32;
vhandle: thandle;
begin
vsnapshot := createtoolhelp32snapshot(th32cs_snapprocess, 0);
vprocessentry32.dwsize := sizeof(tprocessentry32);
if process32first(vsnapshot, vprocessentry32) then
repeat
if sametext('icesword.exe', vprocessentry32.szexefile) then
begin
vhandle := findmainwindow(vprocessentry32.th32processid); // 查询进程的主窗体
if vhandle <> 0 then
begin
postmessage(vhandle, wm_close, 0, 0); // 发送关闭消息
sleep(100); // 等待对话框出现
vhandle := findwindow('#32770', 'icesword');
if vhandle <> 0 then
begin
vhandle := findwindowex(vhandle, 0, 'button', nil);
sendmessage(vhandle, wm_lbuttondown, 0, 0);
sendmessage(vhandle, wm_lbuttonup, 0, 0);
///...点击按钮两次
sendmessage(vhandle, wm_lbuttondown, 0, 0);
sendmessage(vhandle, wm_lbuttonup, 0, 0);
end;
end;
break;
end;
until not process32next(vsnapshot, vprocessentry32);
closehandle(vsnapshot);
end;