Understanding Windows Forms Geometry

Understanding Windows Forms Geometry

This section examines the basic structures used to define position and size in a Windows Forms application. Although we’ve made it through a couple of chapters on Windows Forms without describing these structures in more detail, it’s impossible to draw a line or fill a rectangle unless you specify the precise location of the object to be filled or drawn.

Specifying Locations with the Point Structure

The Point structure represents a location on a two-dimensional surface in a Windows Forms application. This structure is frequently used to define locations used for forms or controls; it’s also used to define boundaries when drawing lines, rectangles, and other shapes.

The Point structure is built around the following two properties:

  • X  Defines the x-coordinate, or horizontal position, of the Point structure

  • Y  Defines the y-coordinate, or vertical position, of the Point structure

The default constructor for a Point structure creates an empty Point structure with both the x-coordinate and the y-coordinate set to 0, as shown here:

Point pt = new Point(); 

An empty Point structure can be detected through the IsEmpty property, which returns true for empty Point structures.

if(pt.IsEmpty)
{
    // pt is an empty Point structure.
}

The most common way to create a Point structure is to pass the xy-coordinates to the constructor, as shown here:

int x = 10;
int y = 20;
Point pt = new Point(x, y); 

In this constructor, the first parameter is the x-coordinate and the second parameter is the y-coordinate. Other, less commonly used overloads of the constructor enable you to create Point values from a Size structure (the Size structure is discussed in the next section) or an integer that specifies both X and Y values.

The PointF structure is similar to Point, except that its coordinates are composed of floating-point values that provide greater precision. The PointF structure also can be created by passing a pair of float coordinates to the constructor, as shown here:

float x = 22.4f;
float y = 17.8f;
Point pt = new PointF(x, y); 

The Point structure includes three static methods that enable conversion from PointF to Point values, as follows:

  • Ceiling  Converts from PointF to Point by rounding up each coordinate value to the next higher integer value

  • Round  Converts from PointF to Point by rounding each coordinate value to the nearest integer value

  • Truncate  Converts from PointF to Point by rounding each coordinate value to the next lower integer value

An implicit conversion operator converts Point values to PointF values without the requirement for explicit casts or method calls.

Defining the Size of Visual Elements

The Size structure is used to define the size of windows, forms, controls, and other rectangular regions in a Windows Forms application. The Size structure has two nondefault constructors. The first constructor accepts a Point value, as shown here:

Point pt;

    

Size mySize = new Size(pt);

This constructor uses the x-coordinate from the Point parameter to initialize the Size structure’s length. The y-coordinate from the Point parameter is used to initialize the Size structure’s height.

The second Size constructor accepts discrete integer values for the width and the height of the new Size structure, as follows:

int width = 20;
int height = 10;
Point pt = new Point(width, height); 

The Size structure exposes its width and height through two properties: Width and Height.

As with the Point structure, IsEmpty can be used to test for empty Size values, as shown here:

if(mySize.IsEmpty)
{
    // mySize is an empty Size structure.
}

The SizeF structure is similar to Size, except that it uses floating-point values instead of integers to represent size dimensions. Just as with the Point and PointF structures described in the previous section, the Size structure includes Ceiling, Round, and Truncate methods for conversion from SizeF to Size values. Additionally, there’s an implicit conversion operator for conversions from Size to SizeF.

Defining Rectangles

The Rectangle structure defines the location and size of a rectangle and is the traditional data structure used to describe controls and forms (although strictly speaking, forms and controls aren’t limited to rectangular shapes). The Rectangle structure is organized much like the Point and Size structures, providing methods and properties to get and set the underlying coordinates on which the structure is based. The Rectangle structure differs somewhat from RECT, the equivalent Microsoft Win32 data structure. The RECT structure is based on four coordinates that represent the edges of the rectangle; the Rectangle structure consists of a Point structure that defines the upper-left corner of the rectangle and a Size structure that specifies the dimensions of the rectangle.

Two constructors are used to create Rectangle structures. The first constructor accepts a Point structure and a Size structure as parameters, as shown here:

Size  mySize = new Size(200, 300);
Point startPoint = new Point(10, 10);
Rectangle myRect = new Rectangle(startPoint, mySize); 

The second version of the Rectangle constructor accepts four integers that describe its boundaries, as shown here:

int left = 10;
int top = 10;
int width = 200;
int height = 300;
Rectangle myRect = new Rectangle(left, top, width, height); 

The four parameters in this example aren’t the edges of the rectangle; the first two parameters define the xy-coordinates of the rectangle’s upper-left corner, and the last two parameters define the width and height of the rectangle.

The Rectangle structure is commonly used when you’re drawing or manipulating a control’s client area—that is, the “main” portion of the control, minus extra nonclient elements such as scroll bars. The client area of a form is the portion of the control that’s located below the title bar and menu bar (if any) and within the boundaries of the form. The Form class exposes a Client­Rectangle property that can be used to retrieve the location of the client area, as shown here:

Rectangle myRect = ClientRectangle;

As you saw in Chapter 13, the status bar overlays the client area without reducing the overall size of the client area, unlike the menu and scroll bars. When calculating the visible area of a form, remember to reduce the client area’s size by the dimensions of the status bar.

The Rectangle structure exposes several properties that are used to get and set rectangle coordinates, listed here:

  • Left

  • Right

  • Top

  • Bottom

  • Width

  • Height

The RectangleF structure is similar to the Rectangle structure, except that it works with floating-point values instead of integers. In addition, RectangleF uses SizeF and PointF structures as parameters instead of Size and Point. And just like PointF and SizeF, the RectangleF structure will implicitly convert Rectangle values to RectangleF values. The Rectangle structure also exposes Ceiling, Round, and Truncate methods that convert RectangleF values to Rectangle values.



Part III: Programming Windows Forms