home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / accell / loadacc.c next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  4.1 KB  |  138 lines

  1. /*
  2.  *   LoadAccelerators
  3.  *
  4.  *   This program demonstrates the use of the LoadAccelerators function.
  5.  *   The LoadAccelerators function loads an accelerator table. In this
  6.  *   program, LoadAccelerators in called in LoadAccWindProc. The two
  7.  *   accelerators that are loaded are ^A and ^B, which are accelerators
  8.  *   for the menu choices "CHOICE1" and "CHOICE2."
  9.  */
  10.  
  11. #include <windows.h>
  12. #include "loadacc.h"
  13.  
  14. long    FAR PASCAL LoadAccWndProc (HWND, unsigned, WORD, LONG);
  15.  
  16. static HANDLE hAccelTable;            /*    handle to accelerator table  */
  17.  
  18. /**************************************************************************/
  19.  
  20. int     PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  21. HANDLE hInstance, hPrevInstance;
  22. LPSTR lpszCmdLine;
  23. int    cmdShow;
  24.   {
  25.   MSG   msg;
  26.   HWND  hWnd;
  27.   HMENU hMenu;
  28.  
  29.   if (!hPrevInstance)
  30.     {
  31.     WNDCLASS   LoadAccClass;
  32.  
  33.     LoadAccClass.hCursor        = LoadCursor (NULL, IDC_ARROW);
  34.     LoadAccClass.hIcon          = LoadIcon (hInstance, NULL);
  35.   /*---------------------- important for adding menu ------------------------*/
  36.     LoadAccClass.lpszMenuName   = (LPSTR)"example";
  37.   /*-------------------------------------------------------------------------*/
  38.     LoadAccClass.lpszClassName  = (LPSTR)"Sample Application";
  39.     LoadAccClass.hbrBackground  = (HBRUSH)GetStockObject (WHITE_BRUSH);
  40.     LoadAccClass.hInstance      = hInstance;
  41.     LoadAccClass.style          = CS_HREDRAW | CS_VREDRAW;
  42.     LoadAccClass.lpfnWndProc    = LoadAccWndProc;
  43.  
  44.     if (!RegisterClass ( (LPWNDCLASS)&LoadAccClass))
  45.       return FALSE;
  46.     }
  47.  
  48.   hWnd = CreateWindow ( (LPSTR)"Sample Application",
  49.       (LPSTR)"Sample Application",
  50.       WS_OVERLAPPEDWINDOW,
  51.       CW_USEDEFAULT,
  52.       CW_USEDEFAULT,
  53.       CW_USEDEFAULT,
  54.       CW_USEDEFAULT,
  55.       (HWND)NULL,        /* no parent */
  56.       (HMENU)NULL,       /* use class menu */
  57.       (HANDLE)hInstance, /* handle to window instance */
  58.       (LPSTR)NULL);      /* no params to pass on */
  59.  
  60.   ShowWindow (hWnd, cmdShow);
  61.   UpdateWindow (hWnd);
  62.  
  63.   while (GetMessage ( (LPMSG) & msg, NULL, 0, 0))
  64.     {
  65. /* if translation of accelerator succesful */
  66.     if (TranslateAccelerator (msg.hwnd, hAccelTable, (LPMSG) & msg) == 0)
  67.       {
  68.       TranslateMessage ( (LPMSG) & msg);
  69.       DispatchMessage ( (LPMSG) & msg);
  70.       }
  71.     }
  72.   return (int)msg.wParam;
  73.   }
  74.  
  75. /* Procedures which make up the window class. */
  76. long    FAR PASCAL LoadAccWndProc (hWnd, message, wParam, lParam)
  77. HWND     hWnd;
  78. unsigned message;
  79. WORD     wParam;
  80. LONG     lParam;
  81.   {
  82.   PAINTSTRUCT    ps;
  83.   TEXTMETRIC     tm;
  84.   static short   Xchar, Ychar;
  85.   HDC            hDC;
  86.   HANDLE         hInstance;
  87.  
  88.   switch (message)
  89.     {
  90.     case WM_CREATE:
  91.       hDC = GetDC (hWnd);
  92.       GetTextMetrics (hDC, &tm);
  93.       Xchar = tm.tmAveCharWidth;
  94.       Ychar = tm.tmHeight + tm.tmExternalLeading;
  95.       ReleaseDC (hWnd, hDC);
  96.       hInstance = GetWindowWord (hWnd, GWW_HINSTANCE);
  97.   /**** Load the accelerator table ***/
  98.       hAccelTable = LoadAccelerators (hInstance, (LPSTR)"example");
  99.       if (!hAccelTable)      /* if accelerator table not properly loaded */
  100.         return FALSE;
  101.       break;
  102.  
  103.     case WM_COMMAND:
  104.       switch (wParam)
  105.         {
  106.         case CHOICE1:   /** can be chosen with accelerator ^A **/
  107.           MessageBox (hWnd, (LPSTR)"CHOICE1 has been chosen",
  108.               (LPSTR)"MENU INFO", MB_OK);
  109.           break;
  110.  
  111.         case CHOICE2:   /** can be chosen with accelerator ^B **/
  112.           MessageBox (hWnd, (LPSTR)"CHOICE2 has been chosen",
  113.               (LPSTR)"MENU INFO", MB_OK);
  114.           break;
  115.  
  116.         default:
  117.           break;
  118.         }
  119.       break;
  120.  
  121.     case WM_DESTROY:
  122.       PostQuitMessage (0);
  123.       break;
  124.  
  125.     case WM_PAINT:
  126.       BeginPaint (hWnd, (LPPAINTSTRUCT) & ps);
  127.       TextOut (ps.hdc, Xchar, Ychar,
  128.           (LPSTR)"To test accelerators press ^A or ^B", 35);
  129.       EndPaint (hWnd, (LPPAINTSTRUCT) & ps);
  130.       break;
  131.  
  132.     default:
  133.       return DefWindowProc (hWnd, message, wParam, lParam);
  134.       break;
  135.     }
  136.   return (0L);
  137.   }
  138.