The appearance of lines drawn on the display is also affected by the drawing mode defined in the device context. Imagine drawing a line that has a color based not only on the color of the pen but also on the original color of the display area where the line is drawn. Imagine a way in which you could use the same pen to draw a black line on a white surface and a white line on a black surface without knowing what color the surface is. Could such a facility be useful to you? It's made possible by the drawing mode.
When Windows uses a pen to draw a line, it actually performs a bitwise Boolean operation between the pixels of the pen and the pixels of the destination display surface. Performing a bitwise Boolean operation with pixels is called a ”raster operation,“ or ”ROP.“ Because drawing a line involves only two pixel patterns (the pen and the destination), the Boolean operation is called a ”binary raster operation,“ or ”ROP2.“ Windows defines 16 ROP2 codes that indicate how Windows combines the pen pixels and the destination pixels. In the default device context, the drawing mode is defined as R2_COPYPEN, which means that Windows simply copies the pixels of the pen to the destination, which is how we normally think about pens. There are 15 other ROP2 codes.
Where do these 16 different ROP2 codes come from? For illustration purposes, let's assume a monochrome system. The destination color (the color of the window's client area) can be either black (which we'll represent by a 0) or white (1). The pen also can be either black or white. There are four combinations of using a black or white pen to draw on a black or white destination: a white pen on a white destination, a white pen on a black destination, a black pen on a white destination, and a black pen on a black destination.
What happens to the destination after you draw with the pen? One possibility is that the line is always drawn as black regardless of the pen or destination color: This drawing mode is indicated by the ROP2 code R2_BLACK. Another possibility is that the line is drawn as black except when both the pen and destination are black, in which case the line is drawn as white. Although this might be a little strange, Windows has a name for it: The drawing mode is called R2_NOTMERGEPEN. Windows performs a bitwise OR operation on the destination pixels and the pen pixels and then inverts that result.
The table below shows all 16 ROP2 drawing modes. The table indicates how the original pen (P) and destination (D) colors are combined for the resultant destination color.
The column labeled ”Boolean Operation“ uses C notation to show how the destination pixels and pen pixels are combined.
Pen (P): Destination (D): |
1 1 |
1 0 |
0 1 |
0 0 |
Boolean Operation |
Drawing Mode |
Results: | 0 | 0 | 0 | 0 | 0 | R2_BLACK |
0 | 0 | 0 | 1 | ~(P | D) | R2_NOTMERGEPEN | |
0 | 0 | 1 | 0 | ~P & D | R2_MASKNOTPEN | |
0 | 0 | 1 | 1 | ~P | R2_NOTCOPYPEN | |
0 | 1 | 0 | 0 | P & ~D | R2_MASKPENNOT | |
0 | 1 | 0 | 1 | ~D | R2_NOT | |
0 | 1 | 1 | 0 | P ^ D | R2_XORPEN | |
0 | 1 | 1 | 1 | ~(P & D) | R2_NOTMASKPEN | |
1 | 0 | 0 | 0 | P & D | R2_MASKPEN | |
1 | 0 | 0 | 1 | ~(P ^ D) | R2_NOTXORPEN | |
1 | 0 | 1 | 0 | D | R2_NOP | |
1 | 0 | 1 | 1 | ~P | D | R2_MERGENOTPEN | |
1 | 1 | 0 | 0 | P | R2_COPYPEN (default) | |
1 | 1 | 0 | 1 | P | ~D | R2_MERGEPENNOT | |
1 | 1 | 1 | 0 | P | D | R2_MERGEPEN | |
1 | 1 | 1 | 1 | 1 | R2_WHITE |
You can set a new drawing mode in the device context by:
SetROP2 (hdc, nDrawMode) ;
The nDrawMode parameter is one of the values listed in the ”Drawing Mode“ column of the table. You can obtain the current drawing mode using the function:
nDrawMode = GetROP2 (hdc) ;
The device context default is R2_COPYPEN, which simply transfers the pen color to the destination. The R2_NOTCOPYPEN mode draws white if the pen color is black and black if the pen color is white. The R2_BLACK mode always draws black, regardless of the color of the pen or the background. Likewise, the R2_WHITE mode always draws white. The R2_NOP mode is a ”no operation“: It leaves the destination unchanged.
We started out using an example of a pure monochrome system. In reality, on a monochrome display Windows can simulate various shades of gray by dithering black and white pixels. When drawing a pen on a dithered background, Windows simply performs the bitwise operation on a pixel-by-pixel basis. The R2_NOT mode always inverts the destination, again regardless of the color of the pen. This mode is useful when you don't know the color of the background, because it guarantees that the pen will be visible. (Well, almost guarantees—if the background is a 50 percent gray, then the pen will be virtually invisible.)