VB6 at Work: The PieChart Project VB Help

The PieChart application uses the Circle method to draw pie charts with connected arcs, as shown in Figure 6.11. Because a connected arc is a closed shape, it can also be filled with a pattern or a solid color, as specified by the FillStyle and FillColor properties. The PieChart application lets you select whether the pie’s slices will be filled with a pattern or a solid color or remain hollow. The PieChart application also serves as an example of how to use the FillStyle and FillColor properties to fill closed shapes.

The program generates 10 random numbers in the range 20 to 100, stores them in the PieDataO array, and tiler, calculates the arc that corresponds to each number, Because the total must be a fun circle (2″pi)~each element of the PieChart array corresponds to an arc of 2~pi”PieData(i)/Total. Each slice’s starting angle is the ending angle of the previous slice, and its ending angle is the starting angle plus the angle corresponding to its element in the PieDataO are:ray.The following code implements the technique just described for drawing pie charts

Using the Drawing Modes

When you draw shapes with Visual Basic’s graphics methods, the pixels that make up the shape replace any existing pixels on the control by default. Visual Basic can, however, combine the new pixels with the existing ones in various ways, depending 01’\ the settings of the control’s DrawModc property. The settings of this property are shown in Table 6.4. Most of them correspond to logical operators such as AND, OR, and NOT. The default DrawMode setting is 13 (Copy Pen), which transfers the new pixels onto the control, replacing the undcnying pixels. Setting 4 (Not Copy Pen) works the same way, except that it reverses me color of the pixels beingdrawn. Setting 9 combines the new pixels with the existing ones with the logical operator AND. As you can see, there are many ways to combine the color of the new pixels with the existing ones.

The default setting of the DrawMode property is vbCopyPen, which replaces !he underlying pixels with the new ones. We’ll return to the topic of the drawing mode in the section “Using the PaintPicture Method,” in the following chapter, where you’ll findan application that allows you to experiment, with the various settings. First, however, let’s explore an especially interesting and useful drawing mode:XOR.

Using the XOR Operator

One of the most practical settings of the DrawMode property is 7 (vbXorPen). With this setting, the values of the new pixels are combined with the existing ones by means of the XOR operator. The bits are combined.

The result of the operation is 1 only if the bits are different. If they are the same (both 0 or both I), the result is O. Let’s see how the XOR operator :works. In the Immediate window (if it’s not visible, choose View >Immediate Window), enter the following statements (press Enter after each Print statement to see the result

As you can see, the number 184 is XORed with the value 45, and the result is a new number (1~9). When the result is again XORed with one of the two original numbers it yields one of the other numbers. This is a unique property of the XOR ‘operator, which makes it useful in many situations, including cryptography. Indeed, at the heart of every encryption algonthm you’ll find the XOR operator. Let’s run a small experiment with characters, As you know, characters are represented by numbers, which you can find with the AscO function. Here are a few experiments with characters and the XORoperator

When the character lid” is XORed with the number 88, it becomes the character “c”.If the new character is XORed with 88, it becomes the original character. Repeating this process for every character in a text me’ encrypts the text. To recover the original text, you must XOR each character of the encrypted text with the key. The number 88 in the previous example is the key (something like an’ encryption
password). Without the ~y,.you’ll find it difficult to recover the original text. More, secure encryption algorithms use long encryption keys or other variations on this technique, but they are all based on this unique property of the XOR operator. Now how does this relateto Visual Basic graphics? If the XOR operator can mask and then reveal a number, why not use it to do the same with a line? If you draw a line in XOR mode, you’ll see a line superimposed over the existing pixels. If you draw another line on top of the first one, the line will disappear, revealing the underlying pixels. The XOR drawing mode has an interestingproperty: The first time you draw a shape on the screen, it’s displayed on top of everything else
and the colors change, but not the shape. If you draw the same shape again, the net result is to remove the shape without affecting the background. The two PictureBox controls on the Form shown in Figures 6.12 and 6.13 have the same dimensions and display the same image. They differ only in the setting • of their DrawMode property, which is :J.3(Default) for the first one and 7 (vbXorPen) .4 for the second one. Figure 6.12 shows the result of drawing a solid rectangle on both of them. Figure 6.13 shows the result of drawing the same rectangle again. Nothing is changed in the,first PictureBox. but the rectangle is removed from the second control

Posted on November 5, 2015 in Drawing with Visual Basic

Share the Story

Back to Top