使用Delphi启动和关闭外部应用程序


本文整理自网络,侵删。

 使用Delphi启动和关闭外部应用程序
━━━━━━━━━━━━━━━━━━━━━━━━━━
  ⑴ 要启动外部应用程序,可以通过调用api函数winexec来实现。该函数用于运行指定的应用程序。下面介绍一下该函数所需的参数和返回值:
uint winexec(
 lpcstr lpcmdline, file://命令行指针
 uint ucmdshow file://应用程序的窗口风格
);
  如果成功,返回值大于31。否则可能返回下列结果:
  0 系统内存或资源不足
  error_bad_format 该*.exe文件无效
  error_file_not_found 没找到指定的文件
  error_path_not_found 没找到指定路径
  ⑵ 通过编写标题为“启动外部应用程序”组件的onclick事件,来实现外部应用程序的启动,代码如下:
procedure tform1.button1click(sender: tobject);
var
str: string; file://存储指定的应用程序文件名begin
if opendialog1.execute then file://选择要调用的外部可执行程序
begin
str := opendialog1.filename; file://获取可执行文件名
winexec(pchar(str), sw_shownormal); file://启动指定的可执行程序
end;
end;
  3. 关闭已开启的外部应用程序
  ⑴ 通过调用两个api函数,可以实现该功能。这两个函数分别为:
  ① findwindow函数 该函数用于查找与指定的类名和窗口名相匹配的高层窗口,如果查找成功,返回非0值,否则返回0。
  ② sendmessage函数 此函数向一个或多个窗口发送指定的消息。在此通过发送wm_close消息来关闭指定的外部应用程序。
  ⑵ 通过编写标题为“关闭已开启的外部应用程序”组件的onclick事件,来关闭已开启的外部应用程序。代码如下:
procedure tform1.button2click(sender: tobject);
var
hwndclose: hwnd; file://存储指定的外部应用程序窗口句柄
str: string; file://存储指定的外部应用程序的窗口名
begin
str := inputbox(提示,请输入应用程序名:,); file://获取要关闭的应用程序窗口名
if str <> then begin
file://根据窗口名查找要关闭的窗口句柄
hwndclose := findwindow(nil, pchar(str));
if hwndclose <> 0 then file://如果查找成功,则发送消息,关闭指定的窗口
sendmessage(hwndclose,wm_close,0,0);
else file://否则,给出提示信息
showmessage(没找到指定的应用程序,所以无法关闭!);
end;
end;

相关阅读 >>

Delphi idsmtp发送邮件的问题

Delphi [函数] unicode 检查字符串是否含中文字

Delphi setlength 内存释放总结

Delphi调用外部程序并等待其运行结束

Delphi 将svg加载到timage控件

Delphi tcomport控件从串品读取数据

Delphi tadodataset 中文使用说明

Delphi 直接将html字符串读入webbrowser中

Delphi 系统长文件名与dos形式短文件名的相互转换

Delphi monthcalendar1 获取选中日期

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...