如何注册一个程序打开的文件类型(100分)

  • 主题发起人 主题发起人 wejc
  • 开始时间 开始时间
W

wejc

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在Delphi中注册一个程序打开的文件类型,这样双击该扩展名的文件
就可以启动该程序并打开该文件。
还有,就在系统中注册这个信息后,如何在程序中和打开文件的操作关联
起来。 谢谢。
 
转贴:来自hubdog的葵花宝典version 2.7

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;

 
这个小东东写得仓促,一点不好使,只是给你提供一些思路。
功能说明:双击窗体,选择文件播放;
实现双击文件/右键点击启动。
有了好东东,别忘了E-MAIL 给我。 }
unit Unit1;

interface

uses
Windows, Messages,Registry, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,shellapi, ExtCtrls, MPlayer;

type
TForm1 = class(TForm)
Panel1: TPanel;
OpenDialog1: TOpenDialog;
MediaPlayer1: TMediaPlayer;
procedure FormCreate(Sender: TObject);
procedure MediaPlayer1Click(Sender: TObject; Button: TMPBtnType;
var DoDefault: Boolean);
procedure MediaPlay(PlayFile:string);
procedure FormShow(Sender: TObject);
procedure Panel1DblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
const
cMyExe = '.avi'; //文件扩展名

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);

var
Reg: TRegistry;
begin
MediaPlayer1.FileName:=ExtractFilePath(paramstr(0))+'dapg.avi';
Reg := TRegistry.Create;
try
Reg.RootKey :=HKEY_CLASSES_ROOT;
// Reg.OpenKey(cMyExe+'/DefaultIcon', True);
// Reg.WriteString('', Application.ExeName + ',0');
reg.OpenKey(cMyExe,true);
reg.WriteString('','myfile');
Reg.CloseKey;
Reg.OpenKey('myfile/shell/open/command', True);
Reg.WriteString('',Application.ExeName + ' %1');
Reg.CloseKey;
finally
Reg.Free;
end;
end;


procedure TForm1.MediaPlayer1Click(Sender: TObject; Button: TMPBtnType;
var DoDefault: Boolean);
begin
try
if Button=btPlay then
begin
MediaPlay(mediaplayer1.FileName);
end;
except
showmessage('error');
end;

end;

procedure TForm1.MediaPlay(PlayFile: string);
begin
with mediaplayer1 do begin
FileName:=PlayFile;
AutoEnable:=true;
AutoRewind:=true;
Open;
Play;
end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin

if paramstr(1)<>'' then begin
MediaPlay(paramstr(1));

end;
end;

procedure TForm1.Panel1DblClick(Sender: TObject);
begin
try
if OpenDialog1.Execute then
MediaPlay(OpenDialog1.FileName);
except
showmessage('error');
end;
end;

end.
 
后退
顶部