**於
handle := findwindow(nil,pchar('視窗的標題'));//或者:
procedure tform1.button1click(sender: tobject);
var hcurrentwindow: hwnd;
wndtext:string;
begin
hcurrentwindow := getwindow(handle, gw_hwndfirst);
while hcurrentwindow <> 0 do
begin
wndtext:=getwndtext(hcurrentwindow);
if uppercase(wndtext)='視窗的標題' then begin
......
end;
hcurrentwindow:=getwindow(hcurrentwindow, gw_hwndnext);
end;
end;
因為目前網路上絕大部分的**都是介紹用這兩種方法取得其它程序的視窗控制代碼。雖這兩種方法都可以達到查詢其它程序的視窗控制代碼的目的,但本人認為這兩都方法存在較大的弊端。因為這兩種方法都是根據其它程序的標題來查詢的,如果其它程序的標題在執行時不斷的發生變化,那麼這兩種方法就無法沒辦法用了。
uses tlhelp32;procedure tform1.button1click(sender: tobject);
var processname : string; //程序名
fsnapshothandle:thandle; //程序快照控制代碼
fprocessentry32:tprocessentry32; //程序入口的結構體資訊
continueloop:bool;
myhwnd:thandle;
begin
fprocessentry32.dwsize:=sizeof(fprocessentry32);
continueloop:=process32first(fsnapshothandle,fprocessentry32); //得到系統中第一個程序
//迴圈例舉
while continueloop do
begin
processname := fprocessentry32.szexefile;
if(processname = '要找的應用程式名.exe') then begin
myhwnd := gethwndbypid(fprocessentry32.th32processid);
......
end;
continueloop:=process32next(fsnapshothandle,fprocessentry32);
end;
closehandle(fsnapshothandle); // 釋放快照控制代碼
end;
//跟據processid獲取程序的視窗控制代碼
function tform1.gethwndbypid(const hpid: thandle): thandle;
type
penuminfo = ^tenuminfo;
tenuminfo = record
processid: dword;
hwnd: thandle;
end;
function enumwindowsproc(wnd: dword; var ei: tenuminfo): bool; stdcall;
varpid: dword;
begin
getwindowthreadprocessid(wnd, @pid);
result := (pid <> ei.processid) or
(not iswindowvisible(wnd)) or
(not iswindowenabled(wnd));
if not result then ei.hwnd := wnd;
end;
function findmainwindow(pid: dword): dword;
varei: tenuminfo;
begin
ei.processid := pid;
ei.hwnd := 0;
enumwindows(@enumwindowsproc, integer(@ei));
result := ei.hwnd;
end;
begin
if hpid<>0 then
result:=findmainwindow(hpid)
else
result:=0;
end;
第四種方法通過滑鼠的位置來獲取當前滑鼠所在窗體的控制代碼
vara:tpoint; //用來存放座標
hw:hwnd; //用來存放視窗控制代碼
begin
getcursorpos(a); //取得滑鼠座標,並存放進a中
hw := windowfrompoint(a); //取得變數a 對應的 視窗控制代碼
end