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

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