2011-08-15 50 views
2

下面的代碼是否正確,如果錯誤,請更正它。使用外部編輯器打開文件的正確代碼

注意:除非「Microsoft Office Word」是默認程序,否則我想用「WordPad.exe」而不是「Microsoft Office Word」打開文件。

我的代碼:

function InitializeSetup: Boolean; 
var 
    S: AnsiString; 
begin 
    // Show the contents of Readme.txt (non Unicode) in a message box 
    ExtractTemporaryFile('Info.rtf'); 
    Result := True; 
end; 

procedure AboutButtonOnClick(Sender: TObject); 
var 
    ErrorCode: Integer; 
begin 
    ShellExec('open', ExpandConstant('{tmp}\Info.rtf'), '', '', SW_SHOWNORMAL, ewNoWait, 
ErrorCode); 
end; 

回答

1

ShellExec('open','Documentname'....);將與該文件的擴展名關聯的程序中打開。如果沒有關聯的程序,它會提示您選擇要查看的程序。

您可以查找WordPad.exe,如果發現可以直接使用WordPad.EXE撥打ShellExec。然後傳遞documentName作爲參數。

更新與功能做到這一點

procedure OpenDocumentInWordPad(Document : String); 
var 
WordPad : String; 
ErrorCode : Integer; 
begin 
    // Typical Location on XP and later. 
    WordPad := ExpandConstant('{pf}') + '\Windows NT\Accessories\WordPad.exe' 
    // Find word pad 
    if Not FileExists(WordPad) then 
    begin 
    // Location in Windows 95/98 
    WordPad := ExpandConstant('{pf}') + '\Accessories\WordPad.exe' 
    if Not FileExists(WordPad) then 
    begin 
     // Fall back to anything associated with document. 
     WordPad := Document; 
     Document := ''; 
    end; 
    end; 

    if not ShellExec('open',WordPad,Document,'',SW_SHOW,ewNoWait,ErrorCode) then 
    begin 
     MsgBox(SysErrorMessage(ErrorCode),mbError,MB_OK); 
    end; 
end; 
+0

我明白你到底說了,但我不能寫的代碼傳遞documentName作爲參數。我用'WordPad.exe'替換'Documentname'並且打開寫字板,但是我不能讓寫字板打開臨時文件'Info.rtf' –

+0

ShellExec中的第三個參數是可以傳遞文檔名稱的參數。我更新了答案,以包含一個可以調用的函數,它將爲您做所有事情。 –

+0

謝謝你的代碼,但 我想讓你知道我究竟想要什麼圖片,也許你知道這個程序 http://oi53.tinypic.com/dc8z7p.jpg 我想使這個安裝文件(當安裝文件打開時,我可以通過AboutPad直接打開幫助文件 1.打開安裝文件 2.發送到臨時文件的文件「Info.rtf」 3.用戶可以通過寫字板打開文件通過關於按鈕 –

相關問題