I find that
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.
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);
}
}