Delphi编程实现文件关联
---- 文件关联的目的是要实现某种扩展名的文件由相应的应用/doc/">程序来打开,对于编写的应用软件来说,由于一般都有设计者特定的扩展名的数据文件,软件编制者应在/doc/">程序中实现自动关联而不能由用户手工去实现。下面给出实现文件关联的实用/doc/">程序。
字串2
---- 实现文件关联的核心是对注册表的操作, 所有的文件关联都在HKEY—CLASSES— ROOT 下面, 要实现文件关联必须在HKEY—CLASSES—ROOT 中增加两个键值,一是和文件扩展名对应的类型说明,二是对这种类型文件的操作所需要执行的应用/doc/">程序。下面举例说明,本例实现将扩展名为.xyz的文件定义为“我自己的文件”类型,双击此种类型的文件时执行的打开功能使用记事簿/doc/">程序。 实现这一功能需要用到Delphi中的函数 RegCreateKey、RegSetValue。实现如下:
字串9
---- 在窗体中放置一个按钮,双击按钮写如下代码: 字串6
字串5
procedure TForm1.Button1Click(Sender: TObject); 字串2
var
lphKey: HKEY; 字串1
sKeyName: string; 字串4
sKeyValue: string;
字串5
begin
sKeyName := ′myfile′;
sKeyValue := ′我的/doc/">文档′; 字串3
RegCreateKey(HKEY—CLASSES—ROOT 字串7
pchar(sKeyName)
字串1
lphKey);
RegSetValue(lphKey 字串7
′′ 字串8
REG—SZ 字串3
字串1
pchar(sKeyValue) 字串3
0);
字串5
sKeyName := ′.xyz′; 字串5
sKeyValue := ′myfile′;
字串5
RegCreateKey(HKEY—CLASSES—ROOT
字串9
pchar(sKeyName) 字串3
lphKey);
RegSetValue(lphKey 字串8
′′
字串6
REG—SZ
字串3
pchar(sKeyValue) 字串7
0); 字串1
sKeyName := ′myfile′;
sKeyValue := ′c:\Windows\NotePad.exe %1′;
RegCreateKey(HKEY—CLASSES—ROOT
字串7
pchar(sKeyName)
字串2
lphKey); 字串5
RegSetValue(lphKey
′shell\open\command′
REG—SZ 字串9
字串8
pchar(sKeyValue)
字串9
MAX—PATH); 字串6
end; 字串5
---- 建立一新文件,使其扩展名为.xyz,然后双击它看是否是启动记事簿来打开的。 字串2


文章评论
共有 0位编程爱好者发表了评论 查看完整内容