如何在Delphi中呼叫或執行外部程式或批次檔? |
尚未結案
|
AnnieKuo
一般會員 發表:5 回覆:6 積分:7 註冊:2002-11-05 發送簡訊給我 |
|
hagar
版主 發表:143 回覆:4056 積分:4445 註冊:2002-04-14 發送簡訊給我 |
|
cmf
尊榮會員 發表:84 回覆:918 積分:1032 註冊:2002-06-26 發送簡訊給我 |
引言: 請問各位先進: 如何在Delphi程式中,呼叫或執行外部程式或批次檔? >>< face="Verdana, Arial, Helvetica"> HINSTANCE ShellExecute( HWND hwnd, // handle to parent window LPCTSTR lpOperation, // pointer to string that specifies operation to perform LPCTSTR lpFile, // pointer to filename or folder name string LPCTSTR lpParameters, // pointer to string that specifies executable-file parameters LPCTSTR lpDirectory, // pointer to string that specifies default directory INT nShowCmd // whether file is shown when opened );
------
︿︿ |
banson1716
高階會員 發表:55 回覆:182 積分:167 註冊:2002-04-14 發送簡訊給我 |
|
banson1716
高階會員 發表:55 回覆:182 積分:167 註冊:2002-04-14 發送簡訊給我 |
uses
ShellApi; { Start notepad } ShellExecute(Handle, 'open', 'notepad.exe', '', nil, SW_SHOW); WinExec('C:\Windows\notepad.exe', SW_SHOW); { Start notepad and load a file } ShellExecute(Handle, 'open', 'notepad', 'c:\MyFile.txt', nil, SW_SHOW); { Open a txt file } ShellExecute(Handle, 'open', 'c:\Readme.txt', nil, nil, SW_SHOW); { Calling "Dir" from the DOS-Prompt and redirect the output to a file } {1. With Winexec } procedure ExecuteShellCommand(cmdline: string; hidden: Boolean);
const
flags: array [Boolean] of Integer = (SW_SHOWNORMAL, SW_HIDE);
var
cmdbuffer: array [0..MAX_PATH] of Char;
begin
GetEnvironmentVariable('COMSPEC', cmdBUffer, SizeOf(cmdBuffer));
StrCat(cmdbuffer, ' /C ');
StrPCopy(StrEnd(cmdbuffer), cmdline);
WinExec(cmdbuffer, flags[hidden]);
end; procedure TForm1.Button1Click(Sender: TObject);
begin
ExecuteShellCommand('dir C:\ > c:\temp\dirlist.txt', True);
end; {2. With Shellexecute } procedure ExecuteShellCommand(cmdline: string; hidden: Boolean);
const
flags: array[Boolean] of Integer = (SW_SHOWNORMAL, SW_HIDE);
var
cmdbuffer: array[0..MAX_PATH] of Char;
begin
GetEnvironmentVariable('COMSPEC', cmdBUffer, SizeOf(cmdBuffer));
ShellExecute(0,'open',cmdbuffer, PChar('/c' cmdline), nil, flags[hidden]);
end; procedure TForm1.Button1Click(Sender: TObject);
begin
ExecuteShellCommand('copy file1.txt file2.txt', True);
end;
|
banson1716
高階會員 發表:55 回覆:182 積分:167 註冊:2002-04-14 發送簡訊給我 |
打開一個外部程序: 我們常用的函數有兩個,WinExec(),ShellExecute(),因為ShellExecute(
)函數主要用於
Win32下,功能也強大一些,故而本文隻對後者進行討論。 首先,我們粗略地研究一下ShellExecute()的幾個參數:
hwnd:窗體的句柄;
lpOperation:打開程序執行的操作,共預留有“open”、“explore”、“prin
t”三種方式,此
參數可以省略,此時將依據打開的文件(lpFile)的類型執行相應的操作,比如
:如果lpFile為一文本
文件,那麼將會在與該文件相關聯的程序中打開它;
lpFile:文件名;
lpParamerters:打開文件時所需的參數;
lpDirectory:文件名所在的路徑,當然,一般來說,在Windows中登“記記”過
的程序(如WinWord)
不必提供此參數;
nShowCmd:打開文件後程序窗體如何顯示。
該函數的詳細文檔請讀者朋友參閱相應幫助。 其次,我們來舉一個例子(以“記事本”為例):
procedure TForm1.OpenBtnClick(Sender:TObject);
begin
ShellExecute(handle,'open','notepad.exe',nil,nil,SW_ShowNormal);
end;
讀者朋友可以在你的Delphi環境下試試這段代碼,當然,由於使用Windows的API
函數,請加上
ShellAPI單元。
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |