11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8. DC, Pen, Brush and Palette<br />

colors). Other colors will change from time to time as we open and close graphic applications. We can use<br />

samples 8.8-1\GDI or 8.8-2\GDI to test this.<br />

Please note that if the sample is executed on non-palette device, the logical palette will not represent<br />

the system palette. This is because on the hardware level, palette does not exist at all.<br />

8.10 Palette Animation<br />

Flag PC_RESERVED<br />

Another interesting flag we can use when creating a logical palette is PC_RESERVED, which can be used<br />

to implement palette animation. If we have a logical palette with this flag set for some entries, the colors in<br />

these entries will only be mapped to the unused entries of the system palette. If the mapping is successful,<br />

when we change the colors in the logical palette, the entries in the system palette will also change. If any<br />

portion of window is painted with such entries, this change will affect that portion. This is the reason why a<br />

logical entry with PC_RESERVED flag can not be mapped to an occupied entry.<br />

The following table lists the difference between a normal logical palette entry and an entry with<br />

PC_RESERVED flag:<br />

Normal Entry<br />

Color First unused entry in the system palette is<br />

Mapping looked for, if there is no such an entry, the<br />

Algorithm color is mapped to the nearest color in the<br />

system palette.<br />

Palette If the logical palette is realized, there is no<br />

Realization guarantee that the color will be realized<br />

successfully. The color may be mapped to<br />

a nearest color.<br />

Other Entries can always be created<br />

successfully.<br />

Entry With PC_RESERVED Flag<br />

The entry can only be mapped to an<br />

unused entry of the system palette.<br />

The color contained in this type of<br />

entries will always be realized<br />

successfully.<br />

Entries may not be created successfully.<br />

While we can change the color of any area in a window by painting it again (using a different brush or<br />

pen), the above mentioned method has two advantages:<br />

1) If we have several areas painted with the same color, the new method will cause all of them to change<br />

at the same time once the old color is replaced with a new one in the logical palette. For the traditional<br />

method, we have to draw each area one by one to make this change, it takes longer time.<br />

1) If we draw each area one by one, the change takes place in software level. For the new method, the<br />

color is filled to the system palette directly (This change happens at the hardware level), which is<br />

extremely fast.<br />

Animation<br />

With this method, it is very easy to implement an animation effect. For example, considering an array<br />

of four rectangles that are filled with the following four different colors respectively: red, green, blue and<br />

black. If we paint the four rectangles with green, blue, black, red next time, and blue, black, red, green next<br />

next time, and so on…, this will give us an impression that the rectangles are doing rotating shift. One way<br />

to implement this effect is to redraw four rectangles again and again using different colors (which means<br />

using different entries to draw the same rectangle again and again). Another way is to switch the colors in<br />

the palette directly.<br />

Sample 8-10\GDI demonstrates how to implement palette animation. It is a standard SDI application<br />

generated from Application Wizard. In the sample, the client area is divided into 236 columns, each row is<br />

painted with a different color. The colors in the logical palette will be shifting all the time, and we will see<br />

that the colors in the client window will also shift accordingly.<br />

229

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!