⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ajrecentkeys.pas

📁 是不是天天看見MS的注冊表看膩了
💻 PAS
字号:

          
unit ajRecentKeys;



interface

uses
  Menus, Classes, Windows, ajAlterMenu;

const
  // Paths
  cRegHKEY             = HKEY_CURRENT_USER;                               // Registry root.
  cRegPath             = 'Software\SoftSpot Software\Key Maker';          // Path for Key Maker.
  cRegRegistryBrowser  = '\Registry Browser';                             // Registry Browser Path.

type
  TAddDelete  = (tAdd, tDelete);

type
  TajRecentKeyClick = procedure (Keyname : string) of object;

type
  TajRecentKeys = class(TObject)
  private
    fMenuItem         : TMenuItem;
    fRegPath          : string;
    fRecentKeyList    : TStringList;
    fOnRecentKeyClick : TajRecentKeyClick;
    fAlterMenu        : TajAlterMenu;
  protected
    procedure AddMenuItem         (Keyname : string);
    procedure LoadRecentKeys;
    procedure SaveRecentKeys;
    procedure Click               (Sender : TObject);
  public
    constructor Create            (MenuItem : TMenuItem; AlterMenu : TajAlterMenu; RegPath : string);
    destructor  Destroy;          override;
    function    InList            (Value : string) : boolean;
    procedure   UpdateRecentKeys  (Keyname : string; AddDelete : TAddDelete);
    property    OnRecentKeyClick  : TajRecentKeyClick   read fOnRecentKeyClick  write fOnRecentKeyClick;
  end;


implementation

uses
  SysUtils;

const
  cRecentMax = 8;                                 // Maximum number of recent files and keys.

{--------------------------------------------------------------------------------------------------}

constructor TajRecentKeys.Create(MenuItem : TMenuItem; AlterMenu : TajAlterMenu; RegPath : string);
begin
  fMenuItem       := MenuItem;                    // Submenu item reference.
  fAlterMenu      := AlterMenu;                   // Save the Alter Menu reference.
  fRegPath        := RegPath;                     // Registry path.
  fRecentKeyList  := TStringList.Create;          // Create recent key list.
  LoadRecentKeys;                                 // Fill recent key list and create menus.
end; {constructor}

{--------------------------------------------------------------------------------------------------}

destructor TajRecentKeys.Destroy;
begin
  SaveRecentKeys;                                // Save the recent key list to the registry.
  fRecentKeyList.Free;                           // Free the recent key list.
end; {destructor}

{--------------------------------------------------------------------------------------------------}

procedure TajRecentKeys.AddMenuItem(Keyname : string);
var
  Menu  : TMenuItem;
begin
  Menu            := TMenuItem.Create(fMenuItem); // Create a new menu item.
  Menu.Caption    := Keyname;                     // Set its caption to that of the key name.
  fMenuItem.Add(Menu);                            // Add the menu to the existing submenu item.
  Menu.OnClick    := Click;                       // Set its click event handler.
  if (fAlterMenu <> nil) then                     // Connect the owner draw stuff.
    fAlterMenu.ConnectDrawItemEventHandlers;
end; {AddMenuItem}

{--------------------------------------------------------------------------------------------------}

procedure TajRecentKeys.Click(Sender : TObject);
var
  RecentKeyname : string;
begin
  with (Sender as TMenuItem) do                   // Extract the key name from the menu caption.
    RecentKeyname := Copy(Caption, succ(Pos(' ', Caption)), Length(Caption)-Pos(' ', Caption));
  if Assigned(fOnRecentKeyClick) then             // Pass the selected key name to the main
    fOnRecentKeyClick(RecentKeyname);             // event handler if it's been set.
end; {Click}

{--------------------------------------------------------------------------------------------------}

procedure TajRecentKeys.LoadRecentKeys;
var
  Key       : HKEY;
  lp1       : integer;
  BuffType  : DWORD;
  BuffChars : array[0..1023] of char;
  BuffSize	: DWORD;
begin
  fRecentKeyList.Clear;                           // Clear the recent keys list.

  while (fMenuItem.Count > 0) do                  // Delete all recent key menu items.
    fMenuItem.Items[pred(fMenuItem.Count)].Free;

  if (RegOpenKeyEx(cRegHKEY, PChar(cRegPath + fRegPath), 0, KEY_QUERY_VALUE, Key) = ERROR_SUCCESS) then begin
    lp1 := 0;                                     // Scan the registry for all recent keys
    while (lp1 < cRecentMax) do begin             // and add them to the list.
      BuffSize  := SizeOf(BuffChars);
      if (RegQueryValueEx(Key, PChar('Key' + IntToStr(lp1)), nil, @BuffType, @(BuffChars), @BuffSize) = ERROR_SUCCESS) then
        fRecentKeyList.Add(BuffChars);
      inc(lp1);
    end; {while}
    RegCloseKey(Key);
  end; {if}

  for lp1 := 0 to pred(fRecentKeyList.Count) do   // Create menu items for the recent key list.
    AddMenuItem(Format('&%d %s', [lp1, fRecentKeyList.Strings[lp1]]));
  fMenuItem.Visible := (fRecentKeyList.Count > 0);
end; {LoadRecentKeys}

{--------------------------------------------------------------------------------------------------}

procedure TajRecentKeys.SaveRecentKeys;
var
  Key       : HKEY;
  lp1, lp2  : integer;
  Buffer    : string;
  BuffSize	: DWORD;
begin
  if (RegCreateKeyEx(cRegHKEY, PChar(cRegPath + fRegPath), 0, nil, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nil, Key, nil) = ERROR_SUCCESS) then begin
    lp2 := 0;
    for lp1 := 0 to pred(fRecentKeyList.Count) do begin
      Buffer    := fRecentKeyList.Strings[lp1];
      BuffSize  := Length(Buffer);
      if (lp2 < cRecentMax) and (Buffer <> '') then begin                 // Save the recent key list to the registry.
        if (RegSetValueEx(Key, PChar('Key' + IntToStr(lp1)), 0, REG_SZ, @(Buffer[1]), BuffSize) = ERROR_SUCCESS) then
          inc(lp2);
      end; {if}
    end; {for}
    for lp1 := lp2 to pred(cRecentMax) do
      RegDeleteValue(Key, PChar('Key' + IntToStr(lp1)));
    RegCloseKey(Key);
  end; {if}
end; {SaveRecentKeys}

{--------------------------------------------------------------------------------------------------}

procedure TajRecentKeys.UpdateRecentKeys(Keyname : string; AddDelete : TAddDelete);
var
  Index : integer;
begin
  Index := fRecentKeyList.IndexOf(Keyname);       // Find it in the list.
  if (Index >= 0) then                            // If it's there then delete it from the list.
    fRecentKeyList.Delete(Index);                 //
  if (AddDelete = tAdd) then                      // Add it to the top of the list if not deleting it.
    fRecentKeyList.Insert(0, Keyname);            //
  SaveRecentKeys;                                 // Then save the list to the registry and reload
  LoadRecentKeys;                                 // it all again !
end; {UpdateRecentKeys}

{--------------------------------------------------------------------------------------------------}

function TajRecentKeys.InList(Value : string) : boolean;
begin
  Result  := (fRecentKeyList.IndexOf(Value) >= 0);
end; {InList}

{--------------------------------------------------------------------------------------------------}
{ajRecentKeys}
end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -