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

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