在Form上使用 Hotkey,Button,ListBox控件各一個,作用:使用 Hotkey定義熱鍵,使用 Button將熱鍵添加到 ListBox中。
現在想問:如何將 ListBox中已經定義的多個熱鍵在 Form關閉時保存到 INI檔案中,並在下次打開程序時讓程序自動注冊所有已經保存的熱鍵呢?

解决方案 »

  1.   

    //参考如下代码~~
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, FileCtrl, ComCtrls;type
      TForm1 = class(TForm)
        HotKey1: THotKey;
        Button1: TButton;
        ListBox1: TListBox;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure ListBox1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
        procedure WMHOTKEY(var Msg: TWMHotKey); message WM_HOTKEY;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}uses Menus, IniFiles;procedure TForm1.Button1Click(Sender: TObject);
    var
      S: string;
      I: Integer;
    begin
      S := ShortCutToText(HotKey1.HotKey);
      if S = '' then Exit;
      I := ListBox1.Items.IndexOf(S);
      if I >= 0 then
        ListBox1.Items.Move(I, 0)
      else ListBox1.Items.Add(S);
    end;procedure TForm1.FormDestroy(Sender: TObject);
    var
      I: Integer;
    begin
      for I := 0 to ListBox1.Items.Count - 1 do
        UnregisterHotKey(Handle, I);  with TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'config.ini') do try
        EraseSection('HotKeys');
        for I := 0 to ListBox1.Count - 1 do
          WriteString('HotKeys', Format('Item%d', [I]), ListBox1.Items[I]);
      finally
        Free;
      end;
    end;function ShiftStateToWord(Shift: TShiftState): Word;
    begin
      Result := 0;
      if ssShift in Shift then Result := Result or MOD_SHIFT;
      if ssCtrl in Shift then Result := Result or MOD_CONTROL;
      if ssAlt in Shift then Result := Result or MOD_ALT;
    end;procedure TForm1.FormCreate(Sender: TObject);
    var
      I: Integer;
      S: string;
      vKey: Word;
      vShift: TShiftState;
    begin
      HotKey1.Enabled := False;
      ListBox1.Enabled := False;
      Button1.Enabled := False;
      Button1.Caption := 'Append';
      Button2.Caption := 'Edit';  with TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'config.ini') do try
        I := 0;
        S := ReadString('HotKeys', Format('Item%d', [I]), '');
        while S <> '' do begin
          ListBox1.AddItem(S, nil);
          ShortCutToKey(TextToShortCut(S), vKey, vShift);
          RegisterHotKey(Handle, I, ShiftStateToWord(vShift), vKey);
          Inc(I);
          S := ReadString('HotKeys', Format('Item%d', [I]), '');
        end;
      finally
        Free;
      end;
    end;procedure TForm1.ListBox1Click(Sender: TObject);
    begin
      if TListBox(Sender).ItemIndex < 0 then Exit;
      HotKey1.HotKey :=
        TextToShortCut(TListBox(Sender).Items[TListBox(Sender).ItemIndex]);
    end;procedure TForm1.WMHOTKEY(var Msg: TWMHotKey);
    begin
      if (Msg.HotKey >= 0) and (Msg.HotKey < ListBox1.Items.Count) then
        ShowMessage(ListBox1.Items[Msg.HotKey]);
    end;procedure TForm1.Button2Click(Sender: TObject);
    var
      I: Integer;
    begin
      for I := 0 to ListBox1.Items.Count - 1 do
        UnregisterHotKey(Handle, I);  HotKey1.Enabled := True;
      ListBox1.Enabled := True;
      Button1.Enabled := True;
      Button2.Enabled := False;
    end;end.