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

variable of class CWinApp. In <strong>MFC</strong>, every application has a CWinApp derived class, which contains a pointer<br />

m_pMainWnd pointing to the mainframe window. For any application, the pointer to the CWinApp object can<br />

be obtained anywhere in the program by calling function AfxGetApp(). Using this method, we can easily<br />

find the mainframe window of any <strong>MFC</strong> application.<br />

Because the application supports status bar and tool bar, part of its client area may be covered by the<br />

control bar. So we need to deduct the covered area when calculating the dimension of the client area. For<br />

this purpose, in CMainFrame, function CWnd::GetClientRect(…) is overridden. Within the overridden<br />

function, the client area is adjusted if either the status bar or the tool bar is present:<br />

void CMainFrame::GetClientRect(LPRECT lpRect)<br />

{<br />

CRect rect;<br />

}<br />

CFrameWnd::GetClientRect(lpRect);<br />

if(m_wndToolBar.IsWindowVisible())<br />

{<br />

m_wndToolBar.GetClientRect(rect);<br />

lpRect->bottom-=rect.Height();<br />

}<br />

if(m_wndStatusBar.IsWindowVisible())<br />

{<br />

m_wndStatusBar.GetClientRect(rect);<br />

lpRect->bottom-=rect.Height();<br />

}<br />

Now back to MCDialogBar::CalcDynamicLayout(…) implementation. After obtaining the size of<br />

mainframe window’s client area, we examine LM_VERTDOCK and LM_HORZDOCK bits of dwMode parameter to<br />

see if the docking size is being inquired. If so, we further examine LM_HORZ bit to see if we should return<br />

horizontally docked size or vertically docked size. We return different sizes for different cases. For all other<br />

conditions, we just return the default implementation of the base class.<br />

Using the New Class<br />

To use this class, first we need to declare a MCDialogBar type variable in class CMainFrame. We also<br />

need to make sure that the header file of this class is included. In the sample application, this new variable<br />

is m_wndDialogBar. Then, as we have experienced many times, we need to create the window of the dialog<br />

bar in function CMainFrame::OnCreate(…). When doing this, we need to specify CBRS_SIZE_DYNAMIC flag<br />

in order to let the dialog bar be resized dynamically. Then we can call CDialogBar::EnableDocking(…),<br />

CDialogBar::SetBarStyle(…) and CMainFrame::DockControlBar(…) to set styles and implement<br />

docking.<br />

Now we can compile and execute the new project. Originally, the dialog bar is docked at the bottom<br />

border of the frame window. We may drag and dock it to any other border, or make it floating. As we do<br />

this, the dimension of the dialog bar will be adjusted automatically to suit different docking styles. Also,<br />

the edit control contained in the dialog bar will be resized dynamically according to the change on the<br />

dialog bar.<br />

1.10. Adding Flyby and Tool Tip<br />

Flyby and tool tip are two very nice features that can be added to both tool bar and dialog bar. If we<br />

enable these features, when the user moves mouse cursor over a control contained in tool bar or dialog bar<br />

and stay there for a while, a describing text about this control will appear on the status bar (It is called<br />

Flyby). At the same time, a small window with a short description will pop up (It is called Tool Tip. See<br />

Figure 1-12 for two types of controls).<br />

28

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

Saved successfully!

Ooh no, something went wrong!