home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_03_01 / 3n01008a < prev    next >
Text File  |  1991-12-05  |  5KB  |  125 lines

  1. /*****************************************************/
  2. /* dlgaccel.c                                        */
  3. /* -- Module implements a routine to create a dialog */
  4. /*    with an accelerator table.                     */
  5. /* -- Make sure you EXPORT AcceleratorHook() in your */
  6. /*    .def file!                                     */
  7. /*****************************************************/
  8.  
  9. /*****************************************************/
  10. /* Header files.                                     */
  11. /*****************************************************/
  12. #include <windows.h>
  13. #include "dlgaccel.h"
  14.  
  15. /*****************************************************/
  16. /* Private static variables.                         */
  17. /*****************************************************/
  18. static  FARPROC lpfnAccelSav;   /* Previous hook. */
  19. static  HANDLE  hmemAccel;      /* Accel. table. */
  20.  
  21. /*****************************************************/
  22. /* Private prototypes.                               */
  23. /*****************************************************/
  24. int FAR PASCAL  AcceleratorHook(int, WORD, DWORD);
  25.  
  26. /*****************************************************/
  27. /* Routines.                                         */
  28. /*****************************************************/
  29.  
  30. VOID
  31. AcceleratedDialog(LPSTR lszDialog, LPSTR lszAccel,
  32.     FARPROC lpfnDialog, HANDLE hins, HWND hwndOwner)
  33. /*****************************************************/
  34. /* -- Create a dialog with an accelerator table.     */
  35. /* -- lszDialog : Name of dialog resource.           */
  36. /* -- lszAccel  : Name of accelerator table.         */
  37. /* -- lpfn      : Actual address of DialogProc.      */
  38. /* -- hins      : App's instance handle.             */
  39. /* -- hwndOwner : Dialog owner window.               */
  40. /*****************************************************/
  41.     {
  42.     FARPROC lpfnInstance;   /* DialogProc instance. */
  43.     FARPROC lpfnAccel;      /* Hook instance. */
  44.     HANDLE  hmemAccelSav;   /* Last accel. table. */
  45.  
  46.     /* If this is the first instance of this routine */
  47.     /* (since it can be called recursively), create */
  48.     /* and install the message hook. */
  49.     if (hmemAccel == NULL)
  50.         {
  51.         lpfnAccel =
  52.           MakeProcInstance(AcceleratorHook, hins);
  53.         lpfnAccelSav =
  54.           SetWindowsHook(WH_MSGFILTER, lpfnAccel);
  55.         }
  56.  
  57.     /* Create an instanced dialog proc address. */
  58.     lpfnInstance = MakeProcInstance(lpfnDialog, hins);
  59.  
  60.     /* Load up the dialog's accelerator table and */
  61.     /* create the dialog.  Save the previous */
  62.     /* accelerator table handle, so we can restore */
  63.     /* the previous instance of this routine, if */
  64.     /* any. */
  65.     hmemAccelSav = hmemAccel;
  66.     hmemAccel = LoadAccelerators(hins, lszAccel);
  67.     DialogBox(hins, lszDialog, hwndOwner,
  68.       lpfnInstance);
  69.     FreeResource(hmemAccel);
  70.     hmemAccel = hmemAccelSav;
  71.     FreeProcInstance(lpfnInstance);
  72.  
  73.     if (hmemAccel == NULL)
  74.         {
  75.         UnhookWindowsHook(WH_MSGFILTER, lpfnAccel);
  76.         lpfnAccelSav = NULL;
  77.         FreeProcInstance(lpfnAccel);
  78.         }
  79.     }
  80.  
  81. int FAR PASCAL
  82. AcceleratorHook(int msgf, WORD wParam, DWORD lParam)
  83. /*****************************************************/
  84. /* -- Message filter windows hook.                   */
  85. /* -- Look for keystrokes and invoke                 */
  86. /*    TranslateAccelerator().                        */
  87. /*****************************************************/
  88.     {
  89.     DWORD   lVal;
  90.  
  91.     if (msgf >= 0 && hmemAccel != NULL)
  92.         {
  93.         switch (((LPMSG)lParam)->message)
  94.             {
  95.         default:
  96.             break;
  97.  
  98.         case WM_KEYDOWN:
  99.         case WM_KEYUP:
  100.         case WM_SYSKEYDOWN:
  101.         case WM_SYSKEYUP:
  102.             {
  103.             HWND    hwnd    = ((LPMSG)lParam)->hwnd;
  104.  
  105.             /* Make sure we don't do this for a child */
  106.             /* window, since Windows will think the */
  107.             /* id is a menu handle! */
  108.             if (GetWindowLong(hwnd, GWL_STYLE) &
  109.               WS_CHILD)
  110.                 hwnd =
  111.                   GetWindowWord(hwnd, GWW_HWNDPARENT);
  112.  
  113.                 if (TranslateAccelerator(hwnd,
  114.                   hmemAccel, (LPMSG)lParam))
  115.                     return TRUE;
  116.             }
  117.             break;
  118.             }   /* End switch message. */
  119.         }       /* End if hmemAccel != NULL. */
  120.  
  121.     lVal =  DefHookProc(msgf, wParam, lParam,
  122.       (FARPROC FAR *)&lpfnAccelSav);
  123.     return msgf < 0 ? LOWORD(lVal) : FALSE;
  124.     }
  125.