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 4. Button<br />

4.5 Irregular Shape Bitmap Button<br />

Transparent Background<br />

Up to now all the buttons created by us have a rectangular shape. It is relatively difficult to create a<br />

button with irregular shapes (e.g., a round button). Even for bitmap buttons, their associated bitmaps are<br />

always rectangular.<br />

We may think that by setting the bitmap’s background color to the color of the dialog box, the bitmap<br />

button may look like a non-rectangular button. For example, in the previous samples, button IDC_PLAY is<br />

made up of two regions: its foreground is the triangular region, and rest area can be treated as its<br />

background (Figure 4-7). We can change the background color to the color of the dialog box so that this<br />

area appears transparent to us when the bitmap button is implemented.<br />

However, this is not the best solution. In Windows operating system, the color of dialog box can be<br />

customized. The user can double click “Display” icon contained in the “Control Panel” window and set the<br />

colors for many objects, which include title bar, backdrop..., and so on. So actually we have no way of<br />

knowing the color of dialog box beforehand. If we implement a bitmap button and set its background color<br />

to the dialog box color in our system, it may not work properly in another system.<br />

Background<br />

Foreground<br />

Figure 4-7. Foreground and background of a bitmap button<br />

To draw a bitmap with transparent background, we need to use “mask” when painting the bitmap. We<br />

can imagine a mask as a matrix with the same dimension of the bitmap image, and each element in the<br />

matrix corresponds to a pixel contained in the bitmap. The elements in the matrix may be either “0” or “1”.<br />

When we paint the bitmap, only those pixels with “0” masks are output to the device (we can also use “1”<br />

to indicate the pixels that need to be drawn).<br />

When programming the application, we can prepare two bitmaps of exactly the same size: one stores<br />

the normal image, the other one stores mask. The mask bitmap is made up of only two types of pixels:<br />

black and white. This is because for black color, its RGB elements are all 0s; for white color, the elements<br />

are all 1s. By implementing the mask bitmap like this, the background area of the mask bitmap is white and<br />

the foreground area is black.<br />

Windows allows us to output the pixels of a bitmap to the target device using different operation<br />

mode. We can copy a pixel directly to the target device, we can also combine a pixel contained in the<br />

bitmap with the pixel contained in the target device using various mode: bit-wise OR, AND, and XOR. For<br />

example, if we combine red color (RGB(255, 0, 0)) with green color (RGB(0, 255, 0)) using bit-wise AND<br />

operation, the output will be yellow color (RGB(255, 255, 0)).<br />

When painting the bitmap, we first need to output the normal bitmap to the target device using bit-wise<br />

XOR drawing mode, then output the mask bitmap to the target device at the same position using bit-wise<br />

AND mode. Finally we can output the normal bitmap again using bit-wise XOR mode. By doing this, the<br />

output bitmap’s background will become transparent.<br />

The reason for this is simple. Before bitmap is painted, the target device may contain a uniform color<br />

or any image pattern. After normal bitmap is first painted, the foreground area of the target device will<br />

become the combination of source image pattern and target image pattern. After we output mask bitmap<br />

78

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

Saved successfully!

Ooh no, something went wrong!