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

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