Finally, the device independent bitmap (DIB) and color palettes are discussed. In the graphics example Of the previous chapter, the area enclosed in a rectangle rounding the shape being formed is redrawn whenever the mouse moves. This causes the entire rectangular area to flicker while drawing. Drawing techniques can be used that only need to change the area within the shape being drawn. When the shape is an ellipse, the shape itself need only be erased and redrawn, rather than the entire rectangular area surrounding the ellipse.
But a way to erase its previous shape from the screen needs to be provided prior to redrawing it in its new shape, In this chapter such techniques, which promote fast drawing and minimum screen flicker, are explained. Using Exclusive-or and Exclusive-nor for Fast Redraws The first fast drawing technique presented is the use of either of the fast drawing modes: exclusive-or or exclusive-nor. This technique is demonstrated by modifying the graphics program presented in the previous chapter in Listing 3-3.
The running program is shown in Figure 4-1. The program called “Faster’ looks a lot like the graphics program with the exception that the menu item “ROPE” has been replaced with a new menu item, “Postgraduate,” which creates a popup menu with two choices: “Corrode” or “Moderated. These menu choices, of course, are used to set the parameters of the fast drawing mode; “Corrode” gives the choice Of drawing With the exclusive-or mode, and “Moderated” gives the choice of drawing with the exclusive-nor mode.
Figure 4-1: Executing straw Program This Faster program refreshes the screen faster by using a new drawing technique in the Anonymousness() message handling function rather than using the call to Invalidated(), as was done in the graphics program of Chapter Three. When the mouse moves with the left button down, Inpatient is no longer called. Instead, when the left button is down and the mouse has moved, the old form of the current shape is exclusive-or-De (or exclusive-nor-De) often screen and the new form is exclusive-or-De (or exclusive-nor-De) onto the screen.
This fast redraw technique depends on the property of exclusive-or (or exclusive- nor) or that a second exclusive-or (or exclusive-nor) removes the shape from the screen, restoring the screen to its state bettor the first exclusive-or (or exclusive- nor), Drawing a second time restores the colors on the screen to what they were before the first drawing, which in essence erases the shape. Listing 4-1 shows the changes that must be made to the previous graphics program, given in Listing 3-3, to create this new program, “Faster. (Of course, the code for the application class is the same, so it is not shown. ) Some of the changes in the new program are due to the changes in the menu items. The resource files, faster_RCA and resource_h, are changed but are not shown because their changes are minor. All of the mainframe. H file has been shown for clarity, With the new code in bold and deleted lines Of code commented out. Since most of the code in mainframe. Cap is the same as the program of Listing 3-3, here practical only the new or changed functions are shown.
The new code is shown in bold. As well as preceded with an arrow to distinguish it from the code that was previously there. The code that was previously there is not bold. If a line of code that was previously there is no longer needed, it is commented out (preceded with a double slash and because it is a change from the previous code, it is also preceded with an arrow to highlight that a change has occurred. The following changes can be seen in the file mainframe. Cap: A new virtual function Drawn(Client*) of class C_Shape is defined.
This new function draws the shape on a small segment of the screen, so it uses a Client device context. Addition/deletion of variables of the class C_MainFrame, as highlighted in the code, Two new message map entries and their corresponding handler functions. (These result from the changed menu items and displace the three previous entries on the old menu. ) Slight modifications to the Inpatient() function. (We no longer set the ROPE mode for the entire screen area. Substantial modifications to the functions Anonymousness() and Malnutrition(), which are discussed in detail following the listing. The Inpatient Function The easiest way for an application to handle the drawing Of graphics and text in its client area is to call CNN::alienated() (or when the client area is to be repainted) and put all the drawing code in the member function Inpatient(). The reason this way is the easiest is that Inpatient() needs to contain the screen. Drawing code any. Ay, in order to respond to resizing and uncovering of the window. The graphics program uses this approach.
As discussed earlier, the Inpatient function will redraw the entire client area r will redraw that portion of the client area of its window which is specified in the first argument of The graphics program to Listing 3-3 will redraw all of the shapes in the shape list that are within the area being redrawn (the entire screen or the specified Correct). In this graphics program, the function is used when redrawing the entire screen is desired, which is indicated when the ROPE code or the m_Become code is changed. Use the function when you want to redraw only a portion of the screen, which is done when the new shape is being drawn and formed.
The Inpatient function loops over the elements of the array of pointers to shape objects (ellipses and rectangles) that the user has created, calling the Draw() virtual member function on each shape, The virtual function mechanism built into C++ ensures that the corresponding Draw() function will be called in each case. The Background Color The function CDC::Stockroom(COLORED) sets the background color. This color is used for filling the update area between the hatch lines when using a hatched brush, for filling the background of character cells, and for filling the gaps in dashed lines.
The system also uses this background color when converting bitmaps between color and monochrome device contexts. This fill is only done when the background mode is OPAQUE, as set by This background is more properly defined as the color of the interstices in brush patterns and nonsolid pen lines. Windows SO also uses the term “background” to refer to an unrelated concept, namely the background brush tort the window. This brush, named hypochondriac, is used to erase the clipping region before drawing in the client area. The variable hypochondriac is essentially the background color and pattern f the client area.
The default value that MFC assigns to hypochondriac is a solid white brush. The value of hypochondriac can be changed for a window, if desired, by assigning a new value to it in the Abstractedness’s() function (as discussed in Chapter One). These two types of background are not tied together in any way. For example, if the user changes the window background, the client area’s background will be the new color, but the interstices in hatched fills of rectangles, etc. Will still be whatever color has most recently specified for the device context.