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

  1. /*
  2.  *
  3.  *  GetMessage
  4.  *
  5.  *  This program demonstrates the use of the GetMessage function.  The
  6.  *   program registers and creates a window.  Then, it goes into the
  7.  *   GetMessage loop.  On the screen is the instruction to double click
  8.  *   the right mouse button.  This conducts the test of GetMessage by
  9.  *   displaying a message box from within the WM_RBOTTONDBLCLK section
  10.  *   of the window procedure .
  11.  *
  12.  */
  13.  
  14. #include "windows.h"
  15.  
  16. /* Global Variables */
  17. static HANDLE hInst;
  18. static HWND hWnd;
  19.  
  20. /* FORWARD REFERENCES */
  21. long FAR PASCAL WindowProc (HWND, unsigned, WORD, LONG);
  22.  
  23. /* WINMAIN */
  24. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  25. HANDLE hInstance, hPrevInstance;
  26. LPSTR lpszCmdLine;
  27. int cmdShow;
  28. {
  29.   MSG msg;
  30.  
  31.   if (!hPrevInstance)  {
  32.  
  33.      WNDCLASS rClass;
  34.  
  35.      rClass.lpszClassName = (LPSTR)"getmsg";
  36.      rClass.hInstance      = hInstance;
  37.      rClass.lpfnWndProc   = WindowProc;
  38.      rClass.hCursor         = LoadCursor(NULL, IDC_ARROW);
  39.      rClass.hIcon            = LoadIcon(hInstance, IDI_APPLICATION);
  40.      rClass.lpszMenuName  = (LPSTR)NULL;
  41.      rClass.hbrBackground = GetStockObject(WHITE_BRUSH);
  42.  
  43.      /* Permit the window class to accept double click messages by
  44.         using the CS_DBLCLKS style.                                */
  45.  
  46.      rClass.style            = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  47.      rClass.cbClsExtra      = 0;
  48.      rClass.cbWndExtra      = 0;
  49.  
  50.      RegisterClass((LPWNDCLASS)&rClass);
  51.  
  52.      }
  53.    else
  54.       ;
  55.  
  56.    hInst = hInstance;
  57.  
  58.    hWnd = CreateWindow((LPSTR) "getmsg",
  59.                (LPSTR) "GetMessage",
  60.                WS_OVERLAPPEDWINDOW,          /* Use an overlapping window. */
  61.                CW_USEDEFAULT,                /* Use default coordinates.   */
  62.                CW_USEDEFAULT,                /* Use default coordinates.   */
  63.                CW_USEDEFAULT,                /* Use default coordinates.   */
  64.                CW_USEDEFAULT,                /* Use default coordinates.   */
  65.                (HWND)NULL,
  66.                (HMENU)NULL,
  67.                (HANDLE)hInstance,
  68.                (LPSTR)NULL
  69.              );
  70.  
  71.    ShowWindow(hWnd, cmdShow);
  72.  
  73.    UpdateWindow(hWnd);
  74.  
  75.    /* Go into the GetMessage loop at this time.  The user can then
  76.       double click on the right mouse button when the cursor is in
  77.       the client area of this application.                         */
  78.  
  79.    while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  80.        TranslateMessage(&msg);
  81.        DispatchMessage(&msg);
  82.    }
  83.  
  84.    exit(msg.wParam);
  85.  
  86. } /* WinMain */
  87.  
  88. /* WINDOWPROC */
  89.  
  90. long FAR PASCAL WindowProc(hWnd, identifier, wParam, lParam)
  91. HWND    hWnd;
  92. unsigned identifier;
  93. WORD     wParam;
  94. LONG     lParam;
  95.  
  96. {
  97.    switch (identifier) {
  98.  
  99.      case WM_PAINT: {
  100.           RECT        rRect;
  101.           PAINTSTRUCT ps;
  102.           HDC        hDC;
  103.  
  104.           hDC=BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  105.            SetMapMode(hDC, MM_ANISOTROPIC);
  106.            SetWindowOrg(hDC, 0, 0);
  107.            SetWindowExt(hDC, 110, 110);
  108.            GetClientRect(hWnd, (LPRECT)&rRect);
  109.            SetViewportOrg(hDC, 0, 0);
  110.            SetViewportExt(hDC, rRect.right, rRect.bottom);
  111.  
  112.          /* Display the instructions in the client area for the
  113.             user to conduct the test.                            */
  114.          
  115.            DrawText(hDC,
  116.                   (LPSTR)"Double Click Right Mouse Button to Conduct Test.",
  117.                   48,
  118.                   (LPRECT)&rRect,
  119.                   DT_SINGLELINE);
  120.            EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  121.  
  122.       }
  123.       break;
  124.  
  125.      /* Check to see if the user has double clicked the right
  126.         mouse button.                                          */
  127.  
  128.     case WM_RBUTTONDBLCLK:        /* If so, display a success message box. */
  129.       MessageBox(hWnd,
  130.                  (LPSTR)"GetMessage Retrieved a WM_RBUTTONDBLCLK Message",
  131.                  (LPSTR)"GetMessage",
  132.                  MB_OK);
  133.       break;                    /* And return for more message processing. */
  134.  
  135.     case WM_CLOSE:
  136.        DestroyWindow(hWnd);
  137.        break;
  138.  
  139.     case WM_DESTROY:
  140.        PostQuitMessage(0);
  141.        break;
  142.  
  143.     default:
  144.        return(DefWindowProc(hWnd, identifier, wParam, lParam));
  145.        break;
  146.  
  147.    }
  148.  
  149.    return(0L);
  150.  
  151. }
  152.