- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望在框架内有一个 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/
我希望在框架内有一个 KeyPreview 功能,我的意思是,当输入(例如,选择框架的一个控件,或者鼠标位于内部)位于框架中时(该框架将有多个面板)和其他控件),然后用户按下的键首先由框架处理。 有办
我和一位同事正在争论哪种方式对系统资源的负担较小。 (注意:这不是我想要答案的问题。相反,标题和下面粗体的行是我寻求答案的问题。) 1. Using KeyPreview to get the key
在 .Net 中有一个称为 KeyPreview 的功能。我怎样才能在 WinApi 中做到这一点。也许我不一定需要这个,但我的 hWnd 有 WM_KEYDOWN,但是当我的文本框有焦点时它不会收到
我正在考虑为我的一些应用程序从 WinForms 转向 WPF,目前我正在研究组合的条形码阅读器/文本输入程序(医疗保健患者表格)。 为了能够处理条形码字符,我依赖 WinForms 中的 Keypr
我正在构建一个小型表单应用程序,我才刚刚开始。但我有这个问题:如果我将控件放入窗体,则不会触发 KeyDown 事件。我知道 KeyPreview属性,并将其设置为 true。但这没有帮助... :(
我想知道 Form.KeyPreview 属性到底有什么用?它为什么存在,我将其设置为 true 有什么“风险”?我想它一定有一些负面影响 - 否则它根本不应该存在(或者至少默认情况下是正确的)? 编
我正在编写一个 metro 应用程序,在用户开始输入时,焦点可以随时跳转到单个文本框。但我想不出如何在本地实现此功能,而不修改其他用户控件来检测按键按下并将焦点转移到文本框。 在 WinForms 中
在 VS Express 12 中重现的步骤: 创建一个新的 Windows 窗体应用程序项目 添加一个按钮 将表单 KeyPreview 设置为 true 向表单添加 keyDown 事件 只要按钮
我正在尝试扩展我的自定义 KeyboardView。有什么方法可以自定义 keyPreview 和 popUpKeyboard 外观而无需从头开始实现 KeyboardView 吗? 最佳答案 key
我有一个包含十几个按钮、单选按钮和只读编辑控件的 MFC 对话框。 我想知道用户何时在该对话框中按下 Ctrl+V,而不管哪个控件具有焦点。 如果这是 C#,我可以设置 KeyPreview 属性,我
我是一名优秀的程序员,十分优秀!