Delphi打开外部程序或文件的方法与命令

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Delphi打开外部程序或文件的方法与命令
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;
--------------------------------------------------------------------------------
来源:(/s/blog_4b05f08e0100h5py.html) - Delphi打开外部程序或文件_漂忆_新浪博客
{用默认浏览器打开网页}
begin
ShellExecute(Handle, 'open', 'Explorer.exe', 'about:blank', nil, SW_SHOWNORMAL);
end;
--------------------------------------------------------------------------------
{还是用默认浏览器打开网页}
begin
ShellExecute(0, nil, '', 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}
functionExecAppWait(AppName, Params: string): Boolean;
var
ShellExInfo: TShellExecuteInfo;
begin
FillChar(ShellExInfo, SizeOf(ShellExInfo), 0);
withShellExInfo 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
whileWaitForSingleObject(ShellExInfo.HProcess, 100) = WAIT_TIMEOUT do begin
Application.ProcessMessages;
ifApplication.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.。

相关文档
最新文档