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

  1. /*
  2.  *  SetRect
  3.  *  setrect.c
  4.  *
  5.  *  This program demonstrates the use of the function SetRect.
  6.  *  The SetRect functions creates a new RECT structure with the values
  7.  *  given by the last four parameters.
  8.  *
  9.  */
  10.  
  11. #include "windows.h"
  12.  
  13. /*************************** GLOBAL VARIABLES *************************/
  14. static  HWND    hWnd;
  15.  
  16. /************************** FORWARD REFERENCES ************************/
  17. long FAR PASCAL WindowProc (HWND, unsigned, WORD, LONG);
  18. BOOL FAR PASCAL SetRectTestPaint ();
  19. RECT            NewRect ();
  20.  
  21. /******************** WINMAIN -- Main Windows Procedure ***************/
  22. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  23. HANDLE          hInstance;
  24. HANDLE          hPrevInstance;
  25. LPSTR           lpszCmdLine;
  26. int             cmdShow;
  27. {
  28.                 MSG    msg;
  29.  
  30.         if (!hPrevInstance) {
  31.                 /* ensure that windows know where to find parts of this
  32.                  * task on disk by Registering a window class.  Registering
  33.                  * a class binds an executable name to an internal name,
  34.                  * known to Windows. */
  35.                 WNDCLASS        rClass;
  36.  
  37.                 rClass.lpszClassName    = (LPSTR)"setrect";
  38.                 rClass.hInstance        = hInstance;
  39.                 rClass.lpfnWndProc      = WindowProc;
  40.                 rClass.hCursor          = LoadCursor (NULL, IDC_ARROW);
  41.                 rClass.hIcon            = LoadIcon (hInstance, (LPSTR)"setrect");
  42.                 rClass.lpszMenuName     = (LPSTR) NULL;
  43.         rClass.hbrBackground    = GetStockObject (WHITE_BRUSH);
  44.                 rClass.style            = CS_HREDRAW | CS_VREDRAW;
  45.                 rClass.cbClsExtra       = 0;
  46.                 rClass.cbWndExtra       = 0;
  47.  
  48.                 RegisterClass ((LPWNDCLASS) &rClass);
  49.                 } /* end if this is the 1st task/instance of this program */
  50.  
  51.         hWnd = CreateWindow (   (LPSTR) "setrect", /* Window class name */
  52.                                 (LPSTR) "setrect", /* Window title */
  53.                                 WS_OVERLAPPEDWINDOW,
  54.                                         /* stlye -- WIN 2.x or later */
  55.                                 CW_USEDEFAULT,  /* x -  WIN 2.x or later */
  56.                                 CW_USEDEFAULT,  /* y -  WIN 2.x or later */
  57.                                 CW_USEDEFAULT,  /* cx - WIN 2.x or later */
  58.                                 CW_USEDEFAULT,  /* cy - WIN 2.x or later */
  59.                                 (HWND)NULL,     /* No parent */
  60.                                 (HMENU)NULL,    /* Use the class menu */
  61.                                 (HANDLE)hInstance, /* .EXE file for Class */
  62.                                 (LPSTR)NULL     /* No Params */
  63.                              );
  64.  
  65.     MessageBox (NULL,
  66.         (LPSTR)"This is a demostration of the SetRect Function",
  67.         (LPSTR)"SetRect",
  68.         MB_OK);
  69.  
  70.         ShowWindow (hWnd, cmdShow);    /* Allocate room for the window    */
  71.         UpdateWindow (hWnd);        /* Paint the client area */
  72.  
  73.         while (GetMessage ((LPMSG)&msg, NULL, 0, 0)) {
  74.                 TranslateMessage (&msg);
  75.                 DispatchMessage (&msg);
  76.                 } /* end while more messages */
  77.         exit (msg.wParam);
  78. } /* WINMAIN */
  79. /***************************** WINDOWPROC *****************************/
  80. /*      Every message dispatched to any window of type "setrect" will
  81.  *      result in a call to this procedure.
  82.  */
  83. long FAR PASCAL WindowProc (hWnd, identifier, wParam, lParam)
  84. HWND            hWnd;           /* Intended window */
  85. unsigned        identifier;     /* Message Number */
  86. WORD            wParam;         /* 16 bit param */
  87. LONG            lParam;         /* 32 bit param */
  88. {
  89.  
  90.         switch (identifier) {
  91.         case WM_PAINT:
  92.             SetRectTestPaint ();
  93.             break;
  94.                 default:
  95.                         return (DefWindowProc
  96.                                 (hWnd, identifier, wParam, lParam));
  97.                         break;
  98.                 } /* end switch */
  99.         return (0L);
  100. } /* windowproc */
  101. /************************** PAINTING PROCEDURE *****************************/
  102. /* This function will paint the new rectangle in the client area with a
  103.  * newly created brush. */
  104. BOOL FAR PASCAL SetRectTestPaint ()
  105. {
  106.         PAINTSTRUCT     ps;
  107.         RECT            rect;
  108.         HDC             hDC;
  109.         HBRUSH          hBrush;
  110.  
  111.     hDC = BeginPaint (hWnd, (LPPAINTSTRUCT) &ps);
  112.  
  113.     /* Create a new brush to paint the new rectangle with */
  114.     hBrush = CreateSolidBrush (RGB (0, 0, 0)); /* BLACK = 0,0,0 */
  115.  
  116.     rect = NewRect ();            /* get the new rectangle */
  117.     FillRect (hDC, (LPRECT)&rect, hBrush);    /* color rectangle    */
  118.  
  119.     ValidateRect (hWnd, (LPRECT)NULL);    /* confirm paint    */
  120.     EndPaint (hWnd, (LPPAINTSTRUCT) &ps);
  121.     return TRUE;
  122. }
  123. /********************** SET NEW RECTANGLE - SETRECT *********************/
  124. /* Gets the current coordinates of the client area and returns a
  125.  * smaller rectangle of that will be centered in the client area.
  126.  */
  127. RECT    NewRect ()
  128. {
  129.         RECT    rect;
  130.         int     left;
  131.         int     top;
  132.         int     right;
  133.         int     bottom;
  134.  
  135.     GetClientRect (hWnd, (LPRECT)&rect); /* Get Client Area Coordinates */
  136.     
  137.     left = rect.left + 15;             /* values for smaller rect. */
  138.     top = rect.top + 15;
  139.     right = rect.right - 15;
  140.     bottom = rect.bottom - 15;
  141.  
  142.     /* Create new rectangle with the above coordinates
  143.      * into the rect struct */
  144.     SetRect ((LPRECT)&rect, left, top, right, bottom);
  145.  
  146.     return (rect);
  147. }
  148.