您的位置:首页 > 编程语言 > Delphi

Delphi打开外部程序或文件

2014-01-15 16:07 281 查看
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: