gpt4 book ai didi

delphi - 使用框架时有没有办法拥有类似 KeyPreview 的功能?

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

我希望在框架内有一个 KeyPreview 功能,我的意思是,当输入(例如,选择框架的一个控件,或者鼠标位于内部)位于框架中时(该框架将有多个面板)和其他控件),然后用户按下的键首先由框架处理。

有办法做到这一点吗?我在TFrame中没有找到类似KeyPreview的属性。

尽管我主要使用 C++Builder,但我正在使用 RAD Studio 的 XE5 版本。

最佳答案

感谢我最近的"When does a ShortCut fire" -调查,我已经为您的框架制定了一个独立的解决方案。

简而言之:所有关键消息都输入事件控件的 TWinControl.CNKeyDwon 中。该方法调用TWinControl.IsMenuKey,它会遍历所有父级,同时确定消息是否为快捷方式。这是通过调用其 GetPopupMenu.IsShortCut 方法来实现的。如果框架的 GetPopupMenu 方法不存在,我会通过创建一个方法来覆盖该方法。请注意,您始终可以自己将 PopupMenu 添加到 Frame 中。通过子类化 TPopupMenu 并重写 IsShortCut 方法,调用 Frame 的 KeyDown 方法,该方法充当您需要的 KeyPreview 功能。 (我也可以分配 OnKeyDdown 事件处理程序)。

unit Unit2;

interface

uses
Winapi.Messages, System.Classes, Vcl.Controls, Vcl.Forms, Vcl.Menus,
Vcl.StdCtrls;

type
TPopupMenu = class(Vcl.Menus.TPopupMenu)
public
function IsShortCut(var Message: TWMKey): Boolean; override;
end;

TFrame2 = class(TFrame)
Label1: TLabel;
Edit1: TEdit;
private
FPreviewPopup: TPopupMenu;
protected
function GetPopupMenu: Vcl.Menus.TPopupMenu; override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
end;

implementation

{$R *.dfm}

{ TPopupMenu }

function TPopupMenu.IsShortCut(var Message: TWMKey): Boolean;
var
ShiftState: TShiftState;
begin
ShiftState := KeyDataToShiftState(Message.KeyData);
TFrame2(Owner).KeyDown(Message.CharCode, ShiftState);
Result := Message.CharCode = 0;
if not Result then
Result := inherited IsShortCut(Message);
end;

{ TFrame2 }

function TFrame2.GetPopupMenu: Vcl.Menus.TPopupMenu;
begin
Result := inherited GetPopUpMenu;
if Result = nil then
begin
if FPreviewPopup = nil then
FPreviewPopup := TPopupMenu.Create(Self);
Result := FPreviewPopup;
end;
end;

procedure TFrame2.KeyDown(var Key: Word; Shift: TShiftState);
begin
if (Key = Ord('X')) and (ssCtrl in Shift) then
begin
Label1.Caption := 'OH NO, DON''T DO THAT!';
Key := 0;
end;
end;

end.

关于delphi - 使用框架时有没有办法拥有类似 KeyPreview 的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27462585/

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