Advanced MFC Programming

Advanced MFC Programming Advanced MFC Programming

math.hcmuns.edu.vn
from math.hcmuns.edu.vn More from this publisher
11.04.2014 Views

Chapter 1. Tool Bar and Dialog Bar variable of class CWinApp. In MFC, every application has a CWinApp derived class, which contains a pointer m_pMainWnd pointing to the mainframe window. For any application, the pointer to the CWinApp object can be obtained anywhere in the program by calling function AfxGetApp(). Using this method, we can easily find the mainframe window of any MFC application. Because the application supports status bar and tool bar, part of its client area may be covered by the control bar. So we need to deduct the covered area when calculating the dimension of the client area. For this purpose, in CMainFrame, function CWnd::GetClientRect(…) is overridden. Within the overridden function, the client area is adjusted if either the status bar or the tool bar is present: void CMainFrame::GetClientRect(LPRECT lpRect) { CRect rect; } CFrameWnd::GetClientRect(lpRect); if(m_wndToolBar.IsWindowVisible()) { m_wndToolBar.GetClientRect(rect); lpRect->bottom-=rect.Height(); } if(m_wndStatusBar.IsWindowVisible()) { m_wndStatusBar.GetClientRect(rect); lpRect->bottom-=rect.Height(); } Now back to MCDialogBar::CalcDynamicLayout(…) implementation. After obtaining the size of mainframe window’s client area, we examine LM_VERTDOCK and LM_HORZDOCK bits of dwMode parameter to see if the docking size is being inquired. If so, we further examine LM_HORZ bit to see if we should return horizontally docked size or vertically docked size. We return different sizes for different cases. For all other conditions, we just return the default implementation of the base class. Using the New Class To use this class, first we need to declare a MCDialogBar type variable in class CMainFrame. We also need to make sure that the header file of this class is included. In the sample application, this new variable is m_wndDialogBar. Then, as we have experienced many times, we need to create the window of the dialog bar in function CMainFrame::OnCreate(…). When doing this, we need to specify CBRS_SIZE_DYNAMIC flag in order to let the dialog bar be resized dynamically. Then we can call CDialogBar::EnableDocking(…), CDialogBar::SetBarStyle(…) and CMainFrame::DockControlBar(…) to set styles and implement docking. Now we can compile and execute the new project. Originally, the dialog bar is docked at the bottom border of the frame window. We may drag and dock it to any other border, or make it floating. As we do this, the dimension of the dialog bar will be adjusted automatically to suit different docking styles. Also, the edit control contained in the dialog bar will be resized dynamically according to the change on the dialog bar. 1.10. Adding Flyby and Tool Tip Flyby and tool tip are two very nice features that can be added to both tool bar and dialog bar. If we enable these features, when the user moves mouse cursor over a control contained in tool bar or dialog bar and stay there for a while, a describing text about this control will appear on the status bar (It is called Flyby). At the same time, a small window with a short description will pop up (It is called Tool Tip. See Figure 1-12 for two types of controls). 28

Chapter 1. Tool Bar and Dialog Bar Tool tip Flyby Figure 1-12. Tool tip and flyby Both features can be enabled by calling function CControlBar::SetBarStyle(…) using the corresponding flags. To enable tool tip, we need to set CBRS_TOOLTIP flag bit, to enable flyby, we need to set CBRS_FLYBY flag bit. Actually, in the previous sections, whenever we create a tool bar or dialog bar, the two flags are always set. Just setting the above flags can not activate the tool tip and flyby. We need to provide the text that will be used by tool tip and flyby. The text string must be implemented as application resources, and the ID of the string must be the same with the command ID of the control. For example, if we want to add flyby and tool tip for button ID_BUTTON_RED, we must create a string resource using ID_BUTTON_RED as its ID. This string will be used for both flyby and tool tip implementation. Within the string, the text is separated into two parts by an ‘\n’ character, with the sub-string before ‘\n’ used for flyby, and the sub-string after ‘\n’ used for tool tip. For example, if we want the flyby and tool tips for the red button to be “This is the red button” and “Red Button” respectively, the resource string should be “This is the red button\nRed Button”. If we do not provide a string resource for this command ID, the flyby and the tool tip will not be displayed even we enable CBRS_TOOLTIP and CBRS_FLYBY flags. If string resource does not have a second sub-string (In this case, there is no ‘\n’ character contained in the string), the whole string will be used for flyby, and no tool tip will be implemented. Adding this string for tool bar buttons is very easy. By opening the property sheet “Toolbar Button Properties”, we will find an edit box labeled “Prompt”. Inputting a string into this edit box will add the string resource automatically (Figure 1-13). Input the string that will be used for flyby and tool tip here Figure 1-13. Add flyby and tool tip string for tool bar control For dialog bar, we don’t have the place to input this string in the property sheet. So we need to edit string resource directly. This can also be implemented very easily. In the Developer Studio, if we execute command Insert | Resource…(or press CTRL+R keys), an “Insert Resource” dialog box will pop up. To add a string resource, we need to highlight node “String Table” and press “New” button. After this, a new window with the string table will pop up. By scrolling to the bottom of the window and double clicking an empty entry, a “String Properties” property sheet will pop up, which can be used to add a new string resource (Figure 1-14). 29

Chapter 1. Tool Bar and Dialog Bar<br />

Tool tip<br />

Flyby<br />

Figure 1-12. Tool tip and flyby<br />

Both features can be enabled by calling function CControlBar::SetBarStyle(…) using the<br />

corresponding flags. To enable tool tip, we need to set CBRS_TOOLTIP flag bit, to enable flyby, we need to<br />

set CBRS_FLYBY flag bit. Actually, in the previous sections, whenever we create a tool bar or dialog bar, the<br />

two flags are always set.<br />

Just setting the above flags can not activate the tool tip and flyby. We need to provide the text that will<br />

be used by tool tip and flyby. The text string must be implemented as application resources, and the ID of<br />

the string must be the same with the command ID of the control. For example, if we want to add flyby and<br />

tool tip for button ID_BUTTON_RED, we must create a string resource using ID_BUTTON_RED as its ID. This<br />

string will be used for both flyby and tool tip implementation. Within the string, the text is separated into<br />

two parts by an ‘\n’ character, with the sub-string before ‘\n’ used for flyby, and the sub-string after ‘\n’<br />

used for tool tip. For example, if we want the flyby and tool tips for the red button to be “This is the red<br />

button” and “Red Button” respectively, the resource string should be “This is the red button\nRed Button”.<br />

If we do not provide a string resource for this command ID, the flyby and the tool tip will not be displayed<br />

even we enable CBRS_TOOLTIP and CBRS_FLYBY flags. If string resource does not have a second sub-string<br />

(In this case, there is no ‘\n’ character contained in the string), the whole string will be used for flyby, and<br />

no tool tip will be implemented.<br />

Adding this string for tool bar buttons is very easy. By opening the property sheet “Toolbar Button<br />

Properties”, we will find an edit box labeled “Prompt”. Inputting a string into this edit box will add the<br />

string resource automatically (Figure 1-13).<br />

Input the string<br />

that will be used<br />

for flyby and<br />

tool tip here<br />

Figure 1-13. Add flyby and tool tip string for tool bar control<br />

For dialog bar, we don’t have the place to input this string in the property sheet. So we need to edit<br />

string resource directly. This can also be implemented very easily. In the Developer Studio, if we execute<br />

command Insert | Resource…(or press CTRL+R keys), an “Insert Resource” dialog box will pop up. To<br />

add a string resource, we need to highlight node “String Table” and press “New” button. After this, a new<br />

window with the string table will pop up. By scrolling to the bottom of the window and double clicking an<br />

empty entry, a “String Properties” property sheet will pop up, which can be used to add a new string<br />

resource (Figure 1-14).<br />

29

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

Saved successfully!

Ooh no, something went wrong!