2011-08-15 47 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.用户可以通过写字板打开文件通过关于按钮 –

相关问题