我想构建一个类似于功能区按键提示的菜单表单 - 你可以
- 按住 Alt,然后按下并释放 e。 G。 d,然后释放 Alt 以触发操作或
- 按下并释放Alt,然后按下并释放d以触发相同的操作
我从Hidden Main Menu in a delphi program automatically shown using Alt key获得灵感并提出了以下演示:
unit Unit1;
interface
uses
Windows,
Messages,
SysUtils,
Variants,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
ImgList,
StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
strict private
FShowKeyTips: Boolean;
procedure UpdateKeyTipState(AShowKeyTips: Boolean);
procedure WMExitMenuLoop(var Message: TMessage); message WM_EXITMENULOOP;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
ShellAPI,
Menus;
{ TForm1 }
constructor TForm1.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Label1.Caption := 'Dummy';
end;
destructor TForm1.Destroy;
begin
inherited Destroy;
end;
procedure TForm1.WMExitMenuLoop(var Message: TMessage);
begin
UpdateKeyTipState(False);
end;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
const
MAPVK_VK_TO_CHAR = 2;
// Adapted from dxBar.pas:
function IsTextCharForKeyTip(AKey: Word): Boolean;
var
ARes: UINT;
begin
ARes := MapVirtualKey(AKey, MAPVK_VK_TO_CHAR);
Result := ((ARes and $FFFF0000) = 0) and (Char(ARes) <> ' ') and (Char(ARes) in [#32..#255]);
end;
var
hk: string;
CheckKeyTips: Boolean;
begin
if (Key = VK_MENU) or (Key = VK_F10) then
begin
UpdateKeyTipState(True);
Exit;
end;
if FShowKeyTips then
CheckKeyTips := True
else
CheckKeyTips := Shift = [ssAlt];
if CheckKeyTips and IsTextCharForKeyTip(Key) then
begin
hk := Char(Key); // TODO: Handle analogouos to TdxBarItemLink.IsAccel?
if SameText(hk, 'd') then
begin
Caption := Caption + '+';
Key := 0;
Exit;
end;
end;
end;
procedure TForm1.UpdateKeyTipState(AShowKeyTips: Boolean);
begin
if FShowKeyTips = AShowKeyTips then
Exit;
FShowKeyTips := AShowKeyTips;
if AShowKeyTips then
Label1.Caption := 'Dummy (d)'
else
Label1.Caption := 'Dummy';
end;
end.
(创建一个标准的VCL应用程序,将Label1添加到Form1中,并将Unit1.pas的内容替换为上述内容。)
第一个项目符号点有效(在表单标题中添加+),但是我无法使第二个项目符号点工作。我找不到处理 d 的位置。我尝试了 WM_(SYS)KEYDOWN、CM_DIALOGCHAR 等,但没有成功。
有什么想法吗?
最佳答案
如documented Alt 键,当单独按下并释放时,“切换进入和退出菜单栏模式”。即使您的窗体没有窗口菜单,系统菜单也足以让系统将窗口放入模式菜单循环中。在此模式下,非加速器将生成 WM_MENUCHAR
消息:
Sent when a menu is active and the user presses a key that does not correspond to any mnemonic or accelerator key.
这是您要查找的消息,请读取 User
中的字符 field 。而且您不必跟踪 Alt 键,因为窗口处于模式菜单循环中意味着 Alt 键已被按下一次。否则,将生成按键消息而不是菜单字符消息。
请注意,如果您的表单没有系统菜单(在 BorderIcons
中取消选中 biSystemMenu
)和窗口菜单,则常规 WM_KEYDOWN
将发送您已经在处理的邮件。
关于delphi - 按键在菜单循环中丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49451207/