请问:打开一个程序和关闭一个程序的API函数是什么?(50分)

  • 主题发起人 主题发起人 delpher2003
  • 开始时间 开始时间
D

delpher2003

Unregistered / Unconfirmed
GUEST, unregistred user!
或用Delphi怎样实现?
 
&nbsp;winexec('abc.exe', SW_SHOWNORMAL); //启动指定的可执行程序<br>
 
//关闭应用程序<br>procedure TMainForm.FormDestroy(Sender: TObject);<br>var<br>&nbsp; str: string;<br>&nbsp; hWndClose: HWnd; //存储指定的外部应用程序窗口句柄<br>begin<br>&nbsp; str := '应用程序窗口名';<br>&nbsp; &nbsp; &nbsp; if str &lt;&gt; '' then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; //根据窗口名查找要关闭的窗口句柄<br>&nbsp; &nbsp; &nbsp; &nbsp; hWndClose := FindWindow(nil, PChar(str));<br>&nbsp; &nbsp; &nbsp; &nbsp; if hWndClose &lt;&gt; 0 then //如果查找成功,则发送消息,关闭指定的窗口<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SendMessage(hWndClose, WM_CLOSE, 0, 0)<br>&nbsp; &nbsp; &nbsp; &nbsp; else //否则,给出提示信息<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowMessage('没找到指定的应用程序,所以无法关闭!');<br>&nbsp; &nbsp; &nbsp; end;<br>end;<br>
 
shellexecute();<br>具体请查看delphi的帮助
 
打开程序用ShellExecute更好,在USES段加入SHELLAPI,使用时如:<br>&nbsp; &nbsp;执行"c:/myapp/myapp.exe"参数为"-s"<br>&nbsp; &nbsp;ShellExecute(handle,'open','c:/myapp/myapp.exe','-s','',SW_SHOWNORMAL);<br>&nbsp; &nbsp;第一个参数为父窗口句柄;<br>&nbsp; &nbsp;第二个参数为打开方式(OPEN,PRINT两种);<br>&nbsp; &nbsp;第三个参数为执行文件全路径;<br>&nbsp; &nbsp;第四个参数为执行文件参数;<br>&nbsp; &nbsp;第五个参数为执行文件开始运行时的初始目录;<br>&nbsp; &nbsp;第六个参数为为执行文件运行方式(SW_HIDE,SW_MAXIMIZE,SW_MINIMIZE,<br>SW_RESTORE,SW_SHOW,SW_SHOWDEFAULT,SW_SHOWMAXIMIZED,SW_SHOWMINIMIZED,<br>SW_SHOWMINNOACTIVE,SW_SHOWNA,SW_SHOWNOACTIVATE,SW_SHOWNORMAL);<br>&nbsp; &nbsp;具体请看帮助。另外,如果为字符串参数,其中包含变量或运算的必需用PCHAR()函数<br>关闭程序<br>procedure TForm1.Button36Click(Sender: TObject);<br>const<br>&nbsp; PROCESS_TERMINATE=$0001;<br>var<br>&nbsp; ExeFileName: String;<br>&nbsp; ContinueLoop: BOOL;<br>&nbsp; FSnapshotHandle: THandle;<br>&nbsp; FProcessEntry32: TProcessEntry32;<br>begin<br>&nbsp; ExeFileName := 'program.exe';//你要结束的程序名称!<br>&nbsp; FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);<br>&nbsp; FProcessEntry32.dwSize := Sizeof(FProcessEntry32);<br>&nbsp; ContinueLoop := Process32First(FSnapshotHandle,FProcessEntry32);<br>&nbsp; while integer(ContinueLoop) &amp;lt;&amp;gt; 0 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;UpperCase(ExeFileName))<br>&nbsp; &nbsp; &nbsp;or (UpperCase(FProcessEntry32.szExeFile) =<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;UpperCase(ExeFileName))) then<br>&nbsp; &nbsp; &nbsp; TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FProcessEntry32.th32ProcessID), 0);<br>&nbsp; &nbsp; ContinueLoop := Process32Next(FSnapshotHandle,FProcessEntry32);<br>&nbsp; &nbsp; end;<br>end;
 
http://www.yesky.com/20010913/196451.shtml
 
如:<br>&nbsp; &nbsp; &nbsp; // 运行记事本。<br>&nbsp; &nbsp; &nbsp;WinExec('NotePad',SW_RESTORE);<br>&nbsp; &nbsp; &nbsp; &nbsp;// 获取“记事本”窗口句柄。<br>&nbsp; &nbsp; HWndCalc:= FindWindow(nil, '未定标题 - 记事本');<br>&nbsp; &nbsp; &nbsp; if HWndCalc &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendMessage(HWndCalc, WM_CLOSE, 0, 0);<br>
 
在Delphi打开外部文档<br>//================================<br>先引用(Uses)ShellApi<br><br>procedure TForm_main.dxBarButton41Click(Sender: TObject);<br>var<br>&nbsp; Sfilename : string;<br>begin<br>&nbsp; &nbsp; //取得文件路径<br>&nbsp; Sfilename := ExtractFilePath(paramstr(0)) + 'hr.chm' ;<br>&nbsp; &nbsp;// 调用Window的一个API函数(ShellExecute),最大化(SW_MAXIMIZE)打开hr.chm<br>&nbsp; ShellExecute(Handle,'open', PChar(Sfilename), '','',SW_MAXIMIZE);<br>end;
 
[:D][:D]<br>关闭就按三楼的做:——)
 
打开程序使用ShellExecute,具体用法见Delphi帮助或MSDN,例如:<br>ShellExecute(handle,'open','c:/a.exe','','',SW_SHOWNORMAL);<br>关闭程序使用:<br>SendMessage(handle, WM_CLOSE, 0, 0); &nbsp;//Handle 为窗口句柄
 
打开可用winexec / shellexecute / createprocess<br>关闭可用findwindow /EnumWindows找到handle,再sendmessage发送 wm_close给handle
 
后退
顶部