Delphi打开外部程序或文件


本文整理自网络,侵删。

 WinAPI: ShellExecute - 打开外部程序或文件
ShellExecute(
  hWnd: HWND;        {指定父窗口句柄}
  Operation: PChar;  {指定动作, 譬如: open、print}
  FileName: PChar;   {指定要打开的文件或程序}
  Parameters: PChar; {给要打开的程序指定参数; 如果打开的是文件这里应该是 nil}
  Directory: PChar;  {缺省目录}
  ShowCmd: Integer   {打开选项}
): HINST;            {执行成功会返回应用程序句柄; 如果这个值 <= 32, 表示执行错误}
//返回值可能的错误有:
                       = 0   {内存不足}
ERROR_FILE_NOT_FOUND   = 2;  {文件名错误}
ERROR_PATH_NOT_FOUND   = 3;  {路径名错误}
ERROR_BAD_FORMAT       = 11; {EXE 文件无效}
SE_ERR_SHARE           = 26; {发生共享错误}
SE_ERR_ASSOCINCOMPLETE = 27; {文件名不完全或无效}
SE_ERR_DDETIMEOUT      = 28; {超时}
SE_ERR_DDEFAIL         = 29; {DDE 事务失败}
SE_ERR_DDEBUSY         = 30; {正在处理其他 DDE 事务而不能完成该 DDE 事务}
SE_ERR_NOASSOC         = 31; {没有相关联的应用程序}
//ShowCmd 参数可选值:
SW_HIDE            = 0;  {隐藏}
SW_SHOWNORMAL      = 1;  {用最近的大小和位置显示, 激活}
SW_NORMAL          = 1;  {同 SW_SHOWNORMAL}
SW_SHOWMINIMIZED   = 2;  {最小化, 激活}
SW_SHOWMAXIMIZED   = 3;  {最大化, 激活}
SW_MAXIMIZE        = 3;  {同 SW_SHOWMAXIMIZED}
SW_SHOWNOACTIVATE  = 4;  {用最近的大小和位置显示, 不激活}
SW_SHOW            = 5;  {同 SW_SHOWNORMAL}
SW_MINIMIZE        = 6;  {最小化, 不激活}
SW_SHOWMINNOACTIVE = 7;  {同 SW_MINIMIZE}
SW_SHOWNA          = 8;  {同 SW_SHOWNOACTIVATE}
SW_RESTORE         = 9;  {同 SW_SHOWNORMAL}
SW_SHOWDEFAULT     = 10; {同 SW_SHOWNORMAL}
SW_MAX             = 10; {同 SW_SHOWNORMAL}
--------------------------------------------------------------------------------
//举例说明更多问题(别忘了 uses ShellAPI;):
{譬如用记事本打开一个文件}
begin
  ShellExecute(Handle, 'open', 'notepad.exe', 'C:\WINDOWS\SchedLgU.Txt', nil, SW_SHOWNORMAL);
end;
--------------------------------------------------------------------------------
{第一个参数是用来当作错误提示窗口的父窗口的, 不能是 nil, 可以是 0(也就是桌面窗口)}
begin
  ShellExecute(0, 'open', 'notepad.exe', 'C:\WINDOWS\SchedLgU.Txt', nil, SW_SHOWNORMAL);
end;
--------------------------------------------------------------------------------
{第二个参数如果是 nil, 也会默认位 open}
begin
  ShellExecute(0, nil, 'notepad.exe', 'C:\WINDOWS\SchedLgU.Txt', nil, SW_SHOWNORMAL);
end;
--------------------------------------------------------------------------------
{文件路径可以放在参数五}
begin
  ShellExecute(0, nil, 'notepad.exe', 'SchedLgU.Txt', 'C:\WINDOWS', SW_SHOWNORMAL);
end;
--------------------------------------------------------------------------------
{把参数三直接指定为要打开的文件, 文件将用对应默认程序打开; 次数参数四应为 nil}
begin
  ShellExecute(0, nil, 'SchedLgU.Txt', nil, 'C:\WINDOWS', SW_SHOWNORMAL);
end;
--------------------------------------------------------------------------------
{如果文件在: 程序目录/当前目录/System32/Windows/PATH环境变量中, 参数五也可以 nil}
begin
  ShellExecute(0, nil, 'SchedLgU.Txt', nil, nil, SW_SHOWNORMAL);
end;
--------------------------------------------------------------------------------
{如果参数三是个文件, 可以用参数二命令打印}
begin
  ShellExecute(0, 'print', 'SchedLgU.Txt', nil, nil, 1);
end;
--------------------------------------------------------------------------------
{用 IE 打开网页}
begin
  ShellExecute(Handle, 'open', 'IExplore.EXE', 'about:blank', nil, SW_SHOWNORMAL);
end;
--------------------------------------------------------------------------------
{用火狐打开网页}
begin
  ShellExecute(Handle, 'open', 'firefox.exe', 'about:blank', nil, SW_SHOWNORMAL);
end;
--------------------------------------------------------------------------------
{用默认浏览器打开网页}
begin
  ShellExecute(Handle, 'open', 'Explorer.exe', 'about:blank', nil, SW_SHOWNORMAL);
end;
--------------------------------------------------------------------------------
{还是用默认浏览器打开网页}
begin
  ShellExecute(0, nil, 'http://del.cnblogs.com', nil, nil, 1);
end;
 
Delphi技巧集六 (等待执行完一个外部程序再执行另一个程序)
Posted on 2008-08-10 23:20 清枫&明月 阅读(80) 评论(0)  编辑 收藏 网摘 所属分类: Delphi编程资料 
unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
uses ShellAPI;  //注意
{$R *.dfm}
function ExecAppWait(AppName, Params: string): Boolean;
var
  ShellExInfo: TShellExecuteInfo;
begin
  FillChar(ShellExInfo, SizeOf(ShellExInfo), 0);
  with ShellExInfo do begin
    cbSize := SizeOf(ShellExInfo);
    fMask := see_Mask_NoCloseProcess;
    Wnd := Application.Handle;
    lpFile := PChar(AppName);
    lpParameters := PChar(Params);
    nShow := sw_ShowNormal;
  end;
  Result := ShellExecuteEx(@ShellExInfo);
  if Result then
    while WaitForSingleObject(ShellExInfo.HProcess, 100) = WAIT_TIMEOUT do
    begin
      Application.ProcessMessages;
      if Application.Terminated then Break;
    end;
end;
procedure TForm1.Button1Click(Sender: TObject);
const    { 连续运行下面这四个EXE文件 }
  EXEFILES : array[1..4] of string =
             ('calc.exe', 'mspaint.exe', 'Notepad.exe', 'wordpad.exe');
var
  Success: Boolean;
  InstanceID: THandle;
  I : integer;
begin
  for I := Low(EXEFILES) to High(EXEFILES) do
    begin
    Application.Minimize;
    Success := False;
    try
       Success := ExecAppWait(EXEFILES[I], '')
    finally
      Application.Restore;
      if not Success then
        ShowMessage(Format('Application %d failed: %s', [ I, EXEFILES[I] ]));
    end;
  end;
end;
end.

相关阅读 >>

Delphi 查看dpr文件

Delphi单元文件基本结构

Delphi获取我的文档路径

Delphi 判断网络链接文件是否存在

Delphi 通过http获取软件版本

delph控制台(console)程序添加图标和版权信息

Delphi url 中文编解码

Delphi not 与整数

Delphi中ocx的动态注册方法

Delphi将图片转换成base64编码函数

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



打赏

取消

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

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

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

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

评论

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