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

  1. /*
  2.  *  MoveTo
  3.  *
  4.  *  This program will demonstrate the use of the MoveTo function.  It will
  5.  *  draw a triangle using MoveTo and LineTo.
  6.  */
  7.  
  8. #include <windows.h>
  9.  
  10. BOOL FAR PASCAL InitMoveTo (HANDLE, HANDLE, int);
  11. long    FAR PASCAL MoveToWindowProc (HANDLE, unsigned, WORD, LONG);
  12.  
  13. int     PASCAL WinMain  (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  14. HANDLE   hInstance, hPrevInstance;
  15. LPSTR    lpszCmdLine;
  16. int      cmdShow;
  17.   {
  18.   MSG  msg;
  19.  
  20.   InitMoveTo (hInstance, hPrevInstance, cmdShow);  /*  Init Routine  */
  21.   while (GetMessage ( (LPMSG) & msg, NULL, 0, 0))
  22.     {
  23.     TranslateMessage ( (LPMSG) & msg);
  24.     DispatchMessage ( (LPMSG) & msg);
  25.     }
  26.   exit (msg.wParam);
  27.   }
  28.  
  29. BOOL FAR PASCAL InitMoveTo (hInstance, hPrevInstance, cmdShow)
  30. HANDLE   hInstance;
  31. HANDLE   hPrevInstance;
  32. int      cmdShow;
  33.   {
  34.   WNDCLASS  wcMoveToClass;
  35.   HWND      hWnd;
  36.  
  37.   wcMoveToClass.lpszClassName = (LPSTR) "MoveTo";
  38.   wcMoveToClass.hInstance     = hInstance;
  39.   wcMoveToClass.lpfnWndProc   = MoveToWindowProc;
  40.   wcMoveToClass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  41.   wcMoveToClass.hIcon          = NULL;
  42.   wcMoveToClass.lpszMenuName  = (LPSTR) NULL;
  43.   wcMoveToClass.hbrBackground = GetStockObject (WHITE_BRUSH);
  44.   wcMoveToClass.style          = CS_HREDRAW | CS_VREDRAW;
  45.   wcMoveToClass.cbClsExtra    = 0;
  46.   wcMoveToClass.cbWndExtra    = 0;
  47.  
  48.   RegisterClass ( (LPWNDCLASS) & wcMoveToClass);
  49.  
  50.   hWnd = CreateWindow ( (LPSTR) "MoveTo", (LPSTR) "MoveTo",
  51.                       WS_OVERLAPPEDWINDOW,
  52.                       CW_USEDEFAULT,    0,
  53.                       CW_USEDEFAULT,    0,
  54.                       NULL,     NULL,   hInstance, NULL);
  55.  
  56.   ShowWindow (hWnd, cmdShow);
  57.   UpdateWindow (hWnd);
  58.  
  59.   return TRUE;
  60.   }
  61.  
  62. long    FAR PASCAL MoveToWindowProc (hWnd, message, wParam, lParam)
  63. HWND        hWnd;
  64. unsigned    message;
  65. WORD        wParam;
  66. LONG        lParam;
  67.   {
  68.   switch (message)
  69.     {
  70.     case WM_PAINT:
  71.       PaintMoveToWindow (hWnd);
  72.       break;
  73.  
  74.     case WM_DESTROY:
  75.       PostQuitMessage (0);
  76.       break;
  77.  
  78.     default:
  79.       return (DefWindowProc (hWnd, message, wParam, lParam));
  80.       break;
  81.     }
  82.   return (0L);
  83.   }
  84.  
  85. PaintMoveToWindow (hWnd)
  86. HWND    hWnd;
  87.   {
  88.   int   cX1, cY1, cX2, cY2;
  89.   int   xinc, yinc;
  90.  
  91.   PAINTSTRUCT    ps;
  92.   HDC        hDC;
  93.   RECT        rRect;
  94.  
  95.   BeginPaint (hWnd, (LPPAINTSTRUCT) & ps);  /* Prepare the client area */
  96.   hDC = ps.hdc;                /*  Get the Display Context  */
  97.  
  98.   GetClientRect (hWnd, (LPRECT) & rRect);
  99. /*    Get the size of the client area  */
  100.  
  101.   cX1 = rRect.left;         /*  Get the left side of the client area  */
  102.   cY1 = rRect.top;         /*  Get the top of the client area  */
  103.   cX2 = rRect.right;         /*  Get the right side of the client area    */
  104.   cY2 = rRect.bottom;         /*  Get the bottom of the client area  */
  105.  
  106.   xinc = (cX2 - cX1) / 25;   /*  Figure out how much to increment  */
  107.   yinc = (cY2 - cY1) / 25;  /*  x and y, evenly  */
  108.  
  109.   while (cX1 < rRect.right)
  110.     {
  111.     MoveTo (hDC, cX1, rRect.top);
  112. /*  Move the pen to the desired location  */
  113.     LineTo (hDC, rRect.right, cY1);
  114. /*  Draw the line to the other side of the client area  */
  115.     MoveTo (hDC, cX2, rRect.bottom);
  116. /*  Move the pen to the desired location  */
  117.     LineTo (hDC, rRect.left, cY2);
  118. /*  Draw the line to the other side of the client area  */
  119.  
  120.     MoveTo (hDC, rRect.right, cY1);
  121. /*  Move the pen to the desired location  */
  122.     LineTo (hDC, cX2, rRect.bottom);
  123. /*  Draw the line to the other side of the client area  */
  124.     MoveTo (hDC, rRect.left, cY2);
  125. /*  Move the pen to the desired location  */
  126.     LineTo (hDC, cX1, rRect.top);
  127. /*  Draw the line to the other side of the client area  */
  128.  
  129.     cX1 += xinc;
  130.     cY1 += yinc;
  131.     cX2 -= xinc;
  132.     cY2 -= yinc;
  133.     }
  134.   ValidateRect (hWnd, (LPRECT) NULL);   /*  Disable any more paint messages  */
  135.   EndPaint (hWnd, (LPPAINTSTRUCT) & ps);
  136.  
  137.   return TRUE;
  138.   }
  139.