Accessories for Applications and Windows


The Location and Size of a Window


The Location of a Window


To locate things that display on the monitor screen, the computer uses a coordinate system similar to the Cartesian's but the origin is located on the top left corner of the screen. Using this coordinate system, any point can be located by its distance from the top left corner of the screen of the horizontal and the vertical axes:


Illustration of Window Origin

To manage such distances, the operating system uses a point that is represented by the horizontal and the vertical distances. The Win32 library provides a structure called POINT and defines it as follows:

typedef struct tagPOINT
    LONG x;
    LONG y;

The x member variable is the distance from the left border of the screen to the point. The y variable represents the distance from the top border of the screen to the point.

Besides the Win32's POINT structure, the Microsoft Foundation Class (MFC) library provides the CPoint class. This provides the same functionality as the POINT structure. As a C++ class, it adds more functionality needed to locate a point.

The CPoint::CPoint() default constructor can be used to declare a point variable without specifying its location. If you know the x and y coordinates of a point, you can use the following constructor to create a point:

CPoint(int X, int Y); 

The Size of a Window

While a point is used to locate an object on the screen, each window has a size. The size provides two measures related to an object. The size of an object can be represented as follows:

Illustration of Window Location

The width of an object, represented as CX, is the distance from its left to its right borders and is provided in pixels. The height of an object, represented as CY is the distance from its top to its bottom borders and is given in pixels.

To represent these measures, the Win32 library uses the SIZE structure defined as follows:

typedef struct tagSIZE {
    int cx;
    int cy;

Besides the Win32's SIZE structure, the MFC provides the CSize class. This class has the same functionality as SIZE but adds features of a C++ class. For example, it provides five constructors that allows you to create a size variable in any way of your choice. The constructors are:

CSize(int initCX, int initCY);
CSize(SIZE initSize);
CSize(POINT initPt);
CSize(DWORD dwSize);

The default constructor allows you to declare a CSize variable whose values are not yet available. The constructor that takes two arguments allows you to provide the width and the height to create a CSize variable. If you want another CSize or a SIZE variables, you can use the CSize(SIZE initSize) constructor to assign its values to your variable. You can use the coordinates of a POINT or a CPoint variable to create and initialize a CSize variable. When we study the effects of the mouse, we will know you can use the coordinates of the position of a mouse pointer. These coordinates can help you define a CSize variable.

Besides the constructors, the CSize class is equipped with different member functions that can be used to perform various operations on CSize and SIZE objects. For example, you can add two sizes to get a new size. You can also compare two sizes to find out whether they are the same. 

A Combination of Location and Size

Remember that, to let you create a window, the CWnd class provides a member function named Create and whose syntax is:

virtual BOOL Create(
   LPCTSTR lpszClassName,
   LPCTSTR lpszWindowName,
   DWORD dwStyle,
   const RECT& rect,
   CWnd* pParentWnd,
   UINT nID,
   CCreateContext* pContext = NULL

When a window displays, it can be identified on the screen by its location with regards to the borders of the monitor. A window can also be identified by its width and height. These characteristics are specified or controlled by the rect argument of the Create() member function of a control's class. This argument is a rectangle that can be created through the Win32 RECT structure. First, you must know how Microsoft Windows represents a rectangle.

A rectangle is a geometric figure with four sides or borders: top, right, bottom, and left. A window is also recognized as a rectangle. Therefore, it can be represented as follows:

Illustration of Window Origin and Location

Microsoft Windows represents items as a coordinate system whose origin (0, 0) is located on the top-left corner of the screen. Everything else is positioned from that point. Therefore:

  • The distance from the left border of the screen to left border of a window represents the left measurement
  • The distance from the top border of the screen to the top border of a window is the top measurement.
  • The distance from the left border of the screen to the right border of a window represents the right measurement
  • The distance from the top border of the screen to the bottom border of a window is the top measurement.

To represent these distances, the Win32 API provides a structure called RECT and defined as follows:

typedef struct _RECT { 
    LONG left;
    LONG top;
    LONG right;
    LONG bottom;

This structure can be used to control the location of a window on a coordinate system. It can also be used to control or specify the dimensions of a window. To use it, specify a natural numeric value for each of its member variables and pass its variable as the rect argument of the Create() member function. Here is an example:

RECT Recto;

Recto.left = 100;
Recto.top = 120;
Recto.right = 620;
Recto.bottom = 540;

Besides the Win32 RECT structure, the MFC provides an alternative to represent a rectangle on a window. This is done as follows:

Illustration of Window Location, Origin, and Size

Besides the left, top, right, and bottom measures, a window is also recognized for its width and its height. The width is the distance from the left to the right borders of a rectangle. The height is the distance from the top to the bottom borders of a rectangle. To recognize all these measures, the Microsoft Foundation Classes library provides a class called CRect. This class provides various constructors for any way you want to represent a rectangle. The CRect class provides the following constructors:

CRect(int l, int t, int r, int b);
CRect(const RECT& srcRect);
CRect(LPCRECT lpSrcRect);
CRect(POINT point, SIZE size);
CRect(POINT topLeft, POINT bottomRight);

The default constructor is used to declare a CRect variable whose values are not known. On the other hand, if you know the left, the top, the right, and the bottom measures of the rectangle, as seen on the RECT structure, you can use them to declare and initialize a rectangle. In the same way, you can assign a CRect, a RECT, a pointer to CRect, or a pointer to a RECT variable to create or initialize a CRect instance.

If you do not know or would not specify the location and dimension of a window, you can specify the value of the rect argument as rectDefault. In this case, the compiler would use its own internal value for this argument.


Introduction to Colors



Microsoft Windows is a graphical operating system. It uses colors to present the windows, controls, and other objects on the monitor. The operating system uses a series of colors defined in the Window Color and Appearance dialog box of the Control Panel:

Windows Color and Appearance


Microsoft Windows considers a color as a combination of three numeric values are used to create or specify a color. Each one of these values is 8 bits. The first number is called red. The second is called green. The third is called blue:

7 6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
7 6 5 4 3 2 1 0

Converted to decimal, each one of these numbers would produce:

27 + 26 + 25 + 24 + 23 + 22 + 21 + 20 
= 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1
= 255

Therefore, each number can have a value that ranges from 0 to 255 in the decimal system. These three numbers are combined to produce a single value as follows:

23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Blue Green Red

Converted to decimal, this number has a value of 255 * 255 * 255 = 16581375. This means that we can have approximately 16 million colors available.

Creating a Color

Microsoft Windows characterizes a color as a 32-bit long integer value. Therefore, a color is actually a combination of 32 bits. The bits of the most significant byte (the left byte) are reserved for the operating system's internal use and must be set to 0. Based on this, each color is characterized by its combination of a red, a green, and a blue values.

The 32-bit numeric value used by the Win32 library to characterize a color is defined as the COLORREF data type:


You can use the COLORREF type to declare a color variable. Here is an example:


As you can see, the COLORREF is just a32-bit unsigned integer. When or after declaring such a variable, you can initialize it with a 32-bit decimal value. Here is an example:

COLORREF ClrMine = 1637623;

Characteristics of Colors


The Color as a Combination of Red, Green, and Blue Values

Although the above number (1637623) is a legitimate color value, it is difficult to identify and predict as its red, green, and blue values are not known. To create a color value, the Win32 API provides the RGB macro. Its syntax is:

COLORREF RGB(BYTE byRed, BYTE byGreen, BYTE byBlue);

The RGB macro behaves like a function and requires three numeric values separated by a comma. Each value must range between 0 and 255 both included. Using RGB, the above initialization can be done as follows:

COLORREF ClrMine = RGB(247, 252, 24);

Decoding a Color

Whether a color was initialized with a 32-bit long integer, the RGB macro, or a valid color name, if you want to retrieve the red, green, and blue values of a color, you can use the GetRValue(), the GetGValue(), and/or the GetBValue() macros to extract the value of each. The syntaxes of these macros are:

BYTE GetRValue(DWORD rgb);
BYTE GetGValue(DWORD rgb);
BYTE GetBValue(DWORD rgb);

Each macro takes a 32-bit value as argument, rgb. The GetRValue() macro returns the red value of the rgb parameter. The GetGValue() macro returns the green value of the rgb number. The GetBValue() macro returns the blue value of rgb.

Color Identification

When all three red, green, and blue numbers of a color have their lowest value, which is 0, the color is referred to as black. When the numbers are at their highest value, which is 255, the color qualifies as white.

Just like you, because users are able and free to change the colors of their computer, it is almost impossible to predict the appearance of these colors on someone else’s machine. Fortunately, if you want to use one of the systeme-defined colors, you can ask your application to check its value on the user’s computer. To do this, you can call the GetSysColor() function. Its syntax is:

DWORD GetSysColor(int nIndex);

This function receives a constant value that is defined in the operating system representing one of the appearance’s colors and returns the 32-bit value of that color. The colors defined in Control Panel can be passed as the nIndex argument of the GetSysColor() function are:

System Color Role: Color of System Color - nIndex
3D Objects Background COLOR_3DFACE
3D Objects: Top and Left Edges COLOR_3DHILIGHT
3D Objects: Right and Bottom Edges COLOR_3DDKSHADOW
3D Effect of Buttons and Dialog Boxes: Top and left edges COLOR_3DLIGHT
3D Effect of Buttons and Dialog Boxes: Right and bottom edges COLOR_3DSHADOW
Background of Buttons and Dialog Boxes COLOR_BTNFACE
Text of Buttons COLOR_BTNTEXT
General Text on Windows COLOR_WINDOWTEXT
Application Background COLOR_BACKGROUND
Desktop General COLOR_DESKTOP
Desktop Background COLOR_BACKGROUND
Inactive Title Bar Background COLOR_INACTIVECAPTION
Menu Bar  
Menu Background COLOR_MENU
Menu Highlight  
Selected Items Background COLOR_HIGHLIGHT
ToolTip Background COLOR_INFOBK
Window: Text on Caption COLOR_CAPTIONTEXT
Window Background COLOR_WINDOW
Right Side of a Gradient Active Window Title Bar COLOR_GRADIENTACTIVECAPTION
Right Side of a Gradient Inactive Window Title Bar COLOR_GRADIENTINACTIVECAPTION
Color of Hot-Track Item COLOR_HOTLIGHT

Color Palettes

Device independence is the ability for an application to draw its intended figures, text, shapes, and display colors regardless of the device on which the drawing is performed. One way to take care of this is to manage colors at the operating system level so that Microsoft Windows can select the right color to render an object or portion of it. In some cases, a device, such as a monitor or a printer, may need to take care of the coloring details of the job(s) it is asked to perform.

A color palette is a list of colors that a device can display. For example, one device may be able to handle only two colors. Such is the case for a black and white printer. Another device could be able to use more colors than that. To control this situation, Microsoft Windows keeps track of the color palette of each device installed on the computer.

There are two types of color palettes. The default color palette is a list of colors that the operating system would use on a device unless notified otherwise. There are typically 20 reserved colors as default. A logical palette is a palette that an application creates for a specific device context.


Previous Copyright © 2010 FunctionX, Inc. Next