关于外部窗口关闭(和以往的不同)100分(100分)

  • 主题发起人 主题发起人 yizhenfeng
  • 开始时间 开始时间
Y

yizhenfeng

Unregistered / Unconfirmed
GUEST, unregistred user!
我用delphi编写了一个程序A,在A中我用winexec函数打开一个powerpoint文件,然后该幻灯片文件可以让用户自己运行,A程序开始记时,时间到了后自动关闭幻灯片文件。现在有个问题,如果时间没到,用户自己关闭幻灯片文件,这时就要求A程序自动关闭,如何拦截到powerpoint文件关闭的消息?该怎么做?请指教,是不是要用到钩子函数?最好能给出具体原程序,谢谢!<br>我的邮箱imjollyfeng@163.com <br>
 
Waiting till an application ends<br>WaitForSingleObject<br><br>If we ever need to run an external application and wait until it terminates, then we would have to do without ShellExecute and resort to more basic functions, like CreateProcess, WaitForSingleObject and CloseHandle.<br><br>uses Forms, Windows;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; proc_info: TProcessInformation;<br>&nbsp; startinfo: TStartupInfo;<br>&nbsp; ExitCode: longword;<br>begin<br>&nbsp; // Initialize the structures<br>&nbsp; FillChar(proc_info, sizeof(TProcessInformation), 0);<br>&nbsp; FillChar(startinfo, sizeof(TStartupInfo), 0);<br>&nbsp; startinfo.cb := sizeof(TStartupInfo);<br><br>&nbsp; // Attempts to create the process<br>&nbsp; if CreateProcess('c:/windows/notepad.exe', nil, nil,<br>&nbsp; &nbsp; &nbsp; nil, false, NORMAL_PRIORITY_CLASS, nil, nil,<br>&nbsp; &nbsp; &nbsp; &nbsp;startinfo, proc_info) &lt;&gt; False then begin<br>&nbsp; &nbsp; // The process has been successfully created<br>&nbsp; &nbsp; // No let's wait till it ends...<br>&nbsp; &nbsp; WaitForSingleObject(proc_info.hProcess, INFINITE);<br>&nbsp; &nbsp; // Process has finished. Now we should close it.<br>&nbsp; &nbsp; GetExitCodeProcess(proc_info.hProcess, ExitCode); &nbsp;// Optional<br>&nbsp; &nbsp; CloseHandle(proc_info.hThread);<br>&nbsp; &nbsp; CloseHandle(proc_info.hProcess);<br>&nbsp; &nbsp; Application.MessageBox(<br>&nbsp; &nbsp; &nbsp; PChar(Format('Notepad finished! (Exit code=%d)', [ExitCode])),<br>&nbsp; &nbsp; &nbsp; 'Info', MB_ICONINFORMATION);<br>&nbsp; end else begin<br>&nbsp; &nbsp; // Failure creating the process<br>&nbsp; &nbsp; Application.MessageBox('Couldn''t execute the '<br>&nbsp; &nbsp; &nbsp; + 'application', 'Error', MB_ICONEXCLAMATION);<br>&nbsp; end;//if<br>end;
 
uses Windows,SysUtils;<br>//////////////////////<br>function CreateWinProc(FileName:string;Visibility:Integer;Var AppExists : Boolean):TProcessInformation;<br>var<br>&nbsp; zAppName:array [0..512] of Char;<br>&nbsp; zCurdir:array [0..256] of Char;<br>&nbsp; WorkDir:string;<br>&nbsp; StartupInfo:TStartupInfo;<br>&nbsp; ProcessInfo:TProcessInformation;<br>begin<br>&nbsp; AppExists := FileExists(FileName) ;<br>&nbsp; StrPCopy(zAppName,FileName);<br>&nbsp; GetDir(0,WorkDir);<br>&nbsp; StrPCopy(zCurDir,WorkDir);<br>&nbsp; FillChar(StartupInfo,Sizeof(StartupInfo),#0);<br><br>&nbsp; StartupInfo.cb:=Sizeof(StartupInfo);<br>&nbsp; StartupInfo.dwFlags:=STARTF_USESHOWWINDOW;<br>&nbsp; StartupInfo.wShowWindow:=Visibility;<br>&nbsp; CreateProcess(<br>&nbsp; &nbsp; nil,<br>&nbsp; &nbsp; zAppName,<br>&nbsp; &nbsp; nil,<br>&nbsp; &nbsp; nil,<br>&nbsp; &nbsp; False,<br>&nbsp; &nbsp; CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS,<br>&nbsp; &nbsp; nil,<br>&nbsp; &nbsp; nil,<br>&nbsp; &nbsp; StartupInfo,<br>&nbsp; &nbsp; ProcessInfo);<br>&nbsp; Result:=ProcessInfo;<br>end;<br><br>调用方法:<br>procedure Login ;<br>var<br>&nbsp; &nbsp;ProcessInfo:TProcessInformation;<br>&nbsp; &nbsp;ExtCode : Cardinal ;<br>&nbsp; &nbsp;AppExists : Boolean ;<br>begin<br>&nbsp; &nbsp;ProcessInfo := CreateWinProc('dl.exe' ,0 ,AppExists) ;<br>&nbsp; &nbsp;Log := AppExists ;<br>&nbsp; &nbsp;if not AppExists then begin<br>&nbsp; &nbsp; &nbsp;Application.MessageBox('系统在当前目录找不到登录程序[DL.EXE]!' ,'登录失败!' ,MB_ICONINFORMATION) ;<br>&nbsp; &nbsp; &nbsp;Exit ;<br>&nbsp; &nbsp;end ;<br>&nbsp; &nbsp;GetExitCodeProcess(ProcessInfo.hProcess ,ExtCode) ;<br>&nbsp; &nbsp;while &nbsp;ExtCode &lt;&gt; 0 do//这里就是等待了<br>&nbsp; &nbsp; &nbsp;GetExitCodeProcess(ProcessInfo.hProcess ,ExtCode) ;<br>end ;
 
wjh_wy你好:你的方法我试过了,但用createprocess打开一个exe文件可以,在我的程序中用它打开指定的PPT文件就出错,这是为什么?
 
把CreateProcess换成下面的:<br>CreateProcess(<br>&nbsp; &nbsp; nil,<br>&nbsp; &nbsp; PChar('powerpnt.exe 你要打开的文档'),<br>&nbsp; &nbsp; nil,<br>&nbsp; &nbsp; nil,<br>&nbsp; &nbsp; False,<br>&nbsp; &nbsp; CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS,<br>&nbsp; &nbsp; nil,<br>&nbsp; &nbsp; nil,<br>&nbsp; &nbsp; StartupInfo,<br>&nbsp; &nbsp; ProcessInfo);
 
如果我的程序是“流程.ppt”那么是不是改成这样就可以了?<br>CreateProcess(<br>&nbsp; &nbsp; nil,<br>&nbsp; &nbsp; PChar('powerpnt.exe 流程'),<br>&nbsp; &nbsp; nil,<br>&nbsp; &nbsp; nil,<br>&nbsp; &nbsp; False,<br>&nbsp; &nbsp; CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS,<br>&nbsp; &nbsp; nil,<br>&nbsp; &nbsp; nil,<br>&nbsp; &nbsp; StartupInfo,<br>&nbsp; &nbsp; ProcessInfo);
 
接受答案了.
 
后退
顶部