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

{<br />

}<br />

TRACE0("Failed to create status bar\n");<br />

return -1;<br />

}<br />

m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |<br />

CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);<br />

m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);<br />

m_wndColorButton.SetBarStyle(m_wndColorButton.GetBarStyle() |<br />

CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);<br />

m_wndColorButton.EnableDocking(CBRS_ALIGN_ANY);<br />

EnableDocking(CBRS_ALIGN_ANY);<br />

DockControlBar(&m_wndToolBar);<br />

DockControlBar(&m_wndColorButton);<br />

return 0;<br />

By compiling and executing the sample application at this point, we can see that the tool bar has been<br />

created. The tool bar can be docked to one of the four borders of the mainframe window or be floated. If we<br />

dock the tool bar to either left or right border, we will see that the tool bar will automatically have a vertical<br />

layout. This feature is supported by class CToolBar, we don’t need to add any line of code in order to have<br />

it.<br />

Command Message Mapping<br />

The new tool bar looks very disappointing. Although we made much effort to add it, none of its<br />

buttons can be used to execute command. This is because we still haven’t implemented any message<br />

handler for the new commands, therefore the buttons will be disabled all the time.<br />

In Windows applications, commands are executed through sending WM_COMMAND message. As the user<br />

clicks a menu command or a tool bar button, the system will send a WM_COMMAND message to the application.<br />

All the Windows messages have two parameters, WPARAM and LPARAM (They are nothing but two integers,<br />

as an application receives a message, it will also receive the message parameters). For WM_COMMAND<br />

message, its WPARAM parameter is used to store the control ID (Command ID, such as ID_BUTTON_RED in<br />

our samples), which can be examined by the application to make appropriate response.<br />

In a general Windows application, message is received and processed by a callback function. When<br />

an application is initialized, it stores the address of the callback function in the system. When a message is<br />

generated, the system uses this address to call the callback function and pass the message as well as the<br />

associated parameters to the application. Besides processing the message, the application can also choose to<br />

pass the message to other applications in the system.<br />

If an application has callback function, we can process message WM_COMMAND within it. A general<br />

callback function for this purpose looks like the following:<br />

……<br />

……<br />

LONG APIENTRY CallBackProc<br />

(<br />

HWND hwnd,<br />

UINT message,<br />

DWORD wParam,<br />

LONG lParam)<br />

{<br />

switch (message)<br />

{<br />

case WM_CREATE:<br />

{<br />

break;<br />

}<br />

case WM_COMMAND:<br />

{<br />

switch(wParam)<br />

{<br />

case ID_BUTTON_RED:<br />

{<br />

}<br />

6

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

Saved successfully!

Ooh no, something went wrong!