请教各位仁兄:程序需要等待外部程序中的某个对话窗口出现,在确认出现以后才能继续执行后面的代码,如何写这一段程序呢?(10分)

  • 主题发起人 主题发起人 lkdbdlkq
  • 开始时间 开始时间
L

lkdbdlkq

Unregistered / Unconfirmed
GUEST, unregistred user!
用createprocess 及waitforinputidle、waitforsingleobject的方法可以等待外部程序整体运行的结果,可是我现在只是需要等待外部程序中的某个对话框的状态,只要一发现对话框出现,程序立即继续执行下面的代码,否则的话就处于等候状态。  请问各位仁兄,如何实现这样的功能呢?
 
你不仿用个死循环 &nbsp;直到那个窗口出现 然后退出死循环<br>当然在死循环中用sleep() 然后休眠 &nbsp;和定时器效果一样 目的减少CPU占用
 
我没有办法。<br>只能在合时时间出现一个提示窗口,提示操作人工判断外部程序中的某个对话框的状态,再点我程序中的“确定”按钮。
 
用<br><br>线程 + FindWindows<br><br>应该可以解决你这个问题。
 
here is a function that you want to get<br>function WinExecAndWait32(FileName: string; Visibility: integer): Dword;<br> &nbsp; &nbsp; &nbsp;{执行一个外部程序并等待其结束}<br>var<br> &nbsp;zAppName: array[0..512] of char;<br> &nbsp;zCurDir: array[0..255] of char;<br> &nbsp;WorkDir: string;<br> &nbsp;StartupInfo: TStartupInfo;<br> &nbsp;ProcessInfo: TProcessInformation;<br>begin<br> &nbsp;StrPCopy(zAppName, FileName);<br> &nbsp;GetDir(0, WorkDir);<br> &nbsp;StrPCopy(zCurDir, WorkDir);<br> &nbsp;FillChar(StartupInfo, Sizeof(StartupInfo), #0);<br> &nbsp;StartupInfo.cb := Sizeof(StartupInfo);<br> &nbsp;StartupInfo.dwFlags := STARTF_USESHOWWINDOW;<br> &nbsp;StartupInfo.wShowWindow := Visibility;<br> &nbsp;if not CreateProcess(nil,<br> &nbsp; &nbsp;zAppName, { pointer to command line string }<br> &nbsp; &nbsp;nil, { pointer to process security attributes }<br> &nbsp; &nbsp;nil, { pointer to thread security attributes }<br> &nbsp; &nbsp;false, { handle inheritance flag }<br> &nbsp; &nbsp;CREATE_NEW_CONSOLE or { creation flags } &nbsp;NORMAL_PRIORITY_CLASS,<br> &nbsp; &nbsp;nil, { pointer to new environment block }<br> &nbsp; &nbsp;nil, { pointer to current directory name }<br> &nbsp; &nbsp;StartupInfo, { pointer to STARTUPINFO }<br> &nbsp; &nbsp;ProcessInfo)<br> &nbsp; &nbsp;then Result := 1 { pointer to PROCESS_INF }<br> &nbsp;else begin<br> &nbsp; &nbsp;while WaitforSingleObject(ProcessInfo.hProcess, 10) = WAIT_TIMEOUT<br> &nbsp; &nbsp; &nbsp;do Application.ProcessMessages;<br> &nbsp; &nbsp;GetExitCodeProcess(ProcessInfo.hProcess, Result);<br> &nbsp;end;<br>end;
 
好象是用HOOK吧
 
function WinExecAndWait32(FileName:String):Cardinal;<br>var<br> &nbsp;WorkDir:String;<br> &nbsp;StartupInfo:TStartupInfo;<br> &nbsp;ProcessInfo:TProcessInformation;<br>begin<br> &nbsp;WorkDir:=ExtractFilePath(FileName);<br> &nbsp;FillChar(StartupInfo,Sizeof(StartupInfo),#0);<br> &nbsp;StartupInfo.cb:=Sizeof(StartupInfo);<br> &nbsp;StartupInfo.dwFlags:=STARTF_USESHOWWINDOW;<br> &nbsp;StartupInfo.wShowWindow:=SW_SHOWNORMAL;<br> &nbsp;if not CreateProcess(nil,<br> &nbsp; &nbsp;PChar(FileName),<br> &nbsp; &nbsp;nil,<br> &nbsp; &nbsp;nil,<br> &nbsp; &nbsp;True,<br> &nbsp; &nbsp;CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS,<br> &nbsp; &nbsp;nil,<br> &nbsp; &nbsp;PChar(WorkDir),<br> &nbsp; &nbsp;StartupInfo,<br> &nbsp; &nbsp;ProcessInfo)<br> &nbsp; &nbsp;then Result := INFINITE {-1} else<br> &nbsp;begin<br> &nbsp; &nbsp;WaitforSingleObject(ProcessInfo.hProcess, INFINITE);<br> &nbsp; &nbsp;GetExitCodeProcess(ProcessInfo.hProcess, Result);<br> &nbsp; &nbsp;CloseHandle(ProcessInfo.hProcess); <br> &nbsp; &nbsp;CloseHandle(ProcessInfo.hThread);<br> &nbsp;end;<br>end;
 
多人接受答案了。
 

Similar threads

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