Delphi獲取其它程序視窗控制代碼的4種方法

2022-06-23 19:21:13 字數 2591 閱讀 4248

**於

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;

第四種方法通過滑鼠的位置來獲取當前滑鼠所在窗體的控制代碼

var

a:tpoint; //用來存放座標

hw:hwnd; //用來存放視窗控制代碼

begin

getcursorpos(a); //取得滑鼠座標,並存放進a中

hw := windowfrompoint(a); //取得變數a 對應的 視窗控制代碼

end

GetThreadTimes獲取其它執行緒cpu時間

鄙視下上面的垃圾博文,純粹忽悠人 參考文章 這是乙個德國人寫的部落格,我看了好幾遍,對他寫的德式英語還是不太明白,本人英文水平不高也是事實 所以我理解的不對的地方還望大家指正。不過結論肯定是正確的,就是通過getthreadtimes 得到的執行緒占用時間是不準確的,在某種條件下甚至是很不準確的。另...

讀取其它程序richedit控制項的內容

和edit控制項不同的是,richedit支援rtf格式,採用wm getwindowtext訊息只能得到文字資訊,因此如果需要獲取完整的rtf檔案,則需要處理em streamout訊息,將控制項中的內容寫出。有些控制項對em streamout進行了過濾,則需要進一步處理em streamin來...

VC獲取其他程序ListCtrl內容

vc讀寫其他程序listctrl資料到本程序的例項,下面用windows任務管理器來做測試 1 捕獲視窗控制代碼 用spy 可以看到如下父子視窗關係 新增listctrl,設定style report 關聯控制項變數m listctrl,再新增乙個按鈕,如下圖 4 程式不足 a 在獲取任務管理器 程...