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 />

Here, we use subclass instead of automatic method to load bitmaps. Also, we use<br />

IDB_BITMAP_BTNUNCHECK and IDB_BITMAP_BTNCHECK to implement button’s normal and highlighted states<br />

respectively.<br />

Because mouse related messages are handled within the member functions of class MCSenButton, once<br />

we declare variables within it, the buttons will automatically become mouse sensitive. There is no need for<br />

us to write extra code for handling mouse messages outside the class.<br />

Summary<br />

1) We can use class CBitmapButton to implement bitmap buttons. To use this class, we need to prepare 1<br />

to 4 bitmap resources indicating button’s different states, then use class CBitmapButton to declare<br />

variables, and call either CBitmapButton::AutoLoad(…) or CBitmapButton::LoadBitmaps(…) to<br />

associate the bitmap resources with the buttons.<br />

2) To use function CBitmapButton::AutoLoad(…), the bitmap resources must have string IDs, and must<br />

be created by suffixing ‘U’, ‘D’, ‘F’ or ‘X’ to the button’s caption text.<br />

3) Buttons, check boxes and radio buttons implemented by class CButton can display user-provided<br />

bitmaps by calling function CButton::LoadBitmap(…). With this method, the button could be<br />

associated with only one image at any time. Also, its focused state will be indicated by drawing a dashbordered<br />

rectangle over button’s face.<br />

4) We can call function CBitmapButton::LoadBitmaps(…) at any time to change the associated bitmaps.<br />

This provides us a way of implementing check box and radio button using push button.<br />

5) Irregular shape button can be implemented by drawing images with transparency. We can prepare a<br />

normal image and a black-and-white mask image. When drawing the button, only the unmasked region<br />

of the normal image should be output to the target device.<br />

6) A button can handle mouse-related messages. If we want to know the mouse position when a button is<br />

being pressed, we can trap WM_LBUTTONUP message.<br />

5) We can implement mouse sensitive button by handling WM_MOUSEMOVE message and setting window<br />

capture.<br />

91

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

Saved successfully!

Ooh no, something went wrong!