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

  1. /*
  2.  
  3. Function(s) demonstrated in this program: SetWindowLong
  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 window structure.  These 4 extra bytes are used to store a long
  17.    value corresponding to the number of left mouse button clicks.
  18.    This value is updated using GetWindowLong and SetWindowLong.
  19.    
  20.  
  21. */
  22.  
  23. #define NOMINMAX    
  24. #include <stdlib.h>
  25. #include <windows.h>
  26. #include "SetWl.h"
  27. #include "string.h"
  28. #include "stdio.h"
  29.  
  30.        char         szAppName             [] = "SetWl"             ;
  31.        HANDLE       hInstMain                                      ;
  32.        HWND         hWndMain                                       ;
  33.        char         szOutputBuffer1       [70]                     ;
  34.        char         szOutputBuffer2       [500]                    ;
  35.        HBITMAP      hBitmapHelp                                    ;
  36.  
  37.  
  38. /****************************************************************************/
  39. /************************    Message Structure      *************************/
  40. /****************************************************************************/
  41.  
  42. struct { char *szMessage; }
  43.        Messages [] = {
  44. "About",
  45. "     This is a sample application to demonstrate the\n\
  46. use  of  the  SetWindowLong  Windows function.",
  47.  
  48. "Help Message",
  49. "     This program uses GetWindowLong and\n\
  50. SetWindowLong to keep track of the number of left\n\
  51. button mouse click message.  A message box shows\n\
  52. how many every time one is sent.  Click the left\n\
  53. mouse button to test this function.",
  54.  
  55. };    
  56.  
  57. /****************************************************************************/
  58.  
  59. void ProcessMessage (HWND, int); 
  60.  
  61. void ProcessMessage (hWnd, MessageNumber) 
  62.      HWND     hWnd;
  63.      int      MessageNumber;
  64. {
  65.      sprintf (szOutputBuffer1, "%s", Messages [MessageNumber]);
  66.      sprintf (szOutputBuffer2, "%s", Messages [MessageNumber + 1]);
  67.      MessageBox (hWnd, szOutputBuffer2, szOutputBuffer1, MB_OK);
  68. }       
  69.  
  70. /****************************************************************************/
  71.  
  72. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  73.     HANDLE    hInstance, hPrevInstance;
  74.     LPSTR    lpszCmdLine;
  75.     int     nCmdShow;
  76.     {
  77.     HWND    hWnd;
  78.     MSG     msg;
  79.     WNDCLASS    wndclass;
  80.     LONG    WindowCount;
  81.  
  82.  
  83.     if (!hPrevInstance)
  84.     {
  85.     wndclass.style             = CS_HREDRAW | CS_VREDRAW | CS_SAVEBITS;
  86.     wndclass.lpfnWndProc    = WndProc;
  87.     wndclass.cbClsExtra     = 0;
  88.     wndclass.cbWndExtra     = 4;
  89.     wndclass.hInstance      = hInstance;
  90.     wndclass.hIcon             = NULL; 
  91.    wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW) ;
  92.     wndclass.hbrBackground    = GetStockObject (WHITE_BRUSH);
  93.     wndclass.lpszMenuName    = szAppName;
  94.     wndclass.lpszClassName    = szAppName;
  95.  
  96.     if (!RegisterClass (&wndclass) )
  97.         return FALSE;
  98.     }
  99.  
  100.     hWnd = CreateWindow (szAppName, "SetWindowLong",
  101.            WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
  102.            NULL, NULL, hInstance, NULL);
  103.        
  104.     ShowWindow (hWnd, nCmdShow);
  105.     UpdateWindow (hWnd);
  106.  
  107.     while (GetMessage (&msg, NULL, 0, 0))
  108.         {
  109.         TranslateMessage (&msg);
  110.         DispatchMessage (&msg);
  111.         }
  112.     return msg.wParam;
  113. }
  114.  
  115. /****************************************************************************/
  116.  
  117. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  118.    HWND      hWnd;
  119.    unsigned  iMessage;
  120.    WORD      wParam;
  121.    LONG      lParam;
  122.    {
  123.    HMENU     hMenu;
  124.  
  125.    switch (iMessage)
  126.        {
  127.        case WM_CREATE:
  128.             hMenu = GetSystemMenu (hWnd, FALSE);
  129.  
  130.             ChangeMenu (hMenu, NULL, "&About", IDM_ABOUT, 
  131.                         MF_APPEND | MF_STRING);
  132.             break;
  133.  
  134.        case WM_LBUTTONUP:
  135.             SetWindowLong (hWnd, 0, GetWindowLong (hWnd, 0) + 1);
  136.             sprintf (szOutputBuffer1, 
  137.                      "Number of left mouse button double clicks = %i",
  138.                       GetWindowLong (hWnd, 0));
  139.             MessageBox (hWnd, szOutputBuffer1, "SetWindowLong", MB_OK);
  140.             break;
  141.  
  142.        case WM_SYSCOMMAND:
  143.             switch (wParam) {
  144.                case IDM_ABOUT:
  145.                     ProcessMessage (hWnd, 0);
  146.                     break;
  147.                default:
  148.                     return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  149.             }
  150.             break;
  151.  
  152.  
  153.        case WM_COMMAND:
  154.             switch (wParam) {
  155.                case IDM_HELP:
  156.                     ProcessMessage (hWnd, 2);
  157.                     break;
  158.              }
  159.             break;
  160.  
  161.        case WM_DESTROY:
  162.             PostQuitMessage (0);
  163.             break;
  164.  
  165.         default:
  166.             return DefWindowProc( hWnd, iMessage, wParam, lParam );
  167.        }
  168.     return 0L;
  169. }
  170.  
  171.