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

  1. /*
  2.  *
  3.  * GetMessageTime
  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 time (in milliseconds) since the last
  9.  *  are then displayed on the screen in a message box before the window
  10.  *  is actually closed and 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)"gmesstim";
  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.      rClass.style            = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  43.      rClass.cbClsExtra      = 0;       /* Add double click capabilities. */
  44.      rClass.cbWndExtra      = 0;
  45.  
  46.      RegisterClass((LPWNDCLASS)&rClass);
  47.  
  48.      }
  49.    else
  50.       ;
  51.  
  52.    hInst = hInstance;
  53.  
  54.    hWnd = CreateWindow((LPSTR) "gmesstim",
  55.                (LPSTR) "GetMessageTime",       /* Create a window.         */
  56.                WS_OVERLAPPEDWINDOW,            /* Make it overlapped.      */
  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);
  99.          SetMapMode(hDC, MM_ANISOTROPIC);
  100.          SetWindowOrg(hDC, 0, 0);
  101.          SetWindowExt(hDC, 110, 110);
  102.          GetClientRect(hWnd, (LPRECT)&rRect);
  103.          SetViewportOrg(hDC, 0, 0);
  104.          SetViewportExt(hDC, rRect.right, rRect.bottom);
  105.          DrawText(hDC,
  106.                 (LPSTR)"Double Click Right Mouse Button To Conduct Test.",
  107.                 48, (LPRECT)&rRect, DT_SINGLELINE);  /* Place text to    */
  108.          EndPaint(hWnd, (LPPAINTSTRUCT)&ps);          /* prompt for test. */
  109.  
  110.       }
  111.         break;
  112.     
  113.      case WM_RBUTTONDBLCLK:                    /* If the user double   */
  114.        {                                       /* clicks on the right  */
  115.        char szbuf[40];   /* Output buffer       | mouse button, then   */
  116.        long lTime;   /* Time since last message | establish needed     */
  117.                                                /* variables and call   */
  118.        lTime = GetMessageTime();               /* the GetMessageTime   */
  119.         sprintf(szbuf,                          /* function.  Capture   */
  120.                "%s%ld%s\0",                    /* the time information */
  121.                "Time Since Last Message is ",  /* in a zero terminated */
  122.                 lTime,                          /* buffer.              */   
  123.                ".");                           
  124.         MessageBox(hWnd,                        /* Output the buffer in */
  125.                   (LPSTR)szbuf,                /* a message box format */
  126.                   (LPSTR)"GetMessageTime",     /* so that the user can */
  127.                    MB_OK);                      /* have a readable and  */
  128.        }                                       /* useful format.       */
  129.        break;
  130.  
  131.      case WM_CLOSE: {
  132.  
  133.           DestroyWindow(hWnd);
  134.        }
  135.          break;
  136.     
  137.     case WM_DESTROY:
  138.         PostQuitMessage(0);
  139.         break;
  140.  
  141.     default:
  142.         return(DefWindowProc(hWnd, identifier, wParam, lParam));
  143.         break;
  144.  
  145.    }
  146.  
  147.    return(0L);
  148.  
  149. }
  150.