各位大侠,偶有疑问相求:我做了一个应用程序,可以打开、保存一类扩展名
为'.slib'的文件,现在我可以做到将这种文件同我的应用关联起来,但是,我想
在资源管理器里通过双击该文件,在激活我的应用程序的同时,将该文件的数据读出
,怎么做?

解决方案 »

  1.   

    MFC application 缺省就有这个功能
      

  2.   

    ShellExecute是api函数,我不做说明了,请查看帮助,下面是简单的说明
    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
       );
    作用:查找与指定文件关联在一起的程序的文件名
    HINSTANCE,非零表示成功,零表示失败。会设置GetLastErrorhwnd ----------- 
     HWND hwnd,指定一个窗口的句柄,有时候,windows程序有必要在创建自己的主窗口前显示一个消息框  lpOperation ----  String,指定字串“open”来打开lpFlie文档,或指定“Print”来打印它  lpFile ---------  String,想用关联程序打印或打开一个程序名或文件名  lpParameters ---  String,如lpszFlie是可执行文件,则这个字串包含传递给执行程序的参数  lpDirectory ----  String,想使用的完整路径  nShowCmd -------  Long,定义了如何显示启动程序的常数值。参考ShowWindow函数的nCmdShow参数新增word文档:
         调用word程序打开默认的文档
        ShellExecute(frmMain.Handle,nil,PChar('WINWORD.EXE'),nil,nil,SW_SHOWNORMAL);
      

  3.   

    注册表:
    [HKEY_CLASSES_ROOT\.slib]
    @ = "slibfile"
    [HKEY_CLASSES_ROOT\slibfile\shell]
    @= "open"
    [HKEY_CLASSES_ROOT\slibfile\shell\open]
    @ = "打开"
    [HKEY_CLASSES_ROOT\slibfile\shell\open\command]
    @ = 你的程序的文件名(包含完整路径) + "%1"
    以上的"@"都是指默认。
      

  4.   

    To PlainSong:
    还是不行,其中:'.slib'文件是我的应用自己定义的文件,应用得到文件后分别在Edit&Memo中显示该文件的内部数据,我按您的方法做的,可是死活读不出文件的数据,好急呀,还是要谢谢您!
      

  5.   

    是啊。
    我刚写了一个例子:
    //主程序(textview.dpr):
    program TextView;uses
      Forms,
      Registry,
      Windows,
      Main in 'Main.pas' {MainForm};{$R *.res}
    var
      AReg: TRegistry;
    begin
      AReg := TRegistry.Create;
      AReg.RootKey := HKEY_CLASSES_ROOT;
      AReg.OpenKey('\.text', True);
      AReg.WriteString('', 'mytextfile');
      AReg.CloseKey;
      AReg.OpenKey('\mytextfile\shell', True);
      AReg.WriteString('', 'open');
      AReg.OpenKey('open', True);
      AReg.WriteString('', '显示');
      AReg.OpenKey('command', True);
      AReg.WriteString('', '"' + Application.ExeName + '" "%1"');
      AReg.CloseKey;
      AReg.Free;
      Application.Initialize;
      Application.CreateForm(TMainForm, MainForm);
      if ParamCount >= 1 then
        MainForm.OpenFile(ParamStr(1));
      Application.Run;
    end.
    //Main.pas:
    unit Main;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TMainForm = class(TForm)
        edView: TMemo;
      private
        { Private declarations }
      public
        { Public declarations }
        procedure OpenFile(FileName: String);
      end;var
      MainForm: TMainForm;implementation{$R *.dfm}procedure TMainForm.OpenFile(FileName: String);
    begin
      edView.Lines.BeginUpdate;
      edView.Lines.LoadFromFile(FileName);
      edView.Lines.EndUpdate;
      Caption := FileName;
    end;end.
    //main.dfm
    object MainForm: TMainForm
      Left = 211
      Top = 107
      Width = 696
      Height = 480
      Caption = 'TextView'
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      OldCreateOrder = False
      DesignSize = (
        688
        453)
      PixelsPerInch = 96
      TextHeight = 13
      object edView: TMemo
        Left = 8
        Top = 8
        Width = 673
        Height = 441
        Anchors = [akLeft, akTop, akRight, akBottom]
        ParentColor = True
        ReadOnly = True
        ScrollBars = ssBoth
        TabOrder = 0
        WantTabs = True
        WordWrap = False
      end
    end
      

  6.   

    上面只是个简单的例子,是把.text文件和我的程序关联起来,并且有个假设:.text没有和任何程序关联。如果有的话,就会被我的程序冲掉。你可以在加上判断,让以前的关联依然存在,只是把缺省关联设为你的程序(HKCR\mytextfile\shell,@='open'的作用).
    如果要设置文件的显示图标的话,加上:
    [KHCR\mytextfile\DefaultIcon]
    @ = 图标文件名(*.exe, *.exe,*.dll,*.ocx……), 图标在文件中的索引(从0开始)