- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我将 TRibbon 控件放置在不是应用程序 MainForm 的窗体上时,TRibbon 的操作(即剪切、粘贴)将始终在执行操作后将焦点返回到 MainForm。
即使保存 TRibbon 的 TForm 不是 MainForm 的子级,也会发生这种情况。
我使用的是 Windows 7 64 位、Embarcadero RAD Studio XE 版本 15.0.3953.35171。
我是否错误地使用了 TRibbon 控件,或者这是 TRibbon 的问题吗?
最佳答案
这显然是设计使然。来自“ribbonactnctrls.pas”的示例代码片段:
procedure TRibbonBaseButtonControl.Click;
begin
inherited;
SetFocus(Application.MainForm.Handle);
end;
如您所见,没有检查任何条件可以帮助我们避免接到电话。菜单项选择和按键处理程序中也有相同的代码。
我可能会修改注释焦点调用的源代码,并尝试查看是否有任何副作用。
作为替代方案,您可以在切换到主窗体后将焦点恢复到窗体。假设“ActionList1”是包含非主表单上的标准操作的 TActionList:
type
TForm2 = class(TForm)
..
procedure ActionList1Execute(Action: TBasicAction; var Handled: Boolean);
private
..
procedure TForm2.ActionList1Execute(Action: TBasicAction; var Handled: Boolean);
begin
PostMessage(Handle, WM_SETFOCUS, WPARAM(True), 0);
end;
但是,这将导致每次执行操作时主窗体都会短暂闪烁。如果您不希望这样,您可以更改设计,以便主窗体知道它何时获得不需要的焦点,并假装它没有获得焦点。
在单元 1 中:
const
UM_CANCELIGNOREFOCUS = WM_USER + 7;
type
TForm1 = class(TForm)
..
private
FIgnoreFocus: Boolean;
procedure UMCancelIgnoreFocus(var Msg: TMessage); message UM_CANCELIGNOREFOCUS;
procedure WMNCActivate(var Msg: TWMNCActivate); message WM_NCACTIVATE;
public
property IgnoreFocus: Boolean write FIgnoreFocus;
end;
...
uses Unit2;
procedure TForm1.WMNCActivate(var Msg: TWMNCActivate);
begin
Msg.Result := 0;
if not (Msg.Active and FIgnoreFocus) then
inherited;
end;
procedure TForm1.UMCancelIgnoreFocus(var Msg: TMessage);
begin
FIgnoreFocus := False;
TForm(Msg.WParam).SetFocus;
end;
在单元2中:
uses
unit1;
procedure TForm2.ActionList1Execute(Action: TBasicAction; var Handled: Boolean);
begin
Form1.IgnoreFocus := True;
PostMessage(Form1.Handle, UM_CANCELIGNOREFOCUS, NativeInt(Self), 0);
end;
但是,如果您没有在项目源代码中设置“MainFormOnTaskBar”,这还不够,因为这样主窗体不仅会获得焦点,而且会被带到前面。在这种情况下,两种形式都可以通过卡住其 z 顺序来响应不需要的焦点更改/激活。然后代码将变为针对unit1:
const
UM_CANCELIGNOREFOCUS = WM_USER + 7;
type
TForm1 = class(TForm)
..
private
FIgnoreFocus: Boolean;
procedure UMCancelIgnoreFocus(var Msg: TMessage); message UM_CANCELIGNOREFOCUS;
procedure WMNCActivate(var Msg: TWMNCActivate); message WM_NCACTIVATE;
procedure WMWindowPosChanging(var Msg: TWMWindowPosChanging);
message WM_WINDOWPOSCHANGING;
public
property IgnoreFocus: Boolean read FIgnoreFocus write FIgnoreFocus;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses Unit2;
procedure TForm1.WMNCActivate(var Msg: TWMNCActivate);
begin
Msg.Result := 0;
if not (Msg.Active and FIgnoreFocus) then
inherited;
end;
procedure TForm1.WMWindowPosChanging(var Msg: TWMWindowPosChanging);
begin
inherited;
if FIgnoreFocus then
Msg.WindowPos.flags := Msg.WindowPos.flags or SWP_NOZORDER;
end;
procedure TForm1.UMCancelIgnoreFocus(var Msg: TMessage);
begin
FIgnoreFocus := False;
TForm(Msg.WParam).SetFocus;
end;
对于单元2:
type
TForm2 = class(TForm)
..
procedure ActionList1Execute(Action: TBasicAction; var Handled: Boolean);
private
procedure WMWindowPosChanging(var Msg: TWMWindowPosChanging);
message WM_WINDOWPOSCHANGING;
public
end;
var
Form2: TForm2;
implementation
uses
unit1;
{$R *.dfm}
procedure TForm2.ActionList1Execute(Action: TBasicAction; var Handled: Boolean);
begin
Form1.IgnoreFocus := True;
PostMessage(Form1.Handle, UM_CANCELIGNOREFOCUS, NativeInt(Self), 0);
end;
procedure TForm2.WMWindowPosChanging(var Msg: TWMWindowPosChanging);
begin
inherited;
if Form1.IgnoreFocus then
Msg.WindowPos.flags := Msg.WindowPos.flags or SWP_NOZORDER;
end;
关于Delphi XE - TRibbon 操作始终将焦点发送到 MainForm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8403323/
当我将 RibbonPage 设置为 false 时,我还需要禁用顶部的选项卡。它必须呈灰色,并且用户不能导航到它。我可以使用 PageControl 做到这一点,但我找不到使用 RibbonPage
我在 64 位 Windows 计算机 (Win 7) 和 32 位 Windows 计算机 (XP) 上运行 Delphi 2010。 我正在尝试学习如何使用 TRibbon 控件。 我一直在遵循
我正在使用delphi 2009的TRibbon组件。根据应用程序状态,某些页面不相关。如何隐藏 TRibbon 页面? RibbonPage1.Visible:=false 并没有真正隐藏页面...
我正在使用 delphi 2009(更新 1、2、3、4)。我看到了一些很奇怪的东西。当我有一个带有大字形的大按钮时,按钮上的图像不在按钮中心!而不是居中,字形的左侧部分从按钮的中心开始。 一个线索是
当我将 TRibbon 控件放置在不是应用程序 MainForm 的窗体上时,TRibbon 的操作(即剪切、粘贴)将始终在执行操作后将焦点返回到 MainForm。 即使保存 TRibbon 的 T
我注意到,当我使用 Delphi 2010 附带的 TRibbon 控件时,它在我的 Windows 7 系统上完美运行。然而,该应用程序在具有“经典主题”的 Windows XP 系统上存在一些奇怪
我是一名优秀的程序员,十分优秀!