这个问题很难,但也可以说不难:)

taxi 2000-08-25 04:53:00
在windows中,当双击一个文本文件时,就会自动打开记事本,并装入这个文件,现在我编了一个文本编辑器,已经能够实现一半的功能,即双击一个文本文件时,windows会打开我做的文本编辑器,但是不会装入这个文件,如何实现自动装入这个文件的功能.
...全文
89 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
w102272 2000-08-28
  • 打赏
  • 举报
回复
关于参数处理,DELPHI有两个函数。
function paramcount 返回调用参数有几个。
function paramstr(index:integer):string;返回调用参数,
0代表应用程序,1代表第一个参数,依次类推。
所以你只需要在程序开始部分检查
if fileexists(params(1)) then begin
//检查是不是我的编辑器要处理的文件。
//用你的编辑器代码打开这个文件,就调用你的FILE OPEN菜单的代码就可以了。
end;
Nicrosoft 2000-08-25
  • 打赏
  • 举报
回复
Delphi有函数可以取得命令行参数的,我记得一个ParamCount可以返回参数个数,你查查help的相关函数就可以了。
taxi 2000-08-25
  • 打赏
  • 举报
回复
但是如何给程序加参数呢,以及如何读取该参数
w102272 2000-08-25
  • 打赏
  • 举报
回复
是这样,你需要注册一个文件关联,其实就是向WINDOWS注册表中写注册信息。
当你双击文件的时候,WINDOWS会根据文件的扩展名找注册表,如果找到对应的处理程序,
会启动你注册的编辑器,然后把双击的文件名作为参数传递过去,
这样你检测命令参数paramstr(1)就可以了。
处理命令行很简单,不说了。
关于注册自己的文件关联,可以使用我的函数,说白了很简单,就是注册表操作,
你也可以自己打开注册表看看文件扩展名是如何注册的。:

//向Windows系统注册一个文件扩展名关联
//例子:FS_FileSetAssociatedExeCuteable
// ('.test','实验文件','我的试验文件','text/test','mydelphi.exe',0);
//其中:FileType, FileDesc and MIMEType 是可以缺省不写的,用''代替.
//最后一个ICONINDEX是用你程序里头的那个图标来作为扩展文件的图标。是个0-N的整数
function FS_FileSetAssociatedExecutable(FileExt, Filetype, FileDesc, MIMEType, ExecutablePath: string;iconindex:word=0): Boolean;
var Reg: TRegistry;
begin
Result := False;
if (FileExt = '') or (ExecutablePath = '') then Exit;
if FileExt[1] <> '.' then FileExt := '.' + FileExt; // 'doc' -> '.doc'
if FileType = '' then FileType := Copy(FileExt, 2, Length(FileExt) - 1) + 'file';
Reg := TRegistry.Create;
try
Reg.RootKey := HKey_Classes_Root;
if not Reg.OpenKey(FileExt, True) then Exit;
Reg.WriteString('', FileType);
if MIMEType <> '' then
Reg.WriteString('Content Type', MIMEType); // Write MIMEType
Reg.CloseKey;

if not Reg.OpenKey(FileType, True) then Exit;
Reg.WriteString('', FileDesc); // write File Description
Reg.closekey;
if not Reg.OpenKey(FileType+'\'+'DefaultIcon', True) then Exit;
Reg.WriteString('', ExecutablePath+','+inttostr(iconindex));
Reg.closekey;
//if Pos('%1', ExecutablePath) = 0 then ExecutablePath := Trim(ExecutablePath) ;
// append the executable path with ' %1', ex. 'c:\winword.exe' -> 'c:\winword.exe %1'

if not Reg.OpenKey(FileType+'\shell\open\command', True) then Exit;
Reg.WriteString('', ExecutablePath+ ' %1'); // write the executable path
Reg.closekey;
finally
Reg.Free; // always free
end;
end;

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧