gpt4 book ai didi

delphi - 添加以编程方式添加的系统菜单选项的快捷方式

转载 作者:行者123 更新时间:2023-12-03 14:48:12 24 4
gpt4 key购买 nike

在我的应用程序中,我有一个基本表单,其中将各种项目添加到系统菜单中,例如

 AppendMenu (SysMenu, MF_SEPARATOR, 0, '');
AppendMenu (SysMenu, MF_STRING, SC_Sticky, 'Sticky');
AppendMenu (SysMenu, MF_STRING, SC_Original, 'Original');

如何向这些菜单选项添加键盘快捷键(例如 Alt-F2、Alt-F3)?

我无法使用使用加速器的标准方法(即 Alt-S 的 &Sticky),因为真正的菜单标题是希伯来语,而加速器似乎无法在这种语言下正常工作。

最佳答案

这是一个使用 accelerator table 的示例:

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, AppEvnts;

type
TForm1 = class(TForm)
ApplicationEvents1: TApplicationEvents;
procedure FormCreate(Sender: TObject);
procedure ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
procedure FormDestroy(Sender: TObject);
private
FAccelTable: HACCEL;
FAccels: array[0..1] of TAccel;
protected
procedure WMSysCommand(var Message: TWMSysCommand); message WM_SYSCOMMAND;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

const
SC_Sticky = 170;
SC_Original = 180;

procedure TForm1.FormCreate(Sender: TObject);
var
SysMenu: HMENU;
begin
SysMenu := GetSystemMenu(Handle, False);
AppendMenu (SysMenu, MF_SEPARATOR, 0, '');
AppendMenu (SysMenu, MF_STRING, SC_Sticky, 'Sticky'#9'Alt+F2');
AppendMenu (SysMenu, MF_STRING, SC_Original, 'Original'#9'Alt+F3');

FAccels[0].fVirt := FALT or FVIRTKEY;
FAccels[0].key := VK_F2;
FAccels[0].cmd := SC_Sticky;
FAccels[1].fVirt := FALT or FVIRTKEY;
FAccels[1].key := VK_F3;
FAccels[1].cmd := SC_Original;

FAccelTable := CreateAcceleratorTable(FAccels, 2);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
DestroyAcceleratorTable(FAccelTable);
end;

procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
begin
TranslateAccelerator(Handle, FAccelTable, Msg);
inherited;
end;

procedure TForm1.WMSysCommand(var Message: TWMSysCommand);
begin
inherited;
case Message.CmdType of
SC_Sticky: ShowMessage('sticky');
SC_Original: ShowMessage('original');
end;
end;

关于delphi - 添加以编程方式添加的系统菜单选项的快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12659300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com