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 1. Tool Bar and Dialog Bar<br />

When we create tool bar resource, the control IDs are generated contiguously according to the<br />

sequence of creation. For example, if we first create the blue button, then the green button, the two IDs will<br />

have the following relationship:<br />

ID_BUTTON_GREEN = ID_BUTTON_BLUE+1<br />

Modifying an ID<br />

Sometimes we don’t know if the IDs of the tool bar buttons have contiguous values, because most of<br />

the time we use only symbolic IDs and seldom care about the actual values of them. If the IDs do not meet<br />

our requirement and we still want to use the above message mapping macros, we need to modify the ID<br />

values by ourselves.<br />

By default, all the resource IDs are defined in file “resource.h”. Although we could open it with a text<br />

editor and make changes, there is a better way to do so. First, an ID value could be viewed in the Developer<br />

Studio by executing View | Resource symbols… command. This command will bring up a dialog box that<br />

contains all the resource IDs used by the application (Figure 1-3).<br />

If we want to make change to any ID value, first we need to highlight that ID, then click the button<br />

labeled “Change…”. After that, a “Change Symbol” dialog box will pop up, if the ID is used for more than<br />

one purpose, we need to select the resource type in “Used by” window (This happens when this ID is used<br />

for both command ID and string ID, in which case the string ID may be used to implement flyby and tool<br />

tip. See Figure 1.9). In our sample, there is only one type of resource that uses the button IDs, so we do not<br />

need to make any choice. Now click “View Use” button (Figure 1-4), which will bring up “Toolbar Button<br />

Properties” property sheet. Within “General” page, we can change the ID’s value by typing in a new<br />

number in the window labeled “ID”. For example, if we want to change the value of ID_BUTTON_RED to<br />

32770, we just need to type in an equal sign and a number after the symbolic ID so that this edit window<br />

has the following contents (Figure 1-5):<br />

ID_BUTTON_RED=32770<br />

Resource ID<br />

ID value<br />

Figure 1-3. View resource IDs and their values<br />

With this method, we can easily change the values of four resource IDs (ID_BUTTON_RED,<br />

ID_BUTTON_GREEN, ID_BUTTON_BLUE, ID_BUTTON_YELLOW) and make them contiguous. After this we can<br />

map all of them to a single member function instead of implementing message handlers for each ID.<br />

Unfortunately, Class Wizard doesn’t do range mappings, so we have to implement it by ourselves.<br />

Sample 1.4\Bar demonstrates how to implement this kind of mapping. It is based upon sample 1.2\Bar,<br />

which already contains the default message mapping macros:<br />

BEGIN_MESSAGE_MAP(CBarDoc, CDocument)<br />

//{{AFX_MSG_MAP(CBarDoc)<br />

ON_COMMAND(ID_BUTTON_BLUE, OnButtonBlue)<br />

ON_COMMAND(ID_BUTTON_GREEN, OnButtonGreen)<br />

ON_COMMAND(ID_BUTTON_RED, OnButtonRed)<br />

ON_COMMAND(ID_BUTTON_YELLOW, OnButtonYellow)<br />

ON_UPDATE_COMMAND_UI(ID_BUTTON_BLUE, OnUpdateButtonBlue)<br />

ON_UPDATE_COMMAND_UI(ID_BUTTON_GREEN, OnUpdateButtonGreen)<br />

ON_UPDATE_COMMAND_UI(ID_BUTTON_RED, OnUpdateButtonRed)<br />

14

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

Saved successfully!

Ooh no, something went wrong!