Share via


Visual Basic Concepts

Controlling Display Using DrawMode

The DrawMode property determines what happens when you draw one pattern on top of another. Although changing the DrawMode property usually has some effect (especially with color systems), it is often not necessary to use this property when you are drawing on a blank or pure white background, or on a background of undifferentiated color.

You can set DrawMode to a value from 1 to 16. Common settings appear in the following table.

Setting Description
4 Not Copy Pen. Draws the inverse of the line pattern, regardless of what is already there.
7 Xor Pen. Displays the difference between the line pattern and the existing display, as explained later in this section. Drawing an object twice with this mode restores the background precisely as it was.
11 No operation. In effect, this turns drawing off.
13 Copy Pen (default). Applies the line’s pattern, regardless of what is already there.

For More Information   See "DrawMode Property" in the Language Reference.

The Xor Pen

A DrawMode setting of 7 is useful for animation. Drawing a line twice restores the existing display precisely as it was before the line was drawn. This makes it possible to create one object that "moves over" a background without corrupting it, because you can restore the background as you go. Most modes are not guaranteed to preserve the old background.

For example, the following code moves a circle every time the mouse is clicked. No matter what pattern was underneath the circle, it gets restored.

Private Sub Form_Click ()
   ForeColor = 255 : DrawMode = 7
   Circle (CurrentX, CurrentY), 1000
   CurrentX = CurrentX + 220
   CurrentY = CurrentY + 220
   Circle (CurrentX, CurrentY), 1000
End Sub

The Xor Pen draw mode (and most of the other DrawMode settings) works by comparing each individual pixel in the draw pattern (called the "Pen") and the corresponding pixel in the existing area (called the "Destination"). On monochrome systems, the pixel is turned either on or off, and Visual Basic performs a simple logical comparison: It turns a pixel on if either the Pen or Destination pixel is on, but not if both are on.

In color systems, each pixel is assigned a color value. For DrawMode settings such as Xor Pen, Visual Basic compares each corresponding pair of pixels in the Pen and Destination and performs a binary (bitwise) comparison. The result determines the color value of the resulting pixel, as shown in Figure 12.21.

Figure 12.21   Using the Xor Pen to set the binary value of a pixel in a line