Splines, Pens and Brushes

The program of this section demonstrates the drawing of cubic splines. The output of the program is shown below.

Splines are cubic curves. Four points that lie on an upright cubic are sufficient to determine the cubic. However, more often, splines are specified using the two end points and two control points that determine the tangents at the endpoints.


When line primitives are drawn, they are rendered to the device in the current pen. A pen may be selected through the function SelectObject. The function GetStandardObject may be used to create one of the __stdcall pens (black and white only). The function DeleteObject destroys pens of any type.

To create a pen other than a __stdcall pen, the function CreatePen may be used. The available styles of pens created in this manner are shown in the diagram below.

As well as specifying the style to this function, the pen width and color are also specified. The width parameter applies to PenStyle::Solid, PenStyle::Blank and PenStyle::InsideFrame. A width of 0 indicates that the pen should be 1 pixel wide. Standard pens are 1 pixel wide. If a width greater than 1 is specified for any of the dotted and dashed styles, a solid pen is used instead. When a pen with style other than PenStyle::InsideFrame is selected into a device context, the color of the pen is converted to the nearest available pure color. Pens of style PenStyle::InsideFrame may use dithered colors when a width greater than 1 is specified.

Pens may also be created via the function CreatePen - Indirect. In this case, a pen is created using an instance of the class LogicalPen. Whenever a pen is created through either of the two aforementioned functions, they are separate objects in their own right and are not directly related to a device context (until selected).

Background Mode and Background Color

When drawing with pens that contain dots and dashes, the coloring of the gaps depends upon the background mode and the background color. When BackgroundMode::Opaque is in effect, the operating system fills the spacings between dots and dashes with the current background color. The background mode may be set using the function SetBackgroundMode; whereas, the background color may be set using SetBackgroundColor. When the background is BackgroundMode::Transparent, the spacings are left unaltered.

Drawing Modes

When a pen is used, the operating system performs bitwise boolean operation between the pixels of the pen and the destination display surface. Such operations are commonly referred to as "binary raster operations". The enumeration Mix gives symbolic names to each of the raster operations. The default mix mode is Mix::CopyPen, which implies that the pen color is copied without reference to the color of the destination pixel.

To simplify the discussion, let it be assumed that the display is monochrome. Then the following table documents the effect of the binary raster operations

Pen (P) 1 1 0 0 Operation Drawing Mode
Destination (D) 1 0 1 0
Results0 0 0 0 0 mix::black
0 0 0 1 ~(P | D) mix::not_merge_pen
0 0 1 0 ~P & D mix::mask_not_pen
0 0 1 1 ~P mix::not_copy_pen
0 1 0 0 P & ~D mix::mask_pen_not
0 1 0 1 ~D mix::not
0 1 1 0 P ^ D mix::exclusive_or_pen
0 1 1 1 ~(P & D) mix::not_mask_pen
1 0 0 0 P & D mix::mask_pen
1 0 0 1 ~(P ^ D) mix::not_exclusive_or_pen
1 0 1 0 D mix::no_operation
1 0 1 1 ~P | D mix::merge_not_pen
1 1 0 0 P mix::copy_pen
1 1 0 1 P | ~D mix::merge_pen_not
1 1 1 0 P | D mix::merge_pen
1 1 1 1 1 mix::white

The value Mix::NoOperation leaves the destination unchanged.

Filled Areas

There are six functions that draw filled areas - as shown in the table below.

DrawRectangle A __stdcall rectangle (square corners).
DrawRoundedRectangle A rectangle with elliptically rounded corners.
DrawEllipse An ellipse.
DrawSector A pie wedge of an ellipse with endpoints connected by a chord.
DrawPolygon A polygon.
DrawPolygons Multiple polygons.

The outline of the figure is drawn with the currently selected pen. The current background mode, background color and drawing mode are used when drawing the outline of the figure. Figures are filled using the currently selected brush. The default brush is white and there is a number of other __stdcall brushes available. The function GetStandardObject may be used to create one of the __stdcall brushes. The function SelectObject may be used to select a brush. If the outline of a figure is to be drawn without drawing the interior, StandardBrush::Null may be selected. If the interior is to be filled without drawing the boundary, StandardPen::Null may be selected.

When drawing with the function DrawPolygon, the fill mode is used to calculate the interior of the figure. The fill mode may be set using the function SetFillMode.

Brushing the Interior

The interiors of the aforementioned primitives are filled with the current brush. A brush is an 8x8 pixel bitmap that is pattern repeated throughout the figure. There are several functions used to create different types of brushes, as shown in the table below.

GetStandardObject Loads one of the __stdcall brushes.
CreateSolidBrush Creates a brush of a given color.
CreateBrush - Indirect Creates a logical brush.
CreatePatternBrush Creates a brush using a given bitmap.
CreateHatchBrush Creates a logical brush with the given color and hatch style.
CreateDeviceIndependentBitmapPatternBrush Creates a brush from a device independent bitmap held in global memory.
CreateDeviceIndependentBitmapPatternBrushPointer Creates a brush from a device independent bitmap stored in memory.

A brush is selected using the function SelectObject. A brush may be relinquished using the function DeleteObject. A brush should not be deleted whilst it is the currently selected brush.