home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / mouse / gmesspos.c < prev    next >
C/C++ Source or Header  |  1988-08-10  |  5KB  |  151 lines

  1. /*
  2.  *
  3.  * GetMessagePos
  4.  *
  5.  * This program registers a window and creates it on the screen.  The
  6.  *  program then creates the window, shows the window, and then updates
  7.  *  the window.  If the user proceeds to close the window, the GetMessagePos
  8.  *  function is executed.  The mouse coordinates are then displayed on
  9.  *  the screen in a message box before the window is actually closed and
  10.  *  destroyed.
  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)"gmesspos";
  36.      rClass.hInstance      = hInstance;
  37.      rClass.lpfnWndProc   = WindowProc;
  38.      rClass.hCursor         = LoadCursor(NULL, IDC_ARROW);
  39.      rClass.hIcon          = LoadIcon(hInstance, (LPSTR)"simpleton");
  40.      rClass.lpszMenuName  = (LPSTR)NULL;
  41.      rClass.hbrBackground = GetStockObject(WHITE_BRUSH);
  42.      rClass.style          = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  43.      rClass.cbClsExtra      = 0;
  44.      rClass.cbWndExtra      = 0;
  45.  
  46.      RegisterClass((LPWNDCLASS)&rClass);
  47.  
  48.      }
  49.    else
  50.       ;
  51.  
  52.    hInst = hInstance;
  53.  
  54.    hWnd = CreateWindow((LPSTR) "gmesspos",
  55.                (LPSTR) "GetMessagePos",
  56.                WS_OVERLAPPEDWINDOW,         /* Use overlapped window.   */
  57.                CW_USEDEFAULT,               /* Use default coordinates. */
  58.                CW_USEDEFAULT,               /* Use default coordinates. */
  59.                CW_USEDEFAULT,               /* Use default coordinates. */
  60.                CW_USEDEFAULT,               /* Use default coordinates. */
  61.                (HWND)NULL,
  62.                (HMENU)NULL,
  63.                (HANDLE)hInstance,
  64.                (LPSTR)NULL
  65.              );
  66.  
  67.    ShowWindow(hWnd, cmdShow);
  68.  
  69.    UpdateWindow(hWnd);
  70.  
  71.    while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  72.        TranslateMessage(&msg);
  73.        DispatchMessage(&msg);
  74.    }
  75.  
  76.    exit(msg.wParam);
  77.  
  78. } /* WinMain */
  79.  
  80. /* WINDOWPROC */
  81.  
  82. long FAR PASCAL WindowProc(hWnd, identifier, wParam, lParam)
  83. HWND    hWnd;
  84. unsigned identifier;
  85. WORD     wParam;
  86. LONG     lParam;
  87.  
  88. {
  89.    
  90.    switch (identifier) {
  91.  
  92.      case WM_PAINT: {
  93.  
  94.            PAINTSTRUCT ps;
  95.           RECT        rRect;
  96.          HDC        hDC;
  97.  
  98.          hDC=BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);       /* Paint a window  */
  99.          SetMapMode(hDC, MM_ANISOTROPIC);                /* which will tell */
  100.          SetWindowOrg(hDC, 0, 0);                        /* the user to     */
  101.          SetWindowExt(hDC, 110, 110);                    /* double click    */
  102.          GetClientRect(hWnd, (LPRECT)&rRect);            /* the right mouse */
  103.          SetViewportOrg(hDC, 0, 0);                      /* button to con-  */
  104.          SetViewportExt(hDC, rRect.right, rRect.bottom); /* the test.       */
  105.          DrawText(hDC,
  106.                 (LPSTR)"Double Click Right Mouse Button to Conduct Test.",
  107.                 48, (LPRECT)&rRect, DT_SINGLELINE);
  108.          EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  109.  
  110.       }
  111.         break;
  112.  
  113.     case WM_RBUTTONDBLCLK: {                  /* If the user double       */
  114.                                               /*  clicks the right        */
  115.        char szbuf[32];                        /*  mouse button, then      */
  116.        unsigned long ptPos;                   /*  an output buffer is     */
  117.        POINT point;                           /*  allocated along with    */
  118.                                               /*  a pointer to a position */
  119.        ptPos = GetMessagePos();               /*  and a point.  The fn    */
  120.         point = MAKEPOINT(ptPos);              /*  GetMessagePos is exec-  */
  121.         sprintf(szbuf,                         /*  uted and the mouse      */
  122.                "%s%d%s%d%s",                  /*  position is printed in  */
  123.                "The Mouse Position is ",      /*  a messge box.           */
  124.                 point.x, ",", point.y, ".");
  125.         MessageBox(hWnd,
  126.                   (LPSTR)szbuf,
  127.                   (LPSTR)"GetMessagePos",
  128.                    MB_OK);
  129.        }
  130.        break;
  131.  
  132.     case WM_CLOSE: {
  133.  
  134.           DestroyWindow(hWnd);
  135.        }
  136.          break;
  137.     
  138.     case WM_DESTROY:
  139.         PostQuitMessage(0);
  140.         break;
  141.  
  142.     default:
  143.         return(DefWindowProc(hWnd, identifier, wParam, lParam));
  144.         break;
  145.  
  146.    }
  147.  
  148.    return(0L);
  149.  
  150. }
  151.