导航

心动吧DELPHI网络书

自发研究:须要多维思想而且要想不可能为可能的人才能做到

« 将WAV文件做到EXE文件的方法及注意事项获取同文件关联的图标 »

建立文件类型关联

uses Registry, ShlObj;

procedure TForm1.Button1Click(Sender: TObject);
const
  cMyExt = '.abc';
  cMyFileType = 'Project1.FileType';
var
  Reg: TRegistry;
begin
  Reg := TRegistry.Create;
  try
    // Set the root key to HKEY_CLASSES_ROOT
    Reg.RootKey := HKEY_CLASSES_ROOT;
    // Now open the key, with the possibility to create
    // the key if it doesn't exist.
    Reg.OpenKey(cMyExt, True);
    // Write my file type to it.
    // This adds HKEY_CLASSES_ROOT\.abc\(Default) = 'Project1.FileType'
    Reg.WriteString('', cMyFileType);
    Reg.CloseKey;
    // Now create an association for that file type
    Reg.OpenKey(cMyFileType, True);
    // This adds HKEY_CLASSES_ROOT\Project1.FileType\(Default)
    //   = 'Project1 File'
    // This is what you see in the file type description for
    // the a file's properties.
    Reg.WriteString('', 'Project1 File');
    Reg.CloseKey;
    // Now write the default icon for my file type
    // This adds HKEY_CLASSES_ROOT\Project1.FileType\DefaultIcon
    //  \(Default) = 'Application Dir\Project1.exe,0'
    Reg.OpenKey(cMyFileType + '\DefaultIcon', True);
    Reg.WriteString('', Application.ExeName + ',0');
    Reg.CloseKey;
    // Now write the open action in explorer
    Reg.OpenKey(cMyFileType + '\Shell\Open', True);
    Reg.WriteString('', '&Open');
    Reg.CloseKey;
    // Write what application to open it with
    // This adds HKEY_CLASSES_ROOT\Project1.FileType\Shell\Open\Command
    //  (Default) = '"Application Dir\Project1.exe" "%1"'
    // Your application must scan the command line parameters
    // to see what file was passed to it.
    Reg.OpenKey(cMyFileType + '\Shell\Open\Command', True);
    Reg.WriteString('', '"' + Application.ExeName + '" "%1"');
    Reg.CloseKey;
    // Finally, we want the Windows Explorer to realize we added
    // our file type by using the SHChangeNotify API.
    SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);
  finally
    Reg.Free;
  end;
end;
/////////////////////////////////////////////////////
procedure TForm1.Button1Click(Sender: TObject);
var
  R : TRegIniFile;
begin
  R := TRegIniFile.Create('');
  with R do begin
    RootKey := HKEY_CLASSES_ROOT;
    WriteString('.myext','','MyExt');
    WriteString('MyExt','','Some description of MyExt files');
    WriteString('MyExt\DefaultIcon','','C:\MyApp.Exe,0');
    WriteString('MyExt\Shell','','This_Is_Our_Default_Action');
    WriteString('MyExt\Shell\First_Action','','This is our first action');
    WriteString('MyExt\Shell\First_Action\command','',
        'C:\MyApp.Exe /LotsOfParamaters %1');
    WriteString('MyExt\Shell\This_Is_Our_Default_Action','',
       'This is our default action');
    WriteString('MyExt\Shell\This_Is_Our_Default_Action\command',
       '','C:\MyApp.Exe %1');
    WriteString('MyExt\Shell\Second_Action','','This is our second action');
    WriteString('MyExt\Shell\Second_Action\command',
       '','C:\MyApp.Exe /TonsOfParameters %1');
    Free;
  end;
end;
 

原创文章如转载,请注明:转载自心动吧DELPHI网络书 [ http://www.abcxd.com/delphi/ ]

本文链接地址:http://www.abcxd.com/delphi/abcxddelphi/delphiDIRAA/251.html

  • 相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热文排行使用

本年排行使用

本月排行使用

网站分类

搜索内容

最新评论及回复

最近发表

所属分类下的文章

日历

Copyright ⊙ 2004-2009 心动吧 UrL:ABCXD.CoM All RiGhts Reserved