Advanced MFC Programming
Advanced MFC Programming Advanced MFC Programming
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
- Page 1 and 2: Advanced MFC Programming Supporting
- Page 3 and 4: Checking a Menu Item 2.2 Right Clic
- Page 5 and 6: Trapping Double Clicking Message Re
- Page 7 and 8: COMMON DIALOG BOXES 7.1 File Open a
- Page 9 and 10: 9.1 Outputting Text Using Different
- Page 11 and 12: Summary SAMPLE: SIMPLE PAINT 11.0 P
- Page 13 and 14: 13.2 Creating Applications without
- Page 15 and 16: Functions Implementing Comparisons
- Page 17 and 18: Chapter 1. Tool Bar and Dialog Bar
- Page 19 and 20: Chapter 1. Tool Bar and Dialog Bar
- Page 21 and 22: Chapter 1. Tool Bar and Dialog Bar
- Page 23 and 24: Chapter 1. Tool Bar and Dialog Bar
- Page 25 and 26: Chapter 1. Tool Bar and Dialog Bar
- Page 27 and 28: Chapter 1. Tool Bar and Dialog Bar
- Page 29 and 30: Chapter 1. Tool Bar and Dialog Bar
- Page 31 and 32: Chapter 1. Tool Bar and Dialog Bar
- Page 33 and 34: Chapter 1. Tool Bar and Dialog Bar
- Page 35 and 36: Chapter 1. Tool Bar and Dialog Bar
- Page 37 and 38: Chapter 1. Tool Bar and Dialog Bar
- Page 39 and 40: Chapter 1. Tool Bar and Dialog Bar
- Page 41 and 42: Chapter 1. Tool Bar and Dialog Bar
- Page 43: Chapter 1. Tool Bar and Dialog Bar
- Page 47 and 48: Chapter 1. Tool Bar and Dialog Bar
- Page 49 and 50: Chapter 2. Menu Chapter 2 Menu Menu
- Page 51 and 52: Chapter 2. Menu ON_COMMAND(ID_EDIT_
- Page 53 and 54: Chapter 2. Menu “clipboard”. We
- Page 55 and 56: Chapter 2. Menu Using Class CMenu W
- Page 57 and 58: Chapter 2. Menu } After implementin
- Page 59 and 60: Chapter 2. Menu ); UINT nPosition,
- Page 61 and 62: Chapter 2. Menu void CMenuDoc::OnEd
- Page 63 and 64: Chapter 2. Menu } In the sample, bi
- Page 65 and 66: Chapter 2. Menu Message Mapping for
- Page 67 and 68: Chapter 2. Menu We can implement bi
- Page 69 and 70: Chapter 2. Menu CDC *ptrDC; CDC dcM
- Page 71 and 72: Chapter 2. Menu Because the popup m
- Page 73 and 74: Chapter 3. Splitter Window Chapter
- Page 75 and 76: Chapter 3. Splitter Window function
- Page 77 and 78: Chapter 3. Splitter Window BOOL CSp
- Page 79 and 80: Chapter 3. Splitter Window In the a
- Page 81 and 82: Chapter 3. Splitter Window Sample S
- Page 83 and 84: Chapter 3. Splitter Window WM_LBUTT
- Page 85 and 86: Chapter 4. Button Chapter 4 Buttons
- Page 87 and 88: Chapter 4. Button Use string text a
- Page 89 and 90: Chapter 4. Button function, first t
- Page 91 and 92: Chapter 4. Button Now we can remove
- Page 93 and 94: Chapter 4. Button } ( m_bBmpCheck ?
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