delphi - 在 Delphi 中如何判断哪个菜单项是打开的?

标签 delphi menu help-system

我正在 Delphi 2009 应用程序中实现上下文相关帮助。除了一种情况之外,它工作得很好。我无法确定我位于主菜单中,也无法确定已打开哪个菜单项。

我想要做的是,如果用户打开了"file"菜单,并且在打开时按 F1,那么我将在"file"菜单上显示帮助。如果他们打开“编辑”菜单并按 F1,那么我将在“编辑”菜单上显示我的帮助等。

我正在使用 ApplicationEventsHelp 处理用户按 F1 的操作,如下所示:

function MainForm.ApplicationEvents1Help(Command: Word; Data: Integer;
  var CallHelp: Boolean): Boolean;
begin
  if Command = HELP_COMMAND then begin
    Application.HelpSystem.ShowTopicHelp(PChar(Data), Application.CurrentHelpFile);
    CallHelp := false;
  end;
  Result := true;
end;

正如我提到的,这适用于除主菜单之外的所有内容。我尝试过使用

FindVCLWindow(Mouse.CursorPos)

以及其他此类方法来识别事件控件以查看它们是否会识别菜单,但它们似乎不会。

有没有办法知道按下 F1 键时打开的是哪个菜单项(如果有)?

<小时/>

感谢大家的帮助和好主意。

为了记录我的最终解决方案,我发现系统并不是特别擅长确定它所在的控件,有时会出错并将错误的数据传递给 ApplicationEventsHelp,从而显示不适当的帮助页面。

在尝试并使用处理已接受答案中的菜单的解决方案后,我发现最好确定我所在的控件以显示正确的帮助项目。我最终甚至没有使用 HelpKeyword 属性,而是对其进行了硬编码。代码很清晰并且可以工作。我的 RVEdit 窗口还提供了帮助,根据您所在文档的部分显示不同的帮助页面(我的 CurCursorID 告诉我这一点)。

对于任何想像我一样这样做的人,方法如下:

function TLogoAppForm.ApplicationEvents1Help(Command: Word; Data: Integer;
  var CallHelp: Boolean): Boolean;
var
  HelpKeyword: string;
  SType: string;

begin
  if Command = HELP_COMMAND then begin
    if PtInRect(RVEdit.ClientRect, RVEdit.ScreenToClient(Mouse.CursorPos)) then begin
      if CurCursorID = 'H' then HelpKeyword := 'RefTopReport'
      else if CurCursorID = 'T' then HelpKeyword := 'RefTableContents'
      else if CurCursorID = '~HNAME' then HelpKeyword := 'RefIndexNames'
      else if copy(CurCursorID, 1, 2) = 'N+' then HelpKeyword := 'RefIndexNames'
      else if CurCursorID = 'B' then HelpKeyword := 'RefBottomReport'
      else if CurCursorID <> '' then HelpKeyword := 'RefInformationArea'
      else HelpKeyword := 'RefEverythingReport';
      Application.HelpSystem.ShowTopicHelp(HelpKeyword, Application.CurrentHelpFile);
    end
    else if PtInRect(ElTree.ClientRect, ElTree.ScreenToClient(Mouse.CursorPos)) then
      Application.HelpSystem.ShowTopicHelp('RefTreeView', Application.CurrentHelpFile)
    else if PtInRect(TopToolbar.ClientRect, TopToolbar.ScreenToClient(Mouse.CursorPos)) then
      Application.HelpSystem.ShowTopicHelp('RefTopToolbar', Application.CurrentHelpFile)
    else if PtInRect(BottomToolbar.ClientRect, BottomToolbar.ScreenToClient(Mouse.CursorPos)) then
      Application.HelpSystem.ShowTopicHelp('RefBottomToolbar', Application.CurrentHelpFile)
    else
      Application.HelpSystem.ShowTopicHelp('RefMainWindow', Application.CurrentHelpFile);
    CallHelp := false;
  end
  else if Command = HELP_CONTEXTPOPUP then begin
    case Data of
      0: HelpKeyword := 'RefMenuBar';
      11: HelpKeyword := 'RefFileMenu';
      12: HelpKeyword := 'RefEditMenu';
      13: HelpKeyword := 'RefSearchMenu';
      14: HelpKeyword := 'RefNavigateMenu';
      15: HelpKeyword := 'RefViewMenu';
      16: HelpKeyword := 'RefOrganizeMenu';
      17: HelpKeyword := 'RefHelpMenu';
      else HelpKeyword := '';
    end;
    if HelpKeyword <> '' then begin
      Application.HelpSystem.ShowTopicHelp(HelpKeyword, Application.CurrentHelpFile);
      CallHelp := false;
    end;
  end;
  Result := true;
end;

我确实必须将 11 到 17 放入我的 7 个主菜单中 MenuItems 的 HelpContext 属性中,以便根据您所在的菜单显示正确的帮助。菜单项的检测就是帮助的答案这个问题给了我。

好处是此代码很容易理解(使用 HelpKeywords 而不是 HelpContext 数字),并且即使在转换为 Delphi XE 和 FireMonkey 后也可能仍然有效。

最佳答案

查看Command = HELP_COMMAND以及DataPChar的转换,看来您使用的是基于关键字的帮助系统,而不是关于上下文标识符(HelpType = htKeyword)。

(在 Delphi 7 中)菜单项没有 HelpTypeHelpKeyword 属性,因此您必须使用 HelpContext 属性:

function TForm1.ApplicationEvents1Help(Command: Word; Data: Integer;
  var CallHelp: Boolean): Boolean;
begin
  if Command = HELP_COMMAND then
  begin
    //Application.HelpSystem.ShowTopicHelp(PChar(Data), Application.CurrentHelpFile);
    //Doesn't this do the same?
    Application.HelpKeyword(PChar(Data));
    CallHelp := False;
  end
  else if Command = HELP_CONTEXT then
  begin
    // Convert the context identifier to your keyword, or:
    Application.HelpContext(Data);
    CallHelp := False;
  end;
  Result := True;
end;

关于delphi - 在 Delphi 中如何判断哪个菜单项是打开的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7857341/

相关文章:

delphi - Application.ProcessMessages 挂起?

delphi - 如何仅扩展到 TreeView 的第二层

delphi - 记录文件升级和向后兼容性

delphi - 如何在Delphi 7中不调试运行?

Java Switch菜单错误以及如何从另一个类调用方法

c# - 使用开关的菜单不接受我的输入,在 powershell 中?

IE8 的 CSS 菜单问题 - 调试

R:查找带有作者评论的代码(如果有)

PowerShell - Visual Studio Code - 如何在长结果上禁用 'more'?