啟動外部程式, 可以使用函數Winexec、ShellExecute和ShellExecuteEx。
函數ShellExecute 相對靈活簡單。示例如下:
1 啟動一個程式
ShellExecute(Handle,'open',PChar('c: estapp.exe'),nil,nil,SW_SHOW);
2 啟動記事本 (因為記事本在系統路徑下,所以不必寫完整的路徑名了):
ShellExecute(Handle, 'open', PChar('notepad'),nil, nil, SW_SHOW);
3 啟動記事本並載入一個純文字檔案:
ShellExecute(Handle, 'open', PChar('notepad'),PChar('c:/readme.txt'), nil, SW_SHOW);
4 使用記事本打開一個純文字檔案 (請確定*.txt文件被關聯到記事本):
ShellExecute(Handle, 'open', PChar('c:/readme.txt'),nil, nil, SW_SHOW);
5 使用默認流覽器打開網址:
ShellExecute(Handle, 'open', PChar('http://www.festra.com/'),nil, nil, SW_SHOW);
6 列印一個檔:
ShellExecute(Handle, 'print', PChar('c:/readme.txt'),nil, nil, SW_SHOW);
7 用Windows Explorer打開一個檔夾:
ShellExecute(Handle, 'explore', PChar('c:/windows)',nil, nil, SW_SHOW);
8 運行一個DOS命令並立即返回:
ShellExecute(Handle, 'open', PChar('command.com'),PChar('/c copy file1.txt file2.txt'), nil, SW_SHOW);
9 運行一個DOS命令並保持DOS視窗打開 ("stay in DOS"):
ShellExecute(Handle, 'open', PChar('command.com'),PChar('/k dir'), nil, SW_SHOW);