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

  1. /*
  2.  
  3. Function(s) demonstrated in this program: SetClassLong
  4.  
  5. Windows version:  2.03
  6.  
  7. Windows SDK version:  2.00
  8.  
  9. Compiler version:  C 5.10
  10.  
  11. Description:   This function replaces the long value in the window class
  12.    structure corresponding to the window handle parameter, with the 
  13.    respective new value.  
  14.  
  15. Additional Comments:  This program requests 4 extra bytes when it registers
  16.    its class structure.  These 4 extra bytes are used to store a long
  17.    value corresponding to the number of instances of windows in this class.
  18.    This value is updated using GetClassLong and SetClassLong and an upper 
  19.    bound of 3 windows of this class is enforced.  
  20.    
  21.  
  22. */
  23.  
  24. #define NOMINMAX    
  25. #include <stdlib.h>
  26. #include <windows.h>
  27. #include "SetCl.h"
  28. #include "string.h"
  29. #include "stdio.h"
  30.  
  31.        char         szAppName             [] = "SetCl"             ;
  32.        HANDLE       hInstMain                                      ;
  33.        HWND         hWndMain                                       ;
  34.        char         szOutputBuffer1       [70]                     ;
  35.        char         szOutputBuffer2       [500]                    ;
  36.        HBITMAP      hBitmapHelp                                    ;
  37.  
  38.  
  39. /****************************************************************************/
  40. /************************    Message Structure      *************************/
  41. /****************************************************************************/
  42.  
  43. struct { char *szMessage; }
  44.        Messages [] = {
  45. "About",
  46. "     This is a sample application to demonstrate the\n\
  47. use  of  the  SetClassLong  Windows function.",
  48.  
  49. "Help Message",
  50. "     This program uses GetClassLong and\n\
  51. SetClassLong to keep track of the number of windows\n\
  52. of this class.  In order to do this four bytes must\n\
  53. be reserved in the class structure using the\n\
  54. cbClsExtra field in the class the first time it is\n\
  55. initialized.  To test this program try to run more\n\
  56. than three instances.  When trying to run a fourth\n\
  57. instance, the program will state that the limit has\n\
  58. been reached and automatically terminate.",
  59.  
  60. "Warning",
  61. "     Only Three windows of this class are allowed\n\
  62. at any one time.",
  63.  
  64. };    
  65.  
  66. /****************************************************************************/
  67.  
  68. void ProcessMessage (HWND, int); 
  69.  
  70. void ProcessMessage (hWnd, MessageNumber) 
  71.      HWND     hWnd;
  72.      int      MessageNumber;
  73. {
  74.      sprintf (szOutputBuffer1, "%s", Messages [MessageNumber]);
  75.      sprintf (szOutputBuffer2, "%s", Messages [MessageNumber + 1]);
  76.      MessageBox (hWnd, szOutputBuffer2, szOutputBuffer1, MB_OK);
  77. }       
  78.  
  79. /****************************************************************************/
  80.  
  81. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  82.     HANDLE    hInstance, hPrevInstance;
  83.     LPSTR    lpszCmdLine;
  84.     int     nCmdShow;
  85.     {
  86.     HWND    hWnd;
  87.     MSG     msg;
  88.     WNDCLASS    wndclass;
  89.     LONG    WindowCount;
  90.  
  91.  
  92.     if (!hPrevInstance)
  93.     {
  94.     wndclass.style             = CS_HREDRAW | CS_VREDRAW | CS_SAVEBITS;
  95.     wndclass.lpfnWndProc    = WndProc;
  96.     wndclass.cbClsExtra     = 4;
  97.     wndclass.cbWndExtra     = 0;
  98.     wndclass.hInstance      = hInstance;
  99.     wndclass.hIcon             = NULL; 
  100.    wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW) ;
  101.     wndclass.hbrBackground    = GetStockObject (WHITE_BRUSH);
  102.     wndclass.lpszMenuName    = szAppName;
  103.     wndclass.lpszClassName    = szAppName;
  104.  
  105.     if (!RegisterClass (&wndclass) )
  106.         return FALSE;
  107.     }
  108.  
  109.     hWnd = CreateWindow (szAppName, "SetClassLong Main",
  110.            WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
  111.            NULL, NULL, hInstance, NULL);
  112.        
  113.     ShowWindow (hWnd, nCmdShow);
  114.     UpdateWindow (hWnd);
  115.  
  116.     WindowCount = GetClassLong (hWnd, 0);
  117.     sprintf (szOutputBuffer1, 
  118.              "     Number of windows with this class currently running = %i.",
  119.              WindowCount + 1);
  120.     MessageBox (hWnd, szOutputBuffer1, "SetClassLong", MB_OK);
  121.  
  122.     if (WindowCount < MAXWINDOWS) {
  123.        WindowCount += 1;
  124.        SetClassLong (hWnd, 0, WindowCount);
  125.     }
  126.     else {
  127.        ProcessMessage (hWnd, 4);
  128.        SendMessage (hWnd, WM_SYSCOMMAND, SC_CLOSE, 0L);
  129.     }
  130.  
  131.     while (GetMessage (&msg, NULL, 0, 0))
  132.         {
  133.         TranslateMessage (&msg);
  134.         DispatchMessage (&msg);
  135.         }
  136.     return msg.wParam;
  137. }
  138.  
  139. /****************************************************************************/
  140.  
  141. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  142.    HWND      hWnd;
  143.    unsigned  iMessage;
  144.    WORD      wParam;
  145.    LONG      lParam;
  146.    {
  147.    HMENU     hMenu;
  148.  
  149.    switch (iMessage)
  150.        {
  151.        case WM_CREATE:
  152.             hMenu = GetSystemMenu (hWnd, FALSE);
  153.  
  154.             ChangeMenu (hMenu, NULL, "&About", IDM_ABOUT, 
  155.                         MF_APPEND | MF_STRING);
  156.             break;
  157.  
  158.        case WM_SIZE:
  159.             break;
  160.  
  161.        case WM_SYSCOMMAND:
  162.             switch (wParam) {
  163.                case IDM_ABOUT:
  164.                     ProcessMessage (hWnd, 0);
  165.                     break;
  166.                default:
  167.                     return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  168.             }
  169.             break;
  170.  
  171.  
  172.        case WM_COMMAND:
  173.             switch (wParam) {
  174.                case IDM_HELP:
  175.                     ProcessMessage (hWnd, 2);
  176.                     break;
  177.              }
  178.             break;
  179.  
  180.        case WM_DESTROY:
  181.             PostQuitMessage (0);
  182.             break;
  183.  
  184.         default:
  185.             return DefWindowProc( hWnd, iMessage, wParam, lParam );
  186.        }
  187.     return 0L;
  188. }
  189.  
  190.