home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / video / vidcap / help.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-05  |  4.9 KB  |  198 lines

  1. /**************************************************************************
  2.  *
  3.  *  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4.  *  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5.  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6.  *  PURPOSE.
  7.  *
  8.  *  Copyright (C) 1992 - 1997 Microsoft Corporation.  All Rights Reserved.
  9.  *
  10.  **************************************************************************/
  11. /****************************************************************************
  12.  *
  13.  *   help.c: Help system interface
  14.  *
  15.  *   Vidcap32 Source code
  16.  *
  17.  ***************************************************************************/
  18.  
  19. /*
  20.  * supports F1 key help in app and in dialog by installing a hook,
  21.  *
  22.  * Keep track of which dialog is currently displayed in a global:
  23.  * dialog ids are also topic ids in the help file.
  24.  */
  25.  
  26.  
  27. #include <windows.h>
  28. #include <windowsx.h>
  29. #include <string.h>
  30.  
  31. #include "help.h"
  32.  
  33. int CurrentDialogID = 0;
  34.  
  35.  
  36. // app info passed to helpinit
  37. HINSTANCE hInstance;
  38. char HelpFile[MAX_PATH];
  39. HWND hwndApp;
  40.  
  41. //hook proc and old msg filter
  42. #ifdef _WIN32
  43. HHOOK hOurHook;
  44. #else
  45. FARPROC fnOldMsgFilter = NULL;
  46. FARPROC fnMsgHook = NULL;
  47. #endif
  48.  
  49.  
  50. // call DialogBoxParam, but ensuring correct help processing:
  51. // assumes that each Dialog Box ID is a context number in the help file.
  52. // calls MakeProcInstance as necessary. Uses instance data passed to
  53. // HelpInit().
  54. int
  55. DoDialog(
  56.    HWND hwndParent,     // parent window
  57.    int DialogID,        // dialog resource id
  58.    DLGPROC fnDialog,    // dialog proc
  59.    long lParam          // passed as lparam in WM_INITDIALOG
  60. )
  61. {
  62.     int olddialog;
  63.     DLGPROC fn;
  64.     int result;
  65.  
  66.     // remember current id (for nested dialogs)
  67.     olddialog = CurrentDialogID;
  68.  
  69.     // save the current id so the hook proc knows what help to display
  70.     CurrentDialogID = DialogID;
  71.  
  72.     fn = (DLGPROC) MakeProcInstance(fnDialog, hInstance);
  73.     result = DialogBoxParam(
  74.                 hInstance,
  75.                 MAKEINTRESOURCE(CurrentDialogID),
  76.                 hwndParent,
  77.                 fn,
  78.                 lParam);
  79.     FreeProcInstance(fn);
  80.     CurrentDialogID = olddialog;
  81.  
  82.     return result;
  83. }
  84.  
  85.  
  86. // set the help context id for a dialog displayed other than by DoDialog
  87. // (eg by GetOpenFileName). Returns the old help context that you must
  88. // restore by a further call to this function
  89. int
  90. SetCurrentHelpContext(int DialogID)
  91. {
  92.     int oldid = CurrentDialogID;
  93.     CurrentDialogID = DialogID;
  94.     return(oldid);
  95. }
  96.  
  97.  
  98.  
  99. // return TRUE if lpMsg is a non-repeat F1 key message
  100. BOOL
  101. IsHelpKey(LPMSG lpMsg)
  102. {
  103.     return lpMsg->message == WM_KEYDOWN &&
  104.                lpMsg->wParam == VK_F1 &&
  105.                !(HIWORD(lpMsg->lParam) & KF_REPEAT) &&
  106.                GetKeyState(VK_SHIFT) >= 0 &&
  107.                GetKeyState(VK_CONTROL) >= 0 &&
  108.                GetKeyState(VK_MENU) >= 0;
  109. }
  110.  
  111.  
  112.  
  113. LRESULT CALLBACK
  114. HelpMsgHook(int nCode, WPARAM wParam, LPARAM lParam)
  115. {
  116.     if (nCode >= 0) {
  117.         if (IsHelpKey((LPMSG)lParam)) {
  118.             if (CurrentDialogID != 0) {
  119.                 WinHelp(hwndApp, HelpFile, HELP_CONTEXT, CurrentDialogID);
  120.             } else {
  121.                 WinHelp(hwndApp, HelpFile, HELP_CONTENTS, 0);
  122.             }
  123.         }
  124.     }
  125. #ifdef _WIN32
  126.     return CallNextHookEx(hOurHook, nCode, wParam, lParam);
  127. #else
  128.     return DefHookProc(nCode, wParam, lParam, fnOldMsgFilter);
  129. #endif
  130.  
  131. }
  132.  
  133.  
  134.  
  135.  
  136.  
  137. // help init - initialise the support for the F1 key help
  138. BOOL
  139. HelpInit(HINSTANCE hinstance, LPSTR helpfilepath, HWND hwnd)
  140. {
  141.     LPSTR pch;
  142.  
  143.     // save app details
  144.     hwndApp = hwnd;
  145.     hInstance = hinstance;
  146.  
  147.     // assume that the help file is in the same directory as the executable-
  148.     // get the executable path, and replace the filename with the help
  149.     // file name.
  150.     GetModuleFileName(hinstance, HelpFile, sizeof(HelpFile));
  151.  
  152.     // find the final backslash, and append the help file name there
  153.     pch = _fstrrchr(HelpFile, '\\');
  154.     pch++;
  155.     lstrcpy(pch, helpfilepath);
  156.  
  157.     // install a hook for msgs and save old one
  158. #ifdef _WIN32
  159.     hOurHook = SetWindowsHookEx(
  160.                         WH_MSGFILTER,
  161.                         (HOOKPROC) HelpMsgHook,
  162.                         NULL, GetCurrentThreadId());
  163. #else
  164.     fnMsgHook = (FARPROC) MakeProcInstance(HelpMsgHook, hInstance);
  165.     fnOldMsgFilter = SetWindowsHook(WH_MSGFILTER, (HOOKPROC) fnMsgHook);
  166. #endif
  167.  
  168.     return(TRUE);
  169. }
  170.  
  171.  
  172.  
  173. // shutdown the help system
  174. void
  175. HelpShutdown(void)
  176. {
  177. #ifdef _WIN32
  178.     UnhookWindowsHookEx(hOurHook);
  179. #else
  180.     if (fnOldMsgFilter) {
  181.         UnhookWindowsHook(WH_MSGFILTER, fnMsgHook);
  182.         FreeProcInstance(fnMsgHook);
  183.     }
  184. #endif
  185.  
  186.     WinHelp(hwndApp, HelpFile, HELP_QUIT, 0);
  187. }
  188.  
  189.  
  190. // start help at the contents page
  191. void
  192. HelpContents(void)
  193. {
  194.     WinHelp(hwndApp, HelpFile, HELP_CONTENTS, 0);
  195. }
  196.  
  197.  
  198.