Regaining Control after Calling WinHelp

Jim Mischel

This article first appeared in PC TECHNIQUES, Aug/Sept 1995

If you've tried to do anything even slightly out of the ordinary with Windows Help, you probably found that when you call the WinHelp() API function, the WINHELP application receives the input focus and doesn't return focus to your program.until the user switches back. This behavior isn't normally a problem, but it does prevent your application from performing some cool tricks. For example, suppose you wanted WinHelp to always display the pertinent help topic whenever your program opens a dialog box. Without a little extra processing, this just isn't possible--WinHelp gets control and doesn't let go.

The solution that immediately comes to mind is to call SetFocus() immediately after calling WinHelp() in order to give focus back to your program. In theory, this should work. It doesn't work, though, because the WinHelp() function doesn't actually call the WINHELP application. It just posts a message to WINHELP's window and returns. So if you write this code:

WinHelp (hwnd, HelpFilename, HELP_CONTEXT, CtxID);

SetFocus (hwnd);

WINHELP ends up processing the message (and gaining the input focus) after you call SetFocus(). What you have to do is insert a Yield() call or a PeekMessage() loop in your code so that WINHELP gets a chance to process the message that was sent to it by the WinHelp() API call. Your code then becomes:

WinHelp (hwnd, HelpFilename, HELP_CONTEXT, CtxID);

Yield ();

SetFocus (hwnd);

And the SetFocus() call will return control to your program.