home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 18.ddi / SAMPLES / HELPHOOK / HELPHOOK.C_ / HELPHOOK.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  15.8 KB  |  456 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: HelpHook.c
  4.  
  5.     PURPOSE: HelpHook template for Windows applications
  6.  
  7.     FUNCTIONS:
  8.  
  9.     WinMain() - calls initialization function, processes message loop
  10.     InitApplication() - initializes window data and registers window
  11.     InitInstance() - saves instance handle and creates main window
  12.     MainWndProc() - processes messages
  13.     About() - processes messages for "About" dialog box
  14.  
  15.     COMMENTS:
  16.  
  17.         Windows can have several copies of your application running at the
  18.         same time.  The variable hInst keeps track of which instance this
  19.         application is so that processing will be to the correct window.
  20.  
  21. ****************************************************************************/
  22.  
  23. #include "windows.h"            /* required for all Windows applications */
  24. #include "HelpHook.h"            /* specific to this program             */
  25.  
  26. HANDLE hInst;                /* current instance                 */
  27. WORD   PWM_PrivateHelpMessage;
  28. HHOOK  MessageFilterHook;
  29. WORD   wCurrentMenuCmd;
  30. DWORD  dwCurrentMenuBits;
  31. HANDLE hWndGlobal;
  32.  
  33.  
  34. /****************************************************************************
  35.  
  36.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  37.  
  38.     PURPOSE: calls initialization function, processes message loop
  39.  
  40.     COMMENTS:
  41.  
  42.         Windows recognizes this function by name as the initial entry point
  43.         for the program.  This function calls the application initialization
  44.         routine, if no other instance of the program is running, and always
  45.         calls the instance initialization routine.  It then executes a message
  46.         retrieval and dispatch loop that is the top-level control structure
  47.         for the remainder of execution.  The loop is terminated when a WM_QUIT
  48.         message is received, at which time this function exits the application
  49.         instance by returning the value passed by PostQuitMessage().
  50.  
  51.         If this function must abort before entering the message loop, it
  52.         returns the conventional value NULL.
  53.  
  54. ****************************************************************************/
  55.  
  56. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  57. HANDLE hInstance;                 /* current instance         */
  58. HANDLE hPrevInstance;                 /* previous instance         */
  59. LPSTR lpCmdLine;                 /* command line             */
  60. int nCmdShow;                     /* show-window type (open/icon) */
  61. {
  62.     MSG msg;                     /* message                 */
  63.  
  64.     if (!hPrevInstance)             /* Other instances of app running? */
  65.         if (!InitApplication(hInstance)) /* Initialize shared things */
  66.             return (FALSE);         /* Exits if unable to initialize     */
  67.  
  68.     /* Perform initializations that apply to a specific instance */
  69.  
  70.     if (!InitInstance(hInstance, nCmdShow))
  71.         return (FALSE);
  72.  
  73.     /* Acquire and dispatch messages until a WM_QUIT message is received. */
  74.  
  75.     PWM_PrivateHelpMessage=RegisterWindowMessage("HelpHookMessage");
  76.  
  77.     if (PWM_PrivateHelpMessage)
  78.     {
  79.         MessageFilterHook=SetWindowsHookEx(WH_MSGFILTER, (FARPROC)HelpMessageFilterHook, hInstance, GetWindowTask(hWndGlobal));
  80.     }
  81.  
  82.     while (GetMessage(&msg,       /* message structure                 */
  83.         NULL,           /* handle of window receiving the message */
  84.         NULL,           /* lowest message to examine             */
  85.         NULL))           /* highest message to examine         */
  86.     {
  87.         TranslateMessage(&msg);       /* Translates virtual key codes         */
  88.         DispatchMessage(&msg);       /* Dispatches message to window         */
  89.     }
  90.  
  91.     if (PWM_PrivateHelpMessage)
  92.     {
  93.         UnhookWindowsHookEx(MessageFilterHook);
  94.     }
  95.  
  96.     return (msg.wParam);       /* Returns the value from PostQuitMessage */
  97. }
  98.  
  99.  
  100. /****************************************************************************
  101.  
  102.     FUNCTION: InitApplication(HANDLE)
  103.  
  104.     PURPOSE: Initializes window data and registers window class
  105.  
  106.     COMMENTS:
  107.  
  108.         This function is called at initialization time only if no other
  109.         instances of the application are running.  This function performs
  110.         initialization tasks that can be done once for any number of running
  111.         instances.
  112.  
  113.         In this case, we initialize a window class by filling out a data
  114.         structure of type WNDCLASS and calling the Windows RegisterClass()
  115.         function.  Since all instances of this application use the same window
  116.         class, we only need to do this when the first instance is initialized.
  117.  
  118.  
  119. ****************************************************************************/
  120.  
  121. BOOL InitApplication(hInstance)
  122. HANDLE hInstance;                   /* current instance         */
  123. {
  124.     WNDCLASS  wc;
  125.  
  126.     /* Fill in window class structure with parameters that describe the       */
  127.     /* main window.                                                           */
  128.  
  129.     wc.style = NULL;                    /* Class style(s).                    */
  130.     wc.lpfnWndProc = MainWndProc;       /* Function to retrieve messages for  */
  131.                                         /* windows of this class.             */
  132.     wc.cbClsExtra = 0;                  /* No per-class extra data.           */
  133.     wc.cbWndExtra = 0;                  /* No per-window extra data.          */
  134.     wc.hInstance = hInstance;           /* Application that owns the class.   */
  135.     wc.hIcon = LoadIcon(hInstance, MAKEINTATOM(helphook));
  136.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  137.     wc.hbrBackground = COLOR_WINDOW+1;
  138.     wc.lpszMenuName =  "HelpHookMenu";   /* Name of menu resource in .RC file. */
  139.     wc.lpszClassName = "HelpHookWClass"; /* Name used in call to CreateWindow. */
  140.  
  141.     /* Register the window class and return success/failure code. */
  142.  
  143.     return (RegisterClass(&wc));
  144. }
  145.  
  146.  
  147. /****************************************************************************
  148.  
  149.     FUNCTION:  InitInstance(HANDLE, int)
  150.  
  151.     PURPOSE:  Saves instance handle and creates main window
  152.  
  153.     COMMENTS:
  154.  
  155.         This function is called at initialization time for every instance of
  156.         this application.  This function performs initialization tasks that
  157.         cannot be shared by multiple instances.
  158.  
  159.         In this case, we save the instance handle in a static variable and
  160.         create and display the main program window.
  161.  
  162. ****************************************************************************/
  163.  
  164. BOOL InitInstance(hInstance, nCmdShow)
  165.     HANDLE          hInstance;          /* Current instance identifier.       */
  166.     int             nCmdShow;           /* Param for first ShowWindow() call. */
  167. {
  168.     HWND            hWnd;               /* Main window handle.                */
  169.  
  170.     /* Save the instance handle in static variable, which will be used in  */
  171.     /* many subsequence calls from this application to Windows.            */
  172.  
  173.     hInst = hInstance;
  174.  
  175.     /* Create a main window for this application instance.  */
  176.  
  177.     hWnd = CreateWindow(
  178.         "HelpHookWClass",                /* See RegisterClass() call.          */
  179.         "HelpHook Sample Application",   /* Text for window title bar.         */
  180.         WS_OVERLAPPEDWINDOW,            /* Window style.                      */
  181.         CW_USEDEFAULT,                  /* Default horizontal position.       */
  182.         CW_USEDEFAULT,                  /* Default vertical position.         */
  183.         CW_USEDEFAULT,                  /* Default width.                     */
  184.         CW_USEDEFAULT,                  /* Default height.                    */
  185.         NULL,                           /* Overlapped windows have no parent. */
  186.         NULL,                           /* Use the window class menu.         */
  187.         hInstance,                      /* This instance owns this window.    */
  188.         NULL                            /* Pointer not needed.                */
  189.     );
  190.  
  191.     hWndGlobal=hWnd;
  192.     /* If window could not be created, return "failure" */
  193.  
  194.     if (!hWnd)
  195.         return (FALSE);
  196.  
  197.     /* Make the window visible; update its client area; and return "success" */
  198.  
  199.     ShowWindow(hWnd, nCmdShow);  /* Show the window                        */
  200.     UpdateWindow(hWnd);          /* Sends WM_PAINT message                 */
  201.     return (TRUE);               /* Returns the value from PostQuitMessage */
  202. }
  203.  
  204. /****************************************************************************
  205.  
  206.     FUNCTION: MainWndProc(HWND, UINT, WPARAM, LPARAM)
  207.  
  208.     PURPOSE:  Processes messages
  209.  
  210.     MESSAGES:
  211.  
  212.     WM_COMMAND    - application menu (About dialog box)
  213.     WM_DESTROY    - destroy window
  214.  
  215.     COMMENTS:
  216.  
  217.     To process the IDM_ABOUT message, call Dialog
  218.     box which will create the box according to the information in your
  219.     HelpHook.rc file and turn control over to the About() function.
  220.  
  221. ****************************************************************************/
  222.  
  223. long __export CALLBACK MainWndProc(hWnd, message, wParam, lParam)
  224. HWND hWnd;                    /* window handle            */
  225. UINT message;                /* type of message            */
  226. WPARAM wParam;                /* additional information    */
  227. LPARAM lParam;                /* additional information    */
  228. {
  229.     switch (message)
  230.     {
  231.         case WM_COMMAND:       /* message: command from application menu */
  232.         {
  233.             switch (wParam)
  234.             {
  235.                 case IDM_ABOUT:
  236.                     DialogBox(hInst, "AboutBox", hWnd, About);
  237.                     break;
  238.  
  239.                 case IDM_DLG1:
  240.                 case IDM_DLG2:
  241.                 case IDM_DLG3:
  242.                 case IDM_DLG4:
  243.                 case IDM_DLG5:
  244.                 case IDM_DLG6:
  245.                 case IDM_DLG7:
  246.                 case IDM_DLG8:
  247.                 case IDM_DLG9:
  248.                 {
  249.                     DialogBox(hInst, MAKEINTRESOURCE(wParam), hWnd, SampleDialog);
  250.                     break;
  251.                 }
  252.  
  253.                 default:
  254.                     return (DefWindowProc(hWnd, message, wParam, lParam));
  255.             }
  256.             break;
  257.         }
  258.  
  259.         case WM_DESTROY:          /* message: window being destroyed */
  260.             PostQuitMessage(0);
  261.             break;
  262.  
  263.         case WM_MENUSELECT:
  264.             dwCurrentMenuBits=lParam;
  265.             wCurrentMenuCmd=wParam;
  266.             return (DefWindowProc(hWnd, message, wParam, lParam));
  267.  
  268.         default:              /* Passes it on if unproccessed    */
  269.             if (message==PWM_PrivateHelpMessage)
  270.             {
  271.                 if (wParam==MSGF_MENU)
  272.                 {
  273.                     if (!(LOWORD(dwCurrentMenuBits) & MF_POPUP))
  274.                     {
  275.                         if (!(LOWORD(dwCurrentMenuBits) & MF_SYSMENU))
  276.                         {
  277.                              char szContext[10];
  278.  
  279.                              wsprintf(szContext, "%i", wCurrentMenuCmd);
  280.  
  281.                              MessageBox(hWnd, szContext, "WinHelp:  Context:", MB_OK);
  282.                              break;
  283.                         }
  284.                     }
  285.                 }
  286.             }
  287.             return (DefWindowProc(hWnd, message, wParam, lParam));
  288.        }
  289.     return (NULL);
  290. }
  291.  
  292.  
  293. /****************************************************************************
  294.  
  295.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  296.  
  297.     PURPOSE:  Processes messages for "About" dialog box
  298.  
  299.     MESSAGES:
  300.  
  301.     WM_INITDIALOG - initialize dialog box
  302.     WM_COMMAND    - Input received
  303.  
  304.     COMMENTS:
  305.  
  306.     No initialization is needed for this particular dialog box, but TRUE
  307.     must be returned to Windows.
  308.  
  309.     Wait for user to click on "Ok" button, then close the dialog box.
  310.  
  311. ****************************************************************************/
  312.  
  313. BOOL __export CALLBACK About(hDlg, message, wParam, lParam)
  314. HWND hDlg;                                /* window handle of the dialog box */
  315. unsigned message;                         /* type of message                 */
  316. WORD wParam;                              /* message-specific information    */
  317. LONG lParam;
  318. {
  319.     extern WORD PWM_PrivateHelpMessage;
  320.  
  321.     switch (message)
  322.     {
  323.         case WM_INITDIALOG:           /* message: initialize dialog box */
  324.             return (TRUE);
  325.  
  326.         case WM_COMMAND:              /* message: received a command */
  327.             switch (wParam)
  328.             {
  329.                 case IDOK:
  330.                 case IDCANCEL:
  331.                     EndDialog(hDlg, TRUE);
  332.                     return TRUE;
  333.                     break;
  334.                 case ID_HELP:
  335. DoHelp:
  336.                 {
  337.                      char szContext[10];
  338.  
  339.                      GetDlgItemText(hDlg, ID_HELPCONTEXT, szContext, 10);
  340.                      MessageBox(hDlg, szContext, "WinHelp:  Context:", MB_OK);
  341.                      break;
  342.                 }
  343.             }
  344.             break;
  345.  
  346.         default:
  347.             if (message==PWM_PrivateHelpMessage)
  348.                 goto DoHelp;
  349.     }
  350.     return (FALSE);                  /* Didn't process a message    */
  351. }
  352. /****************************************************************************
  353.         HelpMessageFilterHook():
  354. ****************************************************************************/
  355.  
  356. DWORD __export CALLBACK HelpMessageFilterHook(nCode, wParam, lpMsg)
  357. int nCode;
  358. WORD wParam;
  359. LPMSG lpMsg;
  360. {
  361.     extern WORD PWM_PrivateHelpMessage;
  362.  
  363.     if (nCode < 0)
  364.         goto DefHook;
  365.  
  366.     if (!lpMsg)
  367.         goto DefHook;
  368.  
  369.     if (nCode==MSGF_DIALOGBOX)
  370.         if (lpMsg->message==WM_KEYDOWN && lpMsg->wParam==VK_F1)
  371.         {
  372.             HWND hTemp=NULL;
  373.             HWND hParent=lpMsg->hwnd;
  374.  
  375.             while (hParent != NULL)
  376.             {
  377.                 hTemp=hParent;
  378.                 if (!(GetWindowLong(hTemp, GWL_STYLE) & WS_CHILD))
  379.                     break;
  380.                 hParent=GetWindowWord(hParent, GWW_HWNDPARENT);
  381.             }
  382.  
  383.             if (hTemp)
  384.                 PostMessage(hTemp, PWM_PrivateHelpMessage, nCode, 0L);
  385.             return TRUE;
  386.         }
  387.  
  388.     if (nCode==MSGF_MENU)
  389.         if (lpMsg->message==WM_KEYDOWN && lpMsg->wParam==VK_F1)
  390.         {
  391.             PostMessage(hWndGlobal, PWM_PrivateHelpMessage, nCode,
  392.                         MAKELONG((WORD)lpMsg->hwnd,0));
  393.         }
  394.  
  395. DefHook:
  396.     return CallNextHookEx(MessageFilterHook, nCode, wParam, (LONG)lpMsg);
  397. }
  398.  
  399. /****************************************************************************
  400.         SampleDialog();
  401. ****************************************************************************/
  402.  
  403. BOOL __export CALLBACK SampleDialog(hDlg, message, wParam, lParam)
  404. HWND hDlg;                                /* window handle of the dialog box */
  405. unsigned message;                         /* type of message                 */
  406. WORD wParam;                              /* message-specific information    */
  407. LONG lParam;
  408. {
  409.     extern WORD PWM_PrivateHelpMessage;
  410.  
  411.     switch (message)
  412.     {
  413.         case WM_INITDIALOG:           /* message: initialize dialog box */
  414.             return (TRUE);
  415.  
  416.         case WM_COMMAND:              /* message: received a command */
  417.             switch (wParam)
  418.             {
  419.                 case IDOK:
  420.                 case IDCANCEL:
  421.                     EndDialog(hDlg, TRUE);
  422.                     return TRUE;
  423.                     break;
  424.  
  425.                 case IDM_DLG1:
  426.                 case IDM_DLG2:
  427.                 case IDM_DLG3:
  428.                 case IDM_DLG4:
  429.                 case IDM_DLG5:
  430.                 case IDM_DLG6:
  431.                 case IDM_DLG7:
  432.                 case IDM_DLG8:
  433.                 case IDM_DLG9:
  434.                 {
  435.                     DialogBox(hInst, MAKEINTRESOURCE(wParam), hDlg, SampleDialog);
  436.                     break;
  437.                 }
  438.  
  439.                 case ID_HELP:
  440. DoHelpSample:
  441.                 {
  442.                     char szContext[10];
  443.  
  444.                     GetDlgItemText(hDlg, ID_HELPCONTEXT, szContext, 10);
  445.                     MessageBox(hDlg, szContext, "WinHelp:  Context:", MB_OK);
  446.                     break;
  447.                 }
  448.             }
  449.             break;
  450.  
  451.         default:
  452.             if (message==PWM_PrivateHelpMessage)
  453.                 goto DoHelpSample;
  454.     }
  455.     return (FALSE);                  /* Didn't process a message    */
  456. }