*★已知某一个进程句柄,如何得到此进程下所有打开窗体的句柄? ( 积分: 200 )

A

abencat

Unregistered / Unconfirmed
GUEST, unregistred user!
已知某一个进程句柄,如何得到此进程下所有打开窗体的句柄? 谢谢<br>比如 delphi32.exe的
 
已知某一个进程句柄,如何得到此进程下所有打开窗体的句柄? 谢谢<br>比如 delphi32.exe的
 
function EnumerateChildWindows(hWnd: HWND; lParam: LPARAM): BOOL;<br>var<br> &nbsp;WindowCaption:array[0..254] of Char;<br> &nbsp;classed:array[0..254] of Char;<br> &nbsp;buffer:array [0..255] of char;<br>begin<br> &nbsp;GetWindowText(Hwnd,WindowCaption, 255);<br> &nbsp;GetClassName(Hwnd, classed, 255);<br> &nbsp;Form1.ListBox2.Items.Add('Handle:'+IntToStr(HWnd)+',Caption:'+StrPas(WindowCaption)+',Class:'+StrPas(classed));<br> &nbsp;Result:=TRUE;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;hCurrentWindow,hWnd1:HWnd;<br> &nbsp;szText:array[0..254]of char;<br> &nbsp;Wind : String;<br>begin<br> &nbsp;hCurrentWindow:=GetWindow(Handle,GW_HWNDFIRST);<br> &nbsp;While hCurrentWindow&amp;lt;&amp;gt;0 Do<br> &nbsp;Begin<br> &nbsp; &nbsp;If GetWindowText(hCurrentWindow,@szText,255)&amp;gt;0 then<br> &nbsp; &nbsp; &nbsp;if pos('Delphi 6',Strpas(@szText))&amp;gt;0 then begin<br> &nbsp; &nbsp; &nbsp; &nbsp;EnumChildWindows( hCurrentWindow, @EnumerateChildWindows, 0 );<br> &nbsp; &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;hCurrentWindow:=GetWindow(hCurrentWindow,GW_HWNDNEXT);<br> &nbsp;end;<br>end;<br><br>
 
希望是 <br>根据某个进程的ID 得到下面的窗体句柄<br>你这种方法不太适合<br>
 
//得到进程的ID 是下面的方法<br>Var<br> &nbsp;lppe: TProcessEntry32;<br> &nbsp;found: boolean;<br> &nbsp;Hand: THandle;<br>Begin<br> &nbsp;Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);<br> &nbsp;found := Process32First(Hand, lppe);<br> &nbsp;While found Do<br> &nbsp; &nbsp;Begin<br> &nbsp; &nbsp; &nbsp;ListBox1.Items.Add(StrPas(lppe.szExeFile)); //列出所有进程。<br> &nbsp; &nbsp; &nbsp;If StrPas(lppe.szExeFile) = 'delphi32.exe' Then<br> &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //GetAllF(Hand);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br> &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp;found := Process32Next(Hand, lppe);<br> &nbsp; &nbsp;End;<br>End;
 
吃完饭回来,再帮你改改看。
 
好的 &nbsp;谢谢
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1863299
 
to ysai<br>你的连接没意思
 
我是想根据得到的窗体句柄,然后在得到窗体的PID和原来的比较 &nbsp;看一样的就是 ,可是行不通
 
你不仔细看当然没意思,多说无益,自己看API:EnumThreadWindows
 
jianguobu的方法可以得到句柄,这个我也知道 ,问题是如何根据进程的ID得到本进程下的窗体句柄
 
用enumwindow来枚举窗口,用GetWindowThreadProcessId来判断窗口所属得进程
 
不行呀 <br>Unit Unit1;<br><br>Interface<br><br>Uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls, TLHelp32;<br><br>Type<br> &nbsp;TProcessInfo = Record<br> &nbsp; &nbsp;ExeFile: String;<br> &nbsp; &nbsp;ProcessID: DWORD;<br> &nbsp;End;<br> &nbsp;pProcessInfo = ^TProcessInfo;<br>Type<br> &nbsp;TForm1 = Class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;ListBox1: TListBox;<br> &nbsp; &nbsp;Button2: TButton;<br> &nbsp; &nbsp;Button3: TButton;<br> &nbsp; &nbsp;Button4: TButton;<br> &nbsp; &nbsp;Button5: TButton;<br> &nbsp; &nbsp;Button6: TButton;<br> &nbsp; &nbsp;Procedure Button1Click(Sender: TObject);<br> &nbsp; &nbsp;Procedure FormCreate(Sender: TObject);<br> &nbsp; &nbsp;Procedure FormDestroy(Sender: TObject);<br> &nbsp; &nbsp;Procedure Button2Click(Sender: TObject);<br> &nbsp; &nbsp;Procedure Button3Click(Sender: TObject);<br> &nbsp; &nbsp;Procedure Button4Click(Sender: TObject);<br> &nbsp; &nbsp;Procedure Button5Click(Sender: TObject);<br> &nbsp; &nbsp;Procedure Button6Click(Sender: TObject);<br> &nbsp;Private<br> &nbsp; &nbsp;function GetAllF(h: HWnd): boolean;<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;Public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;End;<br><br>Var<br> &nbsp;Form1: TForm1;<br> &nbsp;l: Tlist; ////返回的东东在&quot;L&quot;这个TList中。<br><br>Implementation<br><br>Procedure TForm1.Button1Click(Sender: TObject);<br>Var<br> &nbsp;lppe: TProcessEntry32;<br> &nbsp;found: boolean;<br> &nbsp;Hand: THandle;<br>Begin<br> &nbsp;Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);<br> &nbsp;found := Process32First(Hand, lppe);<br> &nbsp;While found Do<br> &nbsp; &nbsp;Begin<br> &nbsp; &nbsp; &nbsp;//ListBox1.Items.Add(StrPas(lppe.szExeFile)); //列出所有进程。<br> &nbsp; &nbsp; &nbsp;If StrPas(lppe.szExeFile) = 'delphi32.exe' Then<br> &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// OpenProcess(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //EnumChildWindows(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetAllF(Hand);<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EnumThreadWindows(OpenProcess(PROCESS_TERMINATE,false,lppe.th32ProcessID), @EnumProc, Integer(ListBox1.Items));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br> &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp;found := Process32Next(Hand, lppe);<br> &nbsp; &nbsp;End;<br>End;<br>function TForm1.GetAllF(h:HWnd):boolean ;<br>Var<br> &nbsp;hCurrentWindow: HWnd;<br> &nbsp;szText: Array[0..254] Of char;<br>Begin<br> &nbsp;listbox1.clear;<br> &nbsp;hCurrentWindow :=GetWindow(Handle, GW_HWNDFIRST);<br> &nbsp;While hCurrentWindow &amp;lt;&amp;gt; 0 Do<br> &nbsp; &nbsp;Begin<br> &nbsp; &nbsp; &nbsp;If GetWindowText(hCurrentWindow, @szText, 255) &amp;gt; 0 Then<br> &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if GetWindowThreadProcessId(hCurrentWindow)=h then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ListBox1.Items.Add(Strpas(@szText));<br> &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp;hCurrentWindow := GetWindow(hCurrentWindow, GW_HWNDNEXT);<br> &nbsp; &nbsp;End;<br>End;
 
还请高手指教
 
var<br> &nbsp;dwProcessID:DWORD;<br><br>function EnumWindowsProc(hwnd:HWND;lParam:LPARAM):BOOL;stdcall;<br>var<br> &nbsp;ProcID:DWORD;<br> &nbsp;szCaption:array[0..255] of Char;<br>begin<br> &nbsp;Result:=True;<br> &nbsp;GetWindowThreadProcessID(hwnd,ProcID);<br> &nbsp;if ProcID=dwProcessID then<br> &nbsp;begin<br> &nbsp; &nbsp;SendMessage(hwnd,WM_GETTEXT,255,Integer(@szCaption));<br> &nbsp; &nbsp;Form1.Memo1.Lines.Add(Format('Handle:%.5d &nbsp;Caption:%s',[hwnd,szCaption]));<br> &nbsp;end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> &nbsp;dwProcessID:=GetProcessID('Delphi32.exe'); <br> &nbsp;//利用CreateToolhelp32Snapshot得到进程ID<br> &nbsp;EnumWindows(@EnumWindowsProc,0);<br> &nbsp;//枚举窗口<br>end;<br>
 
看看是不是你要的<br>http://www.delphibbs.com/keylife/images/u100686/Demo2.rar
 
to smokingroom<br> &nbsp; &nbsp;GetProcessID是????<br>to ysai<br> &nbsp; 什么也看不出来,没有程序
 
看看以下能否满足你的要求<br><br>function EnumerateChildWindows(hWnd: HWND; lParam: LPARAM): BOOL;<br>var<br> &nbsp;WindowCaption:array[0..254] of Char;<br> &nbsp;classed:array[0..254] of Char;<br> &nbsp;buffer:array [0..255] of char;<br>begin<br> &nbsp;GetWindowText(Hwnd,WindowCaption, 255);<br> &nbsp;GetClassName(Hwnd, classed, 255);<br> &nbsp;Form1.ListBox1.Items.Add('Handle:'+IntToStr(HWnd)+',Caption:'+StrPas(WindowCaption)+',Class:'+StrPas(classed));<br> &nbsp;Result:=TRUE;<br>end;<br><br>function EnumWindowsFunc(Handle: THandle; List: TStringList): boolean ; stdcall;<br>var<br> &nbsp;Caption: array[0..256] of Char;<br> &nbsp;PID: DWORD;<br> &nbsp;dWnd: HWND;<br>begin<br> &nbsp;if GetWindowText(Handle, Caption, SizeOf(Caption)-1) &amp;lt;&amp;gt; 0 then<br> &nbsp; begin<br> &nbsp; &nbsp;List.Add(Caption);<br> &nbsp; &nbsp;dWnd := GetWindowThreadProcessId(Handle, @PID);<br> &nbsp; &nbsp;if (ProcessHandle=Pid) then begin &nbsp; // and (Caption='Delphi 6')<br> &nbsp;// &nbsp; &nbsp;SetWindowText(Handle, PChar('About - ' + Caption));<br> &nbsp; &nbsp; &nbsp;EnumChildWindows(Handle, @EnumerateChildWindows, 0 );<br> &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; &nbsp;end;<br>// &nbsp; &nbsp;SetWindowText(Handle, PChar('About - ' + Caption));<br> &nbsp; end;<br> &nbsp;Result :=True;<br>end;<br><br>function FindProcessName(ProcessName:String):THandle;<br>var<br> &nbsp;lppe: tprocessentry32;<br> &nbsp;sshandle: thandle;<br> &nbsp;found: boolean;<br>begin<br> &nbsp;result:=0;<br> &nbsp;sshandle := createtoolhelp32snapshot(TH32CS_SNAPALL, 0);<br> &nbsp;found := process32first(sshandle, lppe);<br> &nbsp;while found do<br> &nbsp;begin<br>// &nbsp; &nbsp;if ansiCompareText(ExtractFileName(lppe.szExefile),'EXPLORER.EXE') = 0 then<br> &nbsp; &nbsp;if ansiCompareText(ExtractFileName(lppe.szExefile),ProcessName) = 0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; result:=lppe.th32ProcessID;<br><br> &nbsp; &nbsp; &nbsp; break;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;found := process32next(sshandle, lppe); {检索下一个进程}<br> &nbsp;end;<br> &nbsp;CloseHandle(sshandle);<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> &nbsp;Memo1.Clear;<br> &nbsp;ProcessHandle := FindProcessName('delphi32.exe');<br> &nbsp;EnumWindows(@EnumWindowsFunc, LParam(Memo1.Lines));<br>end;<br><br>
 
我想smokingroom的GetProcessID就是我的FindProcessName
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
顶部