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

m_wndToolBar.SetBarStyle<br />

(<br />

m_wndToolBar.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC<br />

);<br />

Function CToolBar::SetBarStyle(…) sets tool bar’s styles, which can be a combination of different<br />

style flags using bit-wise OR operation. Because we do not want to lose the default styles, first function<br />

CToolBar::GetBarStyle() is called to retrieve the default tool bar styles, then new styles are combined<br />

with the old ones using bit-wise OR operation. In the above code fragment, three new styles are added to<br />

the tool bar: first, flag CBRS_TOOLTIPS will enable tool tips to be displayed when the mouse cursor passes<br />

over a tool bar button and stay there for a few seconds; second, flag CBRS_FLYBY will cause the status bar to<br />

display a flyby about this button (For details of tool tip and flyby, see section 1.11); third, flag<br />

CBRS_SIZE_DYNAMIC will allow the user to dynamically resize the tool bar, if we do not specify this style,<br />

the dimension of the tool bar will be fixed.<br />

The following statement enables a dockable tool bar:<br />

m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);<br />

Function CToolBar::EnableDocking(…) makes the tool bar dockable. Here, flag CBRS_ALIGN_ANY<br />

indicates that the tool bar may be docked to any of the four boarders of the frame window. We may change<br />

it to CBRS_ALIGN_TOP, CBRS_ALIGN_BOTTOM, CBRS_ALIGN_LEFT, or different combinations of these flags,<br />

whose meanings are self-explanatory.<br />

The dockable tool bar still can’t be docked if the frame window does not support this feature. We must<br />

call function CFrameWnd::EnableDocking(…) to support docking in the frame window and call<br />

CFrameWnd::DockControlBar(…) for each control bar to really dock it. The following code fragment shows<br />

how the two functions are called for the default tool bar:<br />

EnableDocking(CBRS_ALIGN_ANY);<br />

DockControlBar(&m_wndToolBar);<br />

Like function CCtrlBar::EnableDocking(…), CFrameWnd::EnableDocking(…) uses the same<br />

parameters to specify where a control bar is allowed to be docked.<br />

Creating New Tool Bar<br />

We need to do the same thing for the newly declared variable m_wndColorButton. We can call the<br />

above-mentioned functions to create tool bar window, set its styles, enable docking, and dock it. The<br />

following code fragment shows the updated function CMainFrame::OnCreate(…):<br />

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)<br />

{<br />

if (CFrameWnd::OnCreate(lpCreateStruct) == -1)<br />

return -1;<br />

if (!m_wndToolBar.Create(this) ||<br />

!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))<br />

{<br />

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

return -1;<br />

}<br />

if (!m_wndColorButton.Create(this) ||<br />

!m_wndColorButton.LoadToolBar(IDR_COLOR_BUTTON))<br />

{<br />

}<br />

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

return -1;<br />

if (!m_wndStatusBar.Create(this) ||<br />

!m_wndStatusBar.SetIndicators(indicators,<br />

sizeof(indicators)/sizeof(UINT)))<br />

5

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

Saved successfully!

Ooh no, something went wrong!