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

  1. /*
  2.  *  SetMapMode
  3.  *
  4.  *  This function demonstrates the use of the SetMapMode function.  It will
  5.  *  create a window, and procede to draw a triangle in the window in
  6.  *  the MM_ANISOTROPIC mapping mode.
  7.  */
  8.  
  9. #include <windows.h>
  10.  
  11. BOOL FAR PASCAL InitSetMapMode (HANDLE, HANDLE, int);
  12. long    FAR PASCAL SetMapModeWindowProc (HANDLE, unsigned, WORD, LONG);
  13.  
  14. int     PASCAL WinMain  (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  15.  
  16. HANDLE hInstance, hPrevInstance;
  17. LPSTR  lpszCmdLine;
  18. int    cmdShow;
  19.   {
  20.   MSG  msg;
  21.  
  22.   InitSetMapMode (hInstance, hPrevInstance, cmdShow);   /*  Init Routine  */
  23.  
  24.   while (GetMessage ( (LPMSG) & msg, NULL, 0, 0))
  25.     {
  26.     TranslateMessage ( (LPMSG) & msg);
  27.     DispatchMessage ( (LPMSG) & msg);
  28.     }
  29.   exit (msg.wParam);
  30.   }
  31.  
  32. BOOL FAR PASCAL InitSetMapMode (hInstance, hPrevInstance, cmdShow)
  33.  
  34. HANDLE hInstance;
  35. HANDLE hPrevInstance;
  36. int    cmdShow;
  37.   {
  38.   WNDCLASS  wcSetMapModeClass;
  39.   HWND    hWnd;
  40.  
  41.   wcSetMapModeClass.lpszClassName = (LPSTR) "SetMapMode";
  42.   wcSetMapModeClass.hInstance     = hInstance;
  43.   wcSetMapModeClass.lpfnWndProc   = SetMapModeWindowProc;
  44.   wcSetMapModeClass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  45.   wcSetMapModeClass.hIcon         = NULL;
  46.   wcSetMapModeClass.lpszMenuName  = (LPSTR) NULL;
  47.   wcSetMapModeClass.hbrBackground = GetStockObject (WHITE_BRUSH);
  48.   wcSetMapModeClass.style         = CS_HREDRAW | CS_VREDRAW;
  49.   wcSetMapModeClass.cbClsExtra    = 0;
  50.   wcSetMapModeClass.cbWndExtra    = 0;
  51.  
  52.   RegisterClass ( (LPWNDCLASS) & wcSetMapModeClass);
  53.  
  54.   hWnd = CreateWindow ( (LPSTR) "SetMapMode", (LPSTR) "SetMapMode",
  55.                       WS_OVERLAPPEDWINDOW,
  56.                       CW_USEDEFAULT,    0,
  57.                       CW_USEDEFAULT,    0,
  58.                       NULL,     NULL,   hInstance, NULL);
  59.  
  60.   ShowWindow (hWnd, cmdShow);
  61.   UpdateWindow (hWnd);
  62.  
  63.   return TRUE;
  64.   }
  65.  
  66. long    FAR PASCAL SetMapModeWindowProc (hWnd, message, wParam, lParam)
  67.  
  68. HWND        hWnd;
  69. unsigned    message;
  70. WORD        wParam;
  71. LONG        lParam;
  72.   {
  73.   switch (message)
  74.     {
  75.     case WM_PAINT:
  76.       PaintSetMapModeWindow (hWnd);
  77.       break;
  78.  
  79.     case WM_DESTROY:
  80.       PostQuitMessage (0);
  81.       break;
  82.  
  83.     default:
  84.       return (DefWindowProc (hWnd, message, wParam, lParam));
  85.       break;
  86.     }
  87.   return (0L);
  88.   }
  89.  
  90. PaintSetMapModeWindow (hWnd)
  91. HWND    hWnd;
  92.   {
  93.   PAINTSTRUCT    ps;
  94.   HDC        hDC;
  95.   POINT     lpTriangle[4];
  96.   HANDLE        hOldBrush, hBrush;
  97.   RECT        rRect;
  98.  
  99.   BeginPaint (hWnd, (LPPAINTSTRUCT) & ps);
  100.   hDC = ps.hdc;
  101.  
  102.   hBrush = GetStockObject (GRAY_BRUSH);
  103.   hOldBrush = SelectObject (hDC, hBrush);
  104.  
  105.   lpTriangle[0].x = 150;
  106.   lpTriangle[0].y = 100;
  107.   lpTriangle[1].x = 100;
  108.   lpTriangle[1].y = 200;
  109.   lpTriangle[2].x = 200;
  110.   lpTriangle[2].y = 200;
  111.  
  112.   SetMapMode (hDC, MM_ANISOTROPIC);     /*  Set the mapping mode             */
  113.  
  114.   SetWindowExt (hDC, 300, 300);        /*  Set the extent of the drawing
  115.                                         *  area.  This is the area that
  116.                                         *  holds graphics that you create
  117.                                         *  with GDI functions.  Do not
  118.                                         *  confuse this function with
  119.                                         *  the actual window.  The
  120.                                         *  SetViewportExt sets the
  121.                                         *  extent of the area to be mapped
  122.                                         *  to which is the actual window
  123.                                         */
  124.   GetClientRect (hWnd, (LPRECT) & rRect);
  125. /*  Get the size of the client area
  126.                       *  so that we can set the viewport
  127.                       *  extent
  128.                       */
  129.  
  130.   SetViewportExt (hDC, rRect.right, rRect.bottom);
  131.  
  132.   Polygon (hDC, lpTriangle, 3);
  133.  
  134.   ValidateRect (hWnd, (LPRECT) NULL);
  135.   EndPaint (hWnd, (LPPAINTSTRUCT) & ps);
  136.  
  137.   SelectObject (hDC, hOldBrush);
  138.   return TRUE;
  139.   }
  140.