ShowHelp() and weird window behaviour

  • Thread starter Thread starter James Hancock
  • Start date Start date
J

James Hancock

Why is it that ShowHelp causes the Help file to show up as some sort of
child to the main application? (I've tried this with MS help files and it
does the same, it isn't my help file per-say) You can't click on the
application without bring the help file to the front, you can't alt+Tab
between the help file and the application etc.

This is highly not a good thing. It should be a separate application for all
intents and purposes.

Anyone have any ideas?

Thanks,
James Hancock
 
I haven't really played around too much with the Help class, but the way that I've been showing a help file (in chm format) is to just run it in a separate process using the Process class. You can even take care of finding and showing the help process if the user already has the help file open. Code below:

[DllImport("User32.dll", EntryPoint="SetForegroundWindow")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("User32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

private const int SW_RESTORE = 9;

------------------------------------------------------------------------------------

// private member to the main form.
private Process help;

// place this code in a method to handle showing the help.
if (help == null)
{
help = new Process();
}
else
{
if (!help.HasExited)
{
ShowWindow(help.MainWindowHandle, SW_RESTORE);
SetForegroundWindow(help.MainWindowHandle);
return;
}
}
string helpFile = @"C:\Program Files\My App\Help\MyHelp.chm";
try
{
ProcessStartInfo processstartinfo = new ProcessStartInfo(helpFile);
help.StartInfo = processstartinfo;
help.Start();
}
catch (Exception ex)
{
help.Dispose();
help = null;
MessageBox.Show("An error was encountered while opening the help file \"" + helpFile + "\".\nAdditional Info: " + ex.Message, "Help Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

HTH
 
Ugh. OK. That's what I was afraid of.

Anyone have any ideas how to make the ShowHelp function work right?

James Hancock
I haven't really played around too much with the Help class, but the way that I've been showing a help file (in chm format) is to just run it in a separate process using the Process class. You can even take care of finding and showing the help process if the user already has the help file open. Code below:

[DllImport("User32.dll", EntryPoint="SetForegroundWindow")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("User32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

private const int SW_RESTORE = 9;

------------------------------------------------------------------------------------

// private member to the main form.
private Process help;

// place this code in a method to handle showing the help.
if (help == null)
{
help = new Process();
}
else
{
if (!help.HasExited)
{
ShowWindow(help.MainWindowHandle, SW_RESTORE);
SetForegroundWindow(help.MainWindowHandle);
return;
}
}
string helpFile = @"C:\Program Files\My App\Help\MyHelp.chm";
try
{
ProcessStartInfo processstartinfo = new ProcessStartInfo(helpFile);
help.StartInfo = processstartinfo;
help.Start();
}
catch (Exception ex)
{
help.Dispose();
help = null;
MessageBox.Show("An error was encountered while opening the help file \"" + helpFile + "\".\nAdditional Info: " + ex.Message, "Help Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

HTH
 
James,

Unfortunately this is the way the Help engine works. This happens not only
in .Net but in Win32 apps as well. The help file will pop up on top only the
first time it's loaded. After that it's activated in the current stack
location. I consider that a bug in the Help APIs. It behaves kind of similar
to the way IE behaves when you load things into the same window...

I think the approach that Tim showed (or something similar using just
FindWindow and SetForegroundWindow) is about the only way to make sure your
help pops ontop.

To make things worse the .Net Help Provider provides no hooks of any kind to
extend it either...

Help is one of the things that Microsoft is really bad with and this Help
API stinks and has been stale for 5+ years now with not even a clear vision
of how that's going to be improved in the next Windows version (all we got
was a horrible presentation at PDC)...

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/wwhelp/
http://www.west-wind.com/blog/
 
Back
Top