11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4. Button<br />

Chapter 4<br />

Buttons<br />

Button is one of the most commonly used controls, almost every application needs to include one or<br />

more buttons. It seems that it is very easy to implement a standard button in a dialog box: all we<br />

need to do is adding a button resource to the dialog template, then using Class Wizard to generate<br />

message handlers. However, it is not very easy to further customize button’s default properties.<br />

In this chapter, we will discuss how to implement different type of customized buttons. At the end of<br />

this chapter, we will be able to include some eye-catching buttons in our applications.<br />

4.1 Bitmap Button: Automatic Method<br />

Generally, buttons display plain text on its interface. Sometimes it is more desirable to let them have<br />

graphic user interface. A typical application that uses this type of buttons would be a CD player, everyone<br />

would like the play button to have a graphic interface instead of just displaying text such as “play”, “stop”<br />

(so that it looks like a real “play” button).<br />

Button States<br />

Before customizing button’s default feature, it is important for us to understand some basics of buttons.<br />

Every button has four states. When a button is not clicked, is in “up” state (the most common state). When<br />

it is pressed down, it is in “down” state. To emphasis a button’s 3D effect, a default button will recess when<br />

it is pressed by the mouse. Also, a button could be disabled, in this state, the button will not respond to any<br />

mouse clicking (As the default implementation, when a button is disabled, it will be drawn with “grayed”<br />

effect). Finally, a button has a “focused” or “unfocused” state. In the “focused” state, the button is an active<br />

window, and is accessible through using keyboard (ENTER or downward ARROW key). For the default<br />

implementation, a rectangle with dashed border will be drawn over a button’s face when it has the current<br />

focus.<br />

Owner-Draw Bitmap Button<br />

We can use owner draw bitmap button to add graphics to the button. To distinguish among different<br />

states, we need to associate different states with different images if necessary.<br />

The simplest way to create this type of button is to implement bitmap button using class<br />

CBitmapButton. To create a bitmap button, first we must set its “Owner Draw” style. This can be<br />

implemented by checking “Owner Draw” check box in the “Push Button Properties” property sheet when<br />

creating the button resource (We need to add button resource in order to create button, this is the same with<br />

a normal button. See Figure 4-1). For an owner-draw button, message WM_DRAWITEM will be sent to the<br />

button when it needs to be painted (remember, a button is also a window that can receive message). Upon<br />

receiving this message, the button will be drawn by the overridden function. For non-owner-draw button,<br />

its interface is implemented by the default method, and will display a plain text on the button’s face.<br />

Class CBitmapButton handles message trapping and processing; also, it contains member functions<br />

that can be used to paint the button. If we use this class to implement bitmaps buttons, all we need to do is<br />

preparing some bitmap resources, declaring variables using class CBitmapButton, and associating bitmap<br />

resources with the corresponding buttons.<br />

69

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

Saved successfully!

Ooh no, something went wrong!