home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / class / rclass.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  3.3 KB  |  147 lines

  1. /*
  2.  *  RegisterClass
  3.  *  This function demonstrates the use of the RegisterClass function.  It will
  4.  *  register a window Class, and then display that window.
  5.  *
  6.  */
  7.  
  8. #include <windows.h>
  9.  
  10.  
  11. long    FAR PASCAL WndProc (HANDLE, unsigned, WORD, LONG);
  12.  
  13. int    PASCAL WinMain(hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  14. HANDLE      hInstance, hPrevInstance;
  15. LPSTR      lpszCmdLine;
  16. int    nCmdShow;
  17. {
  18.   WNDCLASS  wcClass;
  19.   MSG        msg;
  20.   HWND        hWnd;
  21.  
  22.   if (!hPrevInstance)
  23.   {
  24.     wcClass.lpszClassName = "WndClass";
  25.     wcClass.hInstance      = hInstance;
  26.     wcClass.lpfnWndProc   = WndProc;
  27.     wcClass.hCursor      = LoadCursor (NULL, IDC_ARROW);
  28.     wcClass.hIcon      = NULL;
  29.     wcClass.lpszMenuName  = NULL;
  30.     wcClass.hbrBackground = GetStockObject (WHITE_BRUSH);
  31.     wcClass.style      = CS_HREDRAW | CS_VREDRAW;
  32.     wcClass.cbClsExtra      = 0;
  33.     wcClass.cbWndExtra      = 0;
  34.  
  35.     if (!RegisterClass (&wcClass))
  36.       return FALSE;
  37.   }
  38.  
  39.   hWnd = CreateWindow("WndClass",
  40.       "RegisterClass",
  41.       WS_OVERLAPPEDWINDOW,
  42.       CW_USEDEFAULT,
  43.       CW_USEDEFAULT,
  44.       CW_USEDEFAULT,
  45.       CW_USEDEFAULT,
  46.       NULL,
  47.       NULL,
  48.       hInstance,
  49.       NULL);
  50.  
  51.   ShowWindow (hWnd, nCmdShow);
  52.   UpdateWindow (hWnd);
  53.  
  54.   while (GetMessage(&msg, NULL, 0, 0))
  55.   {
  56.     TranslateMessage(&msg);
  57.     DispatchMessage(&msg);
  58.   }
  59.   return msg.wParam;
  60. }
  61.  
  62.  
  63. long    FAR PASCAL WndProc(hWnd, message, wParam, lParam)
  64. HWND        hWnd;
  65. unsigned    message;
  66. WORD        wParam;
  67. LONG        lParam;
  68. {
  69.   PAINTSTRUCT   ps;
  70.   HDC           hDC;
  71.   HWND        hButton;
  72.   HANDLE    hInst;
  73.   short    xChar,
  74.   yChar;
  75.   TEXTMETRIC    tm;
  76.   WNDCLASS    wndclass;
  77.   BOOL        bRegClass;
  78.  
  79.   hInst = GetWindowWord(hWnd, GWW_HINSTANCE);
  80.   switch (message)
  81.   {
  82.   case WM_CREATE:
  83.     {
  84.       hDC = GetDC(hWnd);
  85.       GetTextMetrics(hDC, &tm);
  86.       xChar = tm.tmAveCharWidth;
  87.       yChar = tm.tmHeight + tm.tmExternalLeading;
  88.       ReleaseDC(hWnd, hDC);
  89.       hButton = CreateWindow("button",
  90.           "Register Window Class",
  91.           WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
  92.           xChar * 20,
  93.           yChar * 7,
  94.           xChar * 23,
  95.           yChar * 3,
  96.           hWnd,
  97.           0,
  98.           hInst,
  99.           NULL);
  100.       break;
  101.     }
  102.   case WM_COMMAND:
  103.     {
  104.       if (wParam == 0)
  105.       {
  106.     MessageBox(hWnd,
  107.         "Registering window\nclass FooClass:",
  108.         "RegisterClass()",
  109.         MB_OK);
  110.     wndclass.lpszClassName = "FooClass";
  111.     wndclass.hInstance     = hInst;
  112.     wndclass.lpfnWndProc   = DefWindowProc;
  113.     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  114.     wndclass.hIcon           = NULL;
  115.     wndclass.lpszMenuName  = NULL;
  116.     wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  117.     wndclass.style           = CS_HREDRAW | CS_VREDRAW;
  118.     wndclass.cbClsExtra    = 0;
  119.     wndclass.cbWndExtra    = 0;
  120.  
  121.     bRegClass = RegisterClass(&wndclass);
  122.     if (bRegClass)
  123.       MessageBox(hWnd,
  124.           "Window class FooClass\nhas been registered.\nAll re-registration\nattempts should fail.",
  125.           "RegisterClass()",
  126.           MB_ICONASTERISK);
  127.     else
  128.       MessageBox(hWnd,
  129.           "Registration failed.\nThe class is already\nregistered.",
  130.           "RegisterClass()",
  131.           MB_ICONEXCLAMATION);
  132.       }
  133.       break;
  134.     }
  135.   case WM_DESTROY:
  136.     {
  137.       PostQuitMessage(0);
  138.       break;
  139.     }
  140.   default:
  141.     return(DefWindowProc(hWnd, message, wParam, lParam));
  142.   }
  143.   return(0L);
  144. }
  145.  
  146.  
  147.