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

  1. /*
  2.  *
  3.  * GetTickCount
  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 couble clicks the right mouse button, the
  8.  *  GetTickCount function is executed.  The time since system startup
  9.  *  is returned and displayed in a message box.
  10.  *
  11.  */
  12.  
  13. #include "windows.h"
  14.  
  15. /* Global Variables */
  16. static HANDLE hInst;
  17. static HWND hWnd;
  18.  
  19. /* FORWARD REFERENCES */
  20. long FAR PASCAL WindowProc (HWND, unsigned, WORD, LONG);
  21.  
  22. /* WINMAIN */
  23. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  24. HANDLE hInstance, hPrevInstance;
  25. LPSTR lpszCmdLine;
  26. int cmdShow;
  27. {
  28.   MSG msg;
  29.  
  30.   if (!hPrevInstance)  {
  31.  
  32.      WNDCLASS rClass;
  33.  
  34.      rClass.lpszClassName = (LPSTR)"gtickcnt";
  35.      rClass.hInstance      = hInstance;
  36.      rClass.lpfnWndProc   = WindowProc;
  37.      rClass.hCursor         = LoadCursor(NULL, IDC_ARROW);
  38.      rClass.hIcon          = LoadIcon(hInstance, IDI_APPLICATION);
  39.      rClass.lpszMenuName  = (LPSTR)NULL;
  40.      rClass.hbrBackground = GetStockObject(WHITE_BRUSH);
  41.      rClass.style            = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  42.      rClass.cbClsExtra      = 0;       /* Add double click capabilities. */
  43.      rClass.cbWndExtra      = 0;
  44.  
  45.      RegisterClass((LPWNDCLASS)&rClass);
  46.  
  47.      }
  48.    else
  49.       ;
  50.  
  51.    hInst = hInstance;
  52.  
  53.    hWnd = CreateWindow((LPSTR) "gtickcnt",
  54.                (LPSTR) "GetTickCount",         /* Create a window.         */
  55.                WS_OVERLAPPEDWINDOW,            /* Make it overlapped.      */
  56.                CW_USEDEFAULT,                  /* Use default coordinates. */
  57.                CW_USEDEFAULT,                  /* Use default coordinates. */
  58.                CW_USEDEFAULT,                  /* Use default coordinates. */
  59.                CW_USEDEFAULT,                  /* Use default coordinates. */
  60.                (HWND)NULL,
  61.                (HMENU)NULL,
  62.                (HANDLE)hInstance,
  63.                (LPSTR)NULL
  64.              );
  65.  
  66.    ShowWindow(hWnd, cmdShow);
  67.  
  68.    UpdateWindow(hWnd);
  69.  
  70.    while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  71.        TranslateMessage(&msg);
  72.        DispatchMessage(&msg);
  73.    }
  74.  
  75.    exit(msg.wParam);
  76.  
  77. } /* WinMain */
  78.  
  79. /* WINDOWPROC */
  80.  
  81. long FAR PASCAL WindowProc(hWnd, identifier, wParam, lParam)
  82. HWND    hWnd;
  83. unsigned identifier;
  84. WORD     wParam;
  85. LONG     lParam;
  86.  
  87. {
  88.    
  89.    switch (identifier) {
  90.  
  91.      case WM_PAINT: {
  92.  
  93.            PAINTSTRUCT ps;
  94.           RECT        rRect;
  95.          HDC        hDC;
  96.  
  97.          hDC=BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  98.          SetMapMode(hDC, MM_ANISOTROPIC);
  99.          SetWindowOrg(hDC, 0, 0);
  100.          SetWindowExt(hDC, 110, 110);
  101.          GetClientRect(hWnd, (LPRECT)&rRect);
  102.          SetViewportOrg(hDC, 0, 0);
  103.          SetViewportExt(hDC, rRect.right, rRect.bottom);
  104.          DrawText(hDC,
  105.                 (LPSTR)"Double Click Right Mouse Button To Conduct Test.",
  106.                 48, (LPRECT)&rRect, DT_SINGLELINE);  /* Place text to    */
  107.          EndPaint(hWnd, (LPPAINTSTRUCT)&ps);          /* prompt for test. */
  108.  
  109.       }
  110.         break;
  111.     
  112.      case WM_RBUTTONDBLCLK:                    /* If the user double   */
  113.        {                                       /* clicks on the right  */
  114.        char szbuf[80];   /* Output buffer       | mouse button, then   */
  115.        DWORD dwCnt;  /* No. of ms since startup | establish needed     */
  116.                                                /* variables and call   */
  117.        dwCnt = GetTickCount();                 /* the GetTickCount     */
  118.                                                /* function.  Capture   */
  119.         sprintf(szbuf,                          /* the time information */
  120.           "%s%s%lu%s\0",                        /* in a zero terminated */
  121.           "The No. of Milliseconds since",     /* buffer.              */
  122.            " system startup is ", dwCnt, ".");  
  123.  
  124.        MessageBox(hWnd,                        /* Output the buffer in */
  125.                   (LPSTR)szbuf,                /* a message box format */
  126.                   (LPSTR)"GetTickCount",       /* 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.