我想构建一个类似于功能区键提示的菜单表单 - 您可以
我在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等等,但没有用。
有什么想法吗?
答案 0 :(得分:3)
作为documented Alt 键,当单独按下并释放时,&#34;切换进出菜单栏模式&#34;。即使您的表单没有窗口菜单也是如此,系统菜单足以让系统将窗口置于模态菜单循环中。在此模式下,非加速器将生成WM_MENUCHAR
消息:
当菜单处于活动状态且用户按下不显示的键时发送 对应于任何助记符或加速键。
这是您正在寻找的消息,请从User
字段中读取字符。并且您不必跟踪 Alt 键,因为窗口处于模态菜单循环中意味着 Alt 键已被按下一次。否则,将生成按键消息而不是菜单字符消息。
请注意,如果您的表单没有系统菜单(在BorderIcons
取消选中biSystemMenu
)和窗口菜单,则会发送您已经发送的常规WM_KEYDOWN
处理