我有一个从主窗体打开非模式窗体的应用程序。非模态表单上有一个TMemo。主窗体菜单使用"space“作为其快捷键字符之一。
当非模态窗体打开并且备忘录具有焦点时,每次我尝试在非模态窗体的备忘录中输入空格时," space“快捷方式的主窗体事件都会触发!
我尝试在另一个表单打开但没有骰子的情况下将MainForm.KeyPreview :=设置为假。
有什么想法吗?
发布于 2010-04-21 20:48:46
在备忘录具有焦点时禁用主窗体上的菜单项,并在备忘录失去该菜单项时重新启用它。您可以在TMemo.OnEnter和TMemo.OnExit事件中执行此操作。
procedure TOtherForm.Memo1Enter(Sender: TObject);
begin
if Application.MainForm is TYourMainForm then
TYourMainForm(Application.MainForm).MenuItemWithSpace. Enabled := False;
end;
procedure TOtherForm.Memo1Exit(Sender: TObject);
begin
if Application.MainForm is TYourMainForm then
TYourMainForm(Application.MainForm).MenuItemWithSpace. Enabled := True;
end;使用Application.MainForm和类型转换是为了防止在子窗体中的窗体变量名中硬编码。
发布于 2014-05-06 23:57:18
这可能是一个古老的话题,但我刚才也遇到了同样的问题,并寻找了合适的解决方案。你的话题提出了,但没有一个我想用的解决方案。
我的问题是:我有一个有很多快捷键(Backspace,Delete等)的主窗体和一个有编辑框的第二个窗体。编辑框未获取任何按键操作,这些操作由主窗体快捷方式处理。
我的解决方案是:设置子窗体OnShortCut,它将在快捷键被主窗体解释之前捕获它们:
procedure ChildForm.FormShortCut(var Msg: TWMKey; var Handled: Boolean);
begin
Handled := True;
Self.DefaultHandler(Msg);
end;这对我来说已经完成了,子窗体捕获快捷键并将它们作为公共键消息进行处理。编辑框可以按预期使用。
发布于 2021-10-03 00:23:44
如果您有许多菜单项或控件,可能很难威胁到每个菜单项或控件的问题。相反,您可以在main window FormActivate()和FormDeActivate()方法中使用函数,以便以一种简单而简单的方式清理和恢复所有快捷方式:
var sstore : TStrings;
procedure Tmain_form.FormActivate(Sender: TObject);
begin
if (sstore <> NIL) then tratta_shortcuts_menu(main_menu, {read_shortcuts}FALSE, sstore)
end;
procedure Tmain_form.FormDeactivate(Sender: TObject);
begin
tratta_shortcuts_menu(main_menu, {read_shortcuts}TRUE, sstore)
end;
procedure tratta_shortcuts_menu(menu : TMainMenu;bo_read_shortcuts : boolean;var sstore : TStrings);
{ if BO_READ_SHORTCUTS then 1) read shortcuts 2) save them on SSTORE (Shortcuts STORE) 3) delete them from menu;
ELSE restore all shortcuts from SSTORE }
procedure sostituisci(im : TMenuItem);
const INDICATORE_SHORTCUT = '~ ';
begin
if bo_read_shortcuts then begin
if (im.ShortCut <> 0) then begin
sstore.Add(im.name);
sstore.Add(INDICATORE_SHORTCUT + menus.ShortCutToText(im.ShortCut));
im.ShortCut := 0
end
end
else begin
var i : smallint := sstore.indexof(im.Name);
if (i <> -1) then begin
im.ShortCut := menus.TextToShortCut(copy(sstore[i + 1], length(INDICATORE_SHORTCUT) + 1, MAXINT))
end
end
end;
procedure tratta(im : TMenuItem);
begin
sostituisci(im);
for var i : smallint := 0 to im.Count-1 do tratta(im.Items[i])
end;
begin
if (menu = NIL) then exit;
if bo_read_shortcuts then begin if (sstore = NIL) then sstore := TStringList.Create else sstore.Clear end;
for var i : smallint := 0 to menu.Items.Count-1 do tratta(menu.Items[i]);
if NOT bo_read_shortcuts then begin sstore.Free;sstore := NIL end
end;https://stackoverflow.com/questions/2680599
复制相似问题