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

  1. /*
  2.  *  SetViewportExt
  3.  *  setview.c
  4.  *
  5.  *  This function demonstrates the use of the SetViewportExt function.    It will
  6.  *  create a window, and procede to draw a triangle in the window in
  7.  *  the MM_ANISOTROPIC mapping mode.
  8.  *
  9.  */
  10.  
  11. #include <windows.h>
  12.  
  13. /* Forward Declarations  */
  14.  
  15. BOOL FAR PASCAL InitSetViewportExt ( HANDLE , HANDLE , int );
  16. long FAR PASCAL SetViewportExtWindowProc ( HANDLE , unsigned , WORD , LONG );
  17.  
  18. /*
  19.  *  MAIN PROCEDURE
  20.  */
  21.  
  22. int PASCAL WinMain  (hInstance , hPrevInstance , lpszCmdLine , cmdShow )
  23.  
  24. HANDLE hInstance , hPrevInstance;
  25. LPSTR  lpszCmdLine;
  26. int cmdShow;
  27.   {
  28.   MSG  msg;                /*    Temp buffer to hold message  */
  29.  
  30.   InitSetViewportExt (hInstance, hPrevInstance, cmdShow );  /*    Init Routine  */
  31.  
  32.   while ( GetMessage((LPMSG)&msg, NULL, 0 , 0 ))
  33.     {
  34.     TranslateMessage((LPMSG)&msg);
  35.     DispatchMessage((LPMSG)&msg);     /*  Give Your windowproc the  */
  36.     }                      /*  message            */
  37.  
  38.   exit(msg.wParam);
  39.   }
  40.  
  41. BOOL FAR PASCAL InitSetViewportExt (hInstance , hPrevInstance , cmdShow)
  42.  
  43. HANDLE hInstance;
  44. HANDLE hPrevInstance;
  45. int cmdShow;
  46.  
  47.   {
  48.   WNDCLASS  wcSetViewportExtClass;
  49.   HWND    hWnd;
  50.  
  51.   wcSetViewportExtClass.lpszClassName = (LPSTR) "SetViewportExt";
  52.   wcSetViewportExtClass.hInstance     = hInstance;
  53.   wcSetViewportExtClass.lpfnWndProc   = SetViewportExtWindowProc;
  54.   wcSetViewportExtClass.hCursor       = LoadCursor ( NULL , IDC_ARROW );
  55.   wcSetViewportExtClass.hIcon          = NULL;
  56.   wcSetViewportExtClass.lpszMenuName  = (LPSTR) NULL;
  57.   wcSetViewportExtClass.hbrBackground = GetStockObject (WHITE_BRUSH);
  58.   wcSetViewportExtClass.style          = CS_HREDRAW | CS_VREDRAW;
  59.   wcSetViewportExtClass.cbClsExtra    = 0;
  60.   wcSetViewportExtClass.cbWndExtra    = 0;
  61.  
  62.   RegisterClass ((LPWNDCLASS) &wcSetViewportExtClass);
  63.  
  64.   hWnd = CreateWindow((LPSTR) "SetViewportExt",   /*  Window class name       */
  65.               (LPSTR) "SetViewportExt",   /*  Window title          */
  66.               WS_OVERLAPPEDWINDOW,  /*    Type of window        */
  67.               CW_USEDEFAULT,        /*    x            */
  68.               CW_USEDEFAULT,        /*    y            */
  69.               CW_USEDEFAULT,        /*    cx            */
  70.               CW_USEDEFAULT,        /*    cy            */
  71.               (HWND)NULL,        /*    No parent for this wind */
  72.               (HMENU)NULL,        /*    Use the Class menu    */
  73.               (HANDLE)hInstance,    /*    Who created this window */
  74.               (LPSTR)NULL        /*    No params. to pass on.    */
  75.              );
  76.  
  77.   ShowWindow (hWnd , cmdShow);     /*  Display this window on the screen    */
  78.   UpdateWindow (hWnd);         /*  Cause a paint message        */
  79.  
  80.   return TRUE;
  81.   }
  82.  
  83. /*
  84.  *  THE WINDOW PROCEDURE - Process messages
  85.  */
  86.  
  87. long FAR PASCAL SetViewportExtWindowProc (hWnd , message , wParam , lParam)
  88.  
  89. HWND        hWnd;            /*    Handle of the window    */
  90. unsigned    message;            /*    Message type        */
  91. WORD        wParam;            /*    Message 16-bit param    */
  92. LONG        lParam;            /*    Message 32-bit param    */
  93.   {
  94.   switch (message)            /*    Check the message type    */
  95.     {
  96.     case WM_PAINT:            /*    Process the Paint    */
  97.     PaintSetViewportExtWindow (hWnd); /*  message              */
  98.     break;
  99.  
  100.     case WM_DESTROY:            /*    If close requested    */
  101.     PostQuitMessage(0);        /*      send yourself a quit    */
  102.     break;                /*      message        */
  103.  
  104.     default:
  105.     return( DefWindowProc( hWnd , message , wParam , lParam ) );
  106.     break;
  107.     }
  108.   return( 0L );
  109.   }
  110.  
  111. /*
  112.  *  THE PAINT PROCEDURE
  113.  */
  114.  
  115. PaintSetViewportExtWindow (hWnd)
  116.  
  117. HWND    hWnd;                   /*  Handle of the window  */
  118.   {
  119.   PAINTSTRUCT    ps;
  120.   HDC        hDC;
  121.   POINT     lpTriangle[4];
  122.   HANDLE    hOldBrush , hBrush;          /*  For loading new brushes  */
  123.   RECT        rRect;                  /*  Will hold the client       */
  124.                           /*  Rectangle           */
  125.  
  126.   BeginPaint (hWnd , (LPPAINTSTRUCT) &ps);    /*  Prepare the client area  */
  127.   hDC = ps.hdc;                   /*  Get the Display Context  */
  128.  
  129.   hBrush = GetStockObject ( GRAY_BRUSH );     /*  Get a gray brush       */
  130.   hOldBrush = SelectObject ( hDC , hBrush );  /*  Select the new brush       */
  131.  
  132.   lpTriangle[0].x = 150;    /*    The values of the points  */
  133.   lpTriangle[0].y = 100;
  134.   lpTriangle[1].x = 100;
  135.   lpTriangle[1].y = 200;
  136.   lpTriangle[2].x = 200;
  137.   lpTriangle[2].y = 200;
  138.  
  139.   SetMapMode ( hDC , MM_ANISOTROPIC );     /*  Set the mapping mode          */
  140.  
  141.   SetWindowExt ( hDC , 300 , 300 );     /*  Set the extent of the drawing
  142.                       *  area.  This is the area that
  143.                       *  holds graphics that you create
  144.                       *  with GDI functions.  Do not
  145.                       *  confuse this function with
  146.                       *  the actual window.  The
  147.                       *  SetViewportExt sets the
  148.                       *  extent of the area to be mapped
  149.                       *  to which is the actual window
  150.                       */
  151.  
  152.   GetClientRect ( hWnd , (LPRECT) &rRect );
  153.                      /*  Get the size of the client area
  154.                       *  so that we can set the viewport
  155.                       *  extent
  156.                       */
  157.  
  158.   SetViewportExt ( hDC , rRect.right , rRect.bottom );
  159.                      /*  Set the Extent of the viewport   */
  160.  
  161.   Polygon ( hDC , lpTriangle , 3 );     /*  Draw the triangle              */
  162.  
  163.   ValidateRect (hWnd , (LPRECT) NULL);     /*  Disable any more paint messages  */
  164.   EndPaint (hWnd, (LPPAINTSTRUCT) &ps );
  165.  
  166.   SelectObject( hDC , hOldBrush );     /*  Replace the old brush  */
  167.   return TRUE;
  168.   }
  169.