代码之家  ›  专栏  ›  技术社区  ›  user1580348

检测MessageBox中的帮助按钮点击?

  •  4
  • user1580348  · 技术社区  · 9 年前

    在DelphiXE7中,我需要使用MessageBox中的帮助按钮。MSDN声明:

    MB_HELP 0x00004000L在消息框中添加帮助按钮。当 用户单击“帮助”按钮或按F1,系统将发送WM_Help 向所有者发送消息。

    但是,当我单击MessageBox中的“帮助”按钮时,似乎没有WM_Help消息发送到应用程序:

    procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
    begin
      if Msg.message = WM_HELP then
        CodeSite.Send('ApplicationEvents1Message WM_HELP');
    end;
    
    procedure TForm1.btnShowMessageBoxClick(Sender: TObject);
    begin
      MessageBox(Self.Handle, 'Let''s test the Help button.', 'Test', MB_ICONINFORMATION or MB_OK or MB_HELP);
    end;
    

    那么,我如何点击MessageBox帮助按钮,以及如何检测它来自哪个MessageBox?

    2 回复  |  直到 9 年前
        1
  •  5
  •   David Heffernan    9 年前

    文件中强调:

    系统 发送 向所有者发送WM_ HELP消息。

    这是MSDN代码,用于消息直接同步传递到窗口过程。换句话说,它是使用 SendMessage ,或等效API。

    你试图在 TApplicationEvents.OnMessage 其用于拦截异步消息。这是放置在消息队列中的消息。这些消息(通常)放置在队列中 PostMessage .

    所以你从来没有在 TApplicationEvents.OnMessage 是消息永远不会放在队列中。相反,您需要在所有者窗口的窗口过程中处理消息。在Delphi中,最简单的方法如下:

    type
      TForm1 = class(TForm)
      ....
      protected
        procedure WMHelp(var Message: TWMHelp); message WM_HELP;
      end;
    ....
    procedure TForm1.WMHelp(var Message: TWMHelp);
    begin
      // your code goes here
    end;
    

    至于如何检测哪个消息框负责发送消息,使用 MessageBox 。也许最好的办法是切换到 MessageBoxIndirect 。这允许您在 dwContextHelpId 字段 MSGBOXPARAMS 。该ID将传递给 WM_HELP 消息,如中所述 documentation .

    如果您要显示主题和帮助文件,以响应用户按下帮助按钮,那么您可以考虑使用VCL函数 MessageDlg 。这允许您传递帮助上下文ID,框架将显示应用程序帮助文件,并传递该帮助上下文ID。

        2
  •  2
  •   Denis Anisimov    9 年前

    最小工作样本:

    type
      TForm20 = class(TForm)
        procedure FormCreate(Sender: TObject);
      protected
        procedure WMHelp(var Message: TWMHelp); message WM_HELP;
      end;
    
    procedure TForm20.FormCreate(Sender: TObject);
    begin
      MessageBox(Handle, 'Help test', nil, MB_OK or MB_HELP);
    end;
    
    procedure TForm20.WMHelp(var Message: TWMHelp);
    begin
      Caption := 'Help button works';
    end;