如何根据进程名来找到该进程主窗口的handle(50分)

  • 主题发起人 主题发起人 sy0116
  • 开始时间 开始时间
S

sy0116

Unregistered / Unconfirmed
GUEST, unregistred user!
比如在任务管理器里有一a.exe,如何找到他的主窗口handle
 
wo ye hen xiang zhidao
 
好象窗口都是同级的,没有主窗口这一说,进程不信赖窗口的
 
可以用 EnumWindows 遍历系统中的顶层窗口,对每个窗口用 GetWindowThreadProcessId<br>得到该窗口所属的 Process 的 Id,再跟你用 ToolHelp API 得到的各 Process 的 Id 比<br>较就能知道该窗口属于哪个进程,但是不是进程的主窗口则不好判断,大概需要其他条件,<br>如已知进程主窗口的标题。
 
a.exe就一个窗口
 
unction TReadMemoryFrm.StartSearch: Boolean;<br>var<br> &nbsp;ProcHandle:Integer;<br>begin<br> &nbsp;Result:=False;<br> &nbsp;ReadMemoryProgress.Position:=0;<br> &nbsp;if Not CheckInput then Exit;<br><br> &nbsp;if FileName=TabSheet1.Caption then //-----------------------------------------搜索次数&gt;1次<br> &nbsp;begin<br> &nbsp; &nbsp;PParameter.FirstSearch:=False;<br> &nbsp; &nbsp;PParameter.Data:=StrToInt(EdtSearchData.Text);<br> &nbsp;end else<br> &nbsp;begin &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//-----------------------------------------第一次搜索<br> &nbsp; &nbsp;PParameter.FirstSearch:=True;<br> &nbsp; &nbsp;if PParameter.ProcessHandle&gt;0 then<br> &nbsp; &nbsp; &nbsp;CloseHandle(PParameter.ProcessHandle);<br> &nbsp; &nbsp;ProcHandle:=OpenProcess(PROCESS_ALL_ACCESS,false,StrToInt(EdtProcID.Text));<br> &nbsp; &nbsp;if ProcHandle&gt;0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;PParameter.Data:=StrToInt(EdtSearchData.Text);<br> &nbsp; &nbsp; &nbsp;Case DataType.ItemIndex of<br> &nbsp; &nbsp; &nbsp; &nbsp;0:PParameter.DataType:=1;<br> &nbsp; &nbsp; &nbsp; &nbsp;1:PParameter.DataType:=2;<br> &nbsp; &nbsp; &nbsp; &nbsp;2:PParameter.DataType:=4;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end else Exit;<br> &nbsp; &nbsp;FileName:=TabSheet1.Caption;<br> &nbsp; &nbsp;PParameter.ProcessHandle:=ProcHandle;<br> &nbsp;end;<br> &nbsp;<br> &nbsp;SearchButton.Enabled:=False;<br> &nbsp;ToolSearchMemory.Enabled:=False;<br> &nbsp;MemoryAddrList.Clear;<br> &nbsp;PReadMemory.StartSearch;<br> &nbsp;Result:=True;<br>end;
 
function TReadMemory.GetProcessInfo: TList;<br>var<br> &nbsp;ProcessInfoList : TList;<br> &nbsp;ProcessInfo &nbsp; &nbsp; : PProcessInfo;<br> &nbsp;hSnapShot &nbsp; &nbsp; &nbsp; : THandle;<br> &nbsp;mProcessEntry32 : TProcessEntry32;<br> &nbsp;bFound &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Boolean;<br>begin<br> &nbsp;ProcessInfoList:=TList.Create;<br> &nbsp;ProcessInfoList.Clear;<br> &nbsp;hSnapShot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);<br> &nbsp;mProcessEntry32.dwSize := Sizeof(mProcessEntry32);<br> &nbsp;bFound := Process32First(hSnapShot, mProcessEntry32);<br> &nbsp;while bFound do<br> &nbsp;begin<br> &nbsp; &nbsp;New(ProcessInfo);<br> &nbsp; &nbsp;ProcessInfo.ProcessExe := mProcessEntry32.szExeFile;<br> &nbsp; &nbsp;ProcessInfo.ProcessId := mProcessEntry32.th32ProcessID;<br> &nbsp; &nbsp;ProcessInfoList.Add(ProcessInfo);<br> &nbsp; &nbsp;bFound := Process32Next(hSnapShot, mProcessEntry32);<br> &nbsp;end;<br> &nbsp;Result := ProcessInfoList; &nbsp;<br>end;
 
接受答案了.
 
后退
顶部