zoukankan      html  css  js  c++  java
  • 使用 TRegistry 类[4]: 文件打开与文件图标的关联

    代码文件:
    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    
    type
      TForm1 = class(TForm)
        Memo1: TMemo;
        Button1: TButton;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    uses Registry, ShlObj;
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      Memo1.Align := alTop;
      Memo1.ScrollBars := ssBoth;
      Button1.Caption := '设置文件关联、图标关联';
      Button2.Caption := '取消设置';
    
      if (ParamStr(1) <> '') and FileExists(ParamStr(1)) then
        Memo1.Lines.LoadFromFile(ParamStr(1));
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      reg: TRegistry;
    begin
      reg := TRegistry.Create;
      reg.RootKey := HKEY_CLASSES_ROOT;
    
      {在注册表中添加我们虚拟的 .tst 格式, 并指定默认项的值是 TestFile}
      reg.OpenKey('.tst', True);
      reg.WriteString('', 'TestFile');
      reg.CloseKey;
    
      {文件打开关联}
      reg.OpenKey('TestFile\shell\open\command', True);
      reg.WriteString('', ParamStr(0) + ' %1');
      reg.CloseKey;
    
      {文件图标关联}
      reg.OpenKey('TestFile\DefaultIcon', True);
      reg.WriteString('', ParamStr(0) + ',0');
      reg.CloseKey;
    
      reg.Free;
    
      {刷新图标显示}
      SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);
    end;
    
    {清除上面的设置}
    procedure TForm1.Button2Click(Sender: TObject);
    var
      reg: TRegistry;
    begin
      reg := TRegistry.Create;
      reg.RootKey := HKEY_CLASSES_ROOT;
      reg.DeleteKey('.tst');
      reg.DeleteKey('TestFile');
      reg.Free;
      SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);
    end;
    
    end.
    
  • 相关阅读:
    模板复习
    [BZOJ4016][FJOI2014]最短路径树问题(dijkstra+点分治)
    Stirling数,Bell数,Catalan数,Bernoulli数
    [BZOJ2820]YY的GCD
    [BZOJ2154]Crash的数字表格
    [HAOI2011]Problem b&&[POI2007]Zap
    [BZOJ2588][SPOJ10628]Count on a tree
    [ONTAK2010]Peaks
    [HNOI2010]弹飞绵羊
    [HNOI2004]宠物收养所
  • 原文地址:https://www.cnblogs.com/del/p/1274892.html
Copyright © 2011-2022 走看看