怎样获得进程主窗口句柄(50分)

  • 主题发起人 主题发起人 xc
  • 开始时间 开始时间
X

xc

Unregistered / Unconfirmed
GUEST, unregistred user!
已知进程ID,不知窗口标题与窗口注册类名,怎样获得进程主窗口句柄。<br>&nbsp; &nbsp;email to:ya_xucheng@sina.com
 
一个进程ID可以对应多个HWND,如何判断哪个是主窗口?
 
枚举吧! 每找到一个窗口后,先得到它的进程ID,然后和已知的作对比
 
unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls;<br><br>type<br>&nbsp; EnumThreadWndProc=function(hw:HWND;lP:LPARAM):boolean;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; ListBox1: TListBox;<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure ListBox1DblClick(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>&nbsp; function Enum(hw:HWND;lP:LPARAM):boolean;stdcall;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; st:TStartUpInfo;<br>&nbsp; pp:array[1..3] of TProcessInformation;<br>&nbsp; ppp:Thandle;<br><br>implementation<br><br>{$R *.DFM}<br><br>function Enum(hw:HWND;lP:LPARAM):boolean;<br>var<br>&nbsp; Buffer:Array[0..255] of Char;<br>begin<br>&nbsp; if GetTopWindow(hw)&lt;&gt;0 then<br>&nbsp; begin<br>&nbsp; &nbsp; GetWindowText(hw,buffer,256);<br>&nbsp; &nbsp; TListBox(lp).items.addobject(buffer,Tobject(hw));<br>&nbsp; end;<br>&nbsp; Result:=true;<br>end;<br><br><br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; proc:EnumThreadWndProc;<br>&nbsp; i:integer;<br>begin<br>&nbsp; proc:=@Enum;<br>for i:=1 to 2 do<br>&nbsp; EnumThreadWindows(pp.dwThreadId,@proc,lparam(listbox1));<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>FillChar(st,sizeof(st),#0);<br>with st do<br>begin<br>&nbsp; cb:=sizeof(st);<br>&nbsp; dwFlags:=StartF_UsesTDHandles or STARTF_USESHOWWINDOW;<br>&nbsp; lptitle:=nil;<br>&nbsp; wShowWindow:=SW_HIDE;<br>end;<br>CreateProcess(PChar('c:/program files/microsoft office/office/winword.exe'),<br>&nbsp; &nbsp; &nbsp; nil,nil,nil,true,DETACHED_PROCESS,nil,nil,st,pp[1]);<br>CreateProcess(PChar('c:/program files/microsoft office/office/Excel.exe'),<br>&nbsp; &nbsp; &nbsp; nil,nil,nil,true,DETACHED_PROCESS,nil,nil,st,pp[2]);<br>end;<br><br>procedure TForm1.ListBox1DblClick(Sender: TObject);<br>begin<br>&nbsp; ppp:=Thandle(Listbox1.Items.Objects[Listbox1.ItemIndex]);<br>&nbsp; if IsWindowVisible(ppp) then<br>&nbsp; &nbsp; BringWindowToTop(ppp)<br>&nbsp; else<br>&nbsp; &nbsp; showwindow(ppp,sw_show);<br>end;<br><br>end.<br>
 
利用getwindow或findwindow来实现找窗口句柄,getwindowtext获得此窗口得标题。
 
关键还是o*o说的,<br>找到该进程的窗口容易,<br>如何判断哪个是主窗口难<br>而且他又不知道主窗口标题与窗口注册类名
 
没有父窗口的就是主窗口嘛! 简单判断一下就可以了.
 
很多没有父窗口的,<br>你用delphi编个程序,有几个窗口的,<br>可以看到很多父窗口为0的
 
在不知窗口标题与窗口注册类名情况下,还是讨论讨论主窗口与一般窗口的区别吧!
 
父窗口不准确,应该是owner.<br><br>通过create(nil)建立的窗口owner也是Tapplication
 
找的别的进程,又不一定是delphi写的,即使是delphi写的怎么知道别的进程的窗口的owner是什么?<br>别的进程的窗口的父窗口只能用 GetParent 来取得,<br>在delphi里面 Create(nil) Create(Apllication) Create(self)创建的窗口<br>的父窗口都是0,除非设置 Parent属性 将自己变成子窗口
 
&gt;&gt;怎么知道别的进程的窗口的owner是什么<br><br>GetWindow(Handle, GW_OWNER); <br><br>不论VC/VB/Delphi,原理都差不多.
 
To Cakk:<br>&nbsp; 你的方法好象不行,得到的Ower都相同,怎么判断谁是主窗口呢? &nbsp; {B-(<br><br>To All:<br>&nbsp; 要将其分辨出来,还是讨论讨论主窗口与一般窗口的区别吧!<br>&nbsp; 看了看Delphi的原代码,好象分辨不出来,因为Delphi程序在建立窗口是并没有<br>因为是主窗口就由什么铁别的代码,只是在建立主窗口时有以下程序段<br>&nbsp; if (FMainForm=nil) and (Instance is TForm) then<br>&nbsp; begin<br>&nbsp; &nbsp; TForm(Instance).HandleNeeded;<br>&nbsp; &nbsp; FMainForm:=TForm(Instance);<br>&nbsp; end;<br><br>在TForm.Close中,当窗口关闭时:<br>&nbsp; if CloseAction&lt;&gt; caNone then<br>&nbsp; &nbsp; if Application.MainForm=Self then Application.Terminate<br>&nbsp; &nbsp; else ......<br><br>也就是说,主窗口与一般窗口每什么分别。<br>我认为,要用程序动态得到一进程的主窗口是不现实的。<br><br>建议:<br>&nbsp; 得到进程的所有窗口,依次发送WM_CLOSE到该窗口,如果进程退出,可能是<br>主窗口,因为这进程的一个非主窗口关闭后可能违背了其的运行规则,它有可能<br>自己结束运行,所以你得到的结果有可能不正确! &nbsp; &nbsp;{B-)<br><br>看看其他人还有什么高招! &nbsp; <br>&nbsp; &nbsp;
 
后退
顶部