简体   繁体   中英

Hеlp-button in a message box not working in a console application

The below code is a console application. By adding System.Windows.Forms reference I am able to use a MessageBox . The help button shows up in the message box, however clicking it doesn't open the help window. It is not throwing any exception. Is it possible to do it?

Code snippet is below,

MessageBox.Show("ABCD", "Caption is",
                  MessageBoxButtons.OK,
                  MessageBoxIcon.Information,
                  MessageBoxDefaultButton.Button2,
                  0, @"S:\Product\Documentation\Help.chm",
                  HelpNavigator.TopicId, "34049");

As pointed out by bommelding in a console application you will use writeline to help the user somehow.

I have have made a mock up WinForm app and the help button does work as expected. Tried with a console application and the help button doesn't behave.

You could probably make it work in a console app if you can capture the event help button click. But it would be more hacky than a solution.

I find that

  1. In a WinForms application, the help file can not be launched either if the Form has not yet shown. That is, if the code snippet is put inside the Form's constructor, it exhibits the same issue.

  2. In a Console application, if you have create a Form and show it, then the code snippet is working fine.

Then I check the reference source of MessageBox class , and find that it exposes a HelpInfo property, which is used in Control class's message loop .

 ///     Handles the WM_HELP message
 private void WmHelp(ref Message)     

In other words, the launch of chm file is actually done by the Control class, by handling the WM_HELP message. That is why this requires a Form and also requires the Form is already displayed (so the message loop is already running).

Below code illustrates my finding, note this is a Console project .

public class OpenCHMInMessageBox
{
    public void ShowCHM()
    {
        MyForm form1 = new MyForm();            
        form1.Show();

        MessageBox.Show("ABCD", "Caption is",
              MessageBoxButtons.OK,
              MessageBoxIcon.Information,
              MessageBoxDefaultButton.Button2,
              0, @"S:\Product\Documentation\Help.chm",
              HelpNavigator.TopicId, "34049");
    }
}

public class MyForm : Form
{
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x0053) //WM_HELP 
        {
            System.Diagnostics.Debug.WriteLine("WM_HELP");
            //return;  //return if you don't want to handle the WM_HELP message, then CHM will NOT be launched
        }

        base.WndProc(ref m);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM