怎么保存到INI,再次读取。。。再热键触发。。。
unit Unit4;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus,
Vcl.ComCtrls;
type
TForm4 = class(TForm)
HotKey1: THotKey;
function ShiftStateToWord(Shift: TShiftState): Word;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure HotKey1Change(Sender: TObject); //热键组合键分解
private
{ Private declarations }
var
aatom: ATOM;
Key, Shift: Word;
public
{ Public declarations }
procedure hotkey(var msg: TMessage); message WM_HOTKEY;//定义全局热键消息事件
public
{ Public declarations }
end;
var
Form4: TForm4;
implementation
{$R *.dfm}
function TForm4.ShiftStateToWord(Shift: TShiftState): Word;
begin
if ssShift in Shift then
Result := MOD_SHIFT;
if ssCtrl in Shift then
Result := Result or MOD_CONTROL;
if ssAlt in Shift then
Result := Result or MOD_ALT;
end;
procedure TForm4.FormClose(Sender: TObject; var Action: TCloseAction);
begin
UnregisterHotKey(Handle, aatom);
GlobalDeleteAtom(aatom);
end;
procedure TForm4.hotkey(var msg: TMessage);
begin
if (msg.LparamLo = Shift) and (msg.LParamHi = Key) then
begin
ShowMessage('调用成功!');
end;
end;
procedure TForm4.HotKey1Change(Sender: TObject);
var
T: TShiftState;
begin
if FindAtom('ZWXhotKey') = 0 then
begin
aatom := GlobalAddAtom('ZWXhotKey');
end;
ShortCutToKey(HotKey1.HotKey, Key, T);
Shift := ShiftStateToWord(T);
try
if RegisterHotKey(Handle, aatom - $C000, Shift, Key) then
begin
ShowMessage('注册成功');
end;
except
on e: Exception do
ShowMessage(e.Message);
end;
end;
end.
问题2:
貌似只有 Ctrl+Shift+X 才起作用
我想让ctrl shiftalt window 全起作用 找到一个https://wenku.baidu.com/view/404d0a45bb4ae45c3b3567ec102de2bd9605ded4.html怎么合二为一? 本帖最后由 风铃夜思雨 于 2022-6-29 00:55 编辑
uses menus, inifiles;
Type
TFrmMain = Class(TForm)
Procedure CmdSetClick(Sender: TObject);
Private
HotKeyID: Integer;
Procedure SetKey();
Procedure HotKeyDown(Var Msg: TWMHotKey); Message WM_HOTKEY; //热键消息
Public
End;
Procedure TFrmMain.HotKeyDown(Var Msg: TWMHotKey);
Begin
If (Msg.HotKey = HotKeyID) Then
begin
..................................省略
end;
End;
Procedure TFrmMain.SetKey();
Var Sc: TShortCut;
Modifiers, vk: Word;
Shift: TShiftState;
Str: String;
Begin
{注册热键}
HotKeyID := GlobalAddAtom('HotKeyID') - $C000;
With TIniFile.Create(Path + 'Config.ini') Do
Begin
Modifiers := 0;
vk := 0;
Str := ReadString('Config', 'CustomKey', 'Ctrl+F11');
If Str = '' Then Str := 'Ctrl+F11';
Sc := TextToShortCut(Str);
ShortCutToKey(Sc, vk, Shift);
If ssShift In Shift Then Modifiers := Modifiers Or MOD_SHIFT;
If ssCtrl In Shift Then Modifiers := Modifiers Or MOD_CONTROL;
If ssAlt In Shift Then Modifiers := Modifiers Or MOD_ALT;
Free;
End;
UnregisterHotKey(Handle, HotKeyID);
RegisterHotKey(Handle, HotKeyID, Modifiers, vk);
End;
Procedure TFrmMain.FormCreate(Sender: TObject);
Begin
SetKey; //注册热键
end;
Procedure TFrmMain.FormDestroy(Sender: TObject);
Begin
UnregisterHotKey(Handle, HotKeyID);
DeleteAtom(HotKeyID);
End;
Procedure TFrmMain.CmdSetClick(Sender: TObject);
Var F: TFrmSet;
Ini: TIniFile;
BB: Boolean;
Begin
F := TFrmSet.Create(self);
Ini := TIniFile.Create(Path + 'Config.ini');
Try
With F Do
Begin
//读到INI热键参数到 hotkey 控件中
HotKey.Hint := Ini.ReadString('Config', 'CustomKey', 'Ctrl+F11');
If HotKey.Hint = '' Then HotKey.Hint := 'Ctrl+F11';
HotKey.HotKey := TextToShortCut(HotKey.Hint);
HotKey.Hint := '';
If ShowModal = mrok Then
Begin
Ini.WriteString('Config', 'CustomKey', ShortCutToText(HotKey.HotKey)); //写入新的热键设置
SetKey; //生效新的热键
End;
End;
Finally
Ini.Free;
F.Free;
End;
End; 学习了,谢谢。 风铃夜思雨 发表于 2022-6-29 00:48
uses menus, inifiles;
Type
@风铃夜思雨
F: TFrmSet;这句不知在哪个uses 模块中?
冥界3大法王 发表于 2022-7-1 09:34
@风铃夜思雨
F: TFrmSet;这句不知在哪个uses 模块中?
软件的设置窗口,新建窗口, 命名 FrmSet, 丢一个HotKey控件(DELPHI自带的), 一个确定按键 命名 CmdSet 风铃夜思雨 发表于 2022-7-2 00:06
软件的设置窗口,新建窗口, 命名 FrmSet, 丢一个HotKey控件(DELPHI自带的), 一个确定按键 命名...
晕,原来是这么来的。。。{:301_1008:}{:301_973:}{:301_972:}
页:
[1]