Advanced MFC Programming
Advanced MFC Programming Advanced MFC Programming
Chapter 7. Common Dialog Boxes The standard dialog template can be copied from file “Commdlg.dll”. By default, all the controls in this template will have a numerical ID. In order to make them easy to use, we can assign each ID a symbol, this can be done by inputting a text ID and assigning the control’s original ID value to it in “ID” window of the property sheet that is used for customizing the styles of a control. For example, when we open “Text Properties” property sheet for control 720, its “ID” window shows “720”. We can change it to “COLOR_BOX1=720”. By doing this, the control will have an ID symbol “COLOR_BOX1”, whose value is 720. In the sample, most of the controls are assigned ID symbols. We can hide certain unnecessary controls by calling function CWnd::ShowWindow(…) in dialog box’s initialization stage. However, there is an easier approach to it: we can resize the dialog template and move the unwanted controls outside the template (Figure 7-7). By doing this, these controls will not be shown in the dialog box, and therefore, can not respond to mouse clicking events. These controls are move outside the dialog template Figure 7-7. Custom color dialog template However, in Developer Studio, a control is confined within the dialog template and is not allowed to be moved outside it. A workaround for this is to edit the resource file directly. Actually, a dialog template is based on a text format resource file. In our sample, all type of resources are stored in file “CDB.rc”. By opening it in “Text” mode, we can find the session describing the color dialog template: …… CHOOSECOLOR DIALOG DISCARDABLE 2, 0, 150, 124 STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU CAPTION "Color" FONT 8, "Helv" BEGIN END LTEXT CONTROL "&Basic Colors:",-1,4,4,140,9 "",COLOR_BOX1,"Static",SS_SIMPLE | WS_GROUP | WS_TABSTOP, 59,14,85,86 The first four lines specify the properties of this dialog template, which include its dimension, styles, caption, and font. Between “BEGIN” and “END” keywords, all controls included in the template are listed. Each item is defined with a type description followed by a series of styles. In the above example, the first item is a static text control (LTEXT), which contains text “Basic Colors” ("&Basic Colors:"). It has an ID of 65535 (-1), located at (4, 4), and its dimension is (140, 9). In the above example, if we change a control’s horizontal coordinate to a value bigger than 150 (Because the dimension of the dialog template is 150×124), it will be moved outside the template. In the sample, two such dialog templates are prepared: “CHOOSECOLOR” and “CHOOSECUSCOLOR”. In “CHOOSECOLOR”, static text control COLOR_BOX1 is inside the template, and the area within this control will be used to draw base colors. For “CHOOSECUSCOLOR”, static text control COLOR_CUSTOM1 is inside the template, the area within this control will be used to draw custom colors (COLOR_BOX1 and COLOR_CUSTOM1 are used to define an area where the controls will be created dynamically for displaying colors). In both cases, frame COLOR_CURRENT is inside the template, which will be used to display the current selected color. 188
Chapter 7. Common Dialog Boxes Commands Implementation Function CCDBDoc::OnColordialogboxChoosebasecolor() is implemented as follows: void CCDBDoc::OnColordialogboxChoosebasecolor() { CColorDialog dlg; } dlg.m_cc.Flags|=CC_ENABLETEMPLATE; dlg.m_cc.Flags|=CC_FULLOPEN; dlg.m_cc.hInstance=(HWND)AfxGetInstanceHandle(); dlg.m_cc.lpTemplateName="CHOOSECOLOR"; dlg.DoModal(); There is nothing special for this function. The only thing we need to pay attention to is that we must set CC_FULLOPEN flag in order to display currently selected color. Otherwise control COLOR_CURRENT will not work. The following is the implementation of function CCDBDoc::OnColordialogboxChoosecostumcolor(): void CCDBDoc::OnColordialogboxChoosecostumcolor() { CColorDialog dlg; int i; COLORREF color[16]; } for(i=0; i
- Page 153 and 154: Chapter 5. Common Controls m_tabCtr
- Page 155 and 156: Chapter 5. Common Controls Custom R
- Page 157 and 158: Chapter 6. Dialog Box Chapter 6 Dia
- Page 159 and 160: Chapter 6. Dialog Box void CMLDialo
- Page 161 and 162: Chapter 6. Dialog Box } Function CP
- Page 163 and 164: Chapter 6. Dialog Box Everything is
- Page 165 and 166: Chapter 6. Dialog Box } MINMAXINFO;
- Page 167 and 168: Chapter 6. Dialog Box Sample Sampel
- Page 169 and 170: Chapter 6. Dialog Box } brush=(HBRU
- Page 171 and 172: Chapter 6. Dialog Box wndA.GetWindo
- Page 173 and 174: Chapter 6. Dialog Box rectStaticGrp
- Page 175 and 176: Chapter 6. Dialog Box } TOOLTIPTEXT
- Page 177 and 178: Chapter 6. Dialog Box ) { } if ( )
- Page 179 and 180: Chapter 6. Dialog Box pWndCtrl=GetW
- Page 181 and 182: Chapter 6. Dialog Box Sample 6.8-2\
- Page 183 and 184: Chapter 6. Dialog Box } // handle t
- Page 185 and 186: Chapter 6. Dialog Box 1) Tracking s
- Page 187 and 188: Chapter 7. Common Dialog Boxes DWOR
- Page 189 and 190: Chapter 7. Common Dialog Boxes } {
- Page 191 and 192: Chapter 7. Common Dialog Boxes } }
- Page 193 and 194: Chapter 7. Common Dialog Boxes dial
- Page 195 and 196: Chapter 7. Common Dialog Boxes Alth
- Page 197 and 198: Chapter 7. Common Dialog Boxes } NU
- Page 199 and 200: Chapter 7. Common Dialog Boxes Stc3
- Page 201 and 202: Chapter 7. Common Dialog Boxes DWOR
- Page 203: Chapter 7. Common Dialog Boxes the
- Page 207 and 208: Chapter 7. Common Dialog Boxes Retr
- Page 209 and 210: Chapter 7. Common Dialog Boxes 7.9
- Page 211 and 212: Chapter 7. Common Dialog Boxes } }
- Page 213 and 214: Chapter 7. Common Dialog Boxes …
- Page 215 and 216: Chapter 7. Common Dialog Boxes …
- Page 217 and 218: Chapter 8. DC, Pen, Brush and Palet
- Page 219 and 220: Chapter 8. DC, Pen, Brush and Palet
- Page 221 and 222: Chapter 8. DC, Pen, Brush and Palet
- Page 223 and 224: Chapter 8. DC, Pen, Brush and Palet
- Page 225 and 226: Chapter 8. DC, Pen, Brush and Palet
- Page 227 and 228: Chapter 8. DC, Pen, Brush and Palet
- Page 229 and 230: Chapter 8. DC, Pen, Brush and Palet
- Page 231 and 232: Chapter 8. DC, Pen, Brush and Palet
- Page 233 and 234: Chapter 8. DC, Pen, Brush and Palet
- Page 235 and 236: Chapter 8. DC, Pen, Brush and Palet
- Page 237 and 238: Chapter 8. DC, Pen, Brush and Palet
- Page 239 and 240: Chapter 8. DC, Pen, Brush and Palet
- Page 241 and 242: Chapter 8. DC, Pen, Brush and Palet
- Page 243 and 244: Chapter 8. DC, Pen, Brush and Palet
- Page 245 and 246: Chapter 8. DC, Pen, Brush and Palet
- Page 247 and 248: Chapter 8. DC, Pen, Brush and Palet
- Page 249 and 250: Chapter 8. DC, Pen, Brush and Palet
- Page 251 and 252: Chapter 8. DC, Pen, Brush and Palet
- Page 253 and 254: Chapter 9. Font Here, member lfFace
Chapter 7. Common Dialog Boxes<br />
The standard dialog template can be copied from file “Commdlg.dll”. By default, all the controls in<br />
this template will have a numerical ID. In order to make them easy to use, we can assign each ID a symbol,<br />
this can be done by inputting a text ID and assigning the control’s original ID value to it in “ID” window of<br />
the property sheet that is used for customizing the styles of a control. For example, when we open “Text<br />
Properties” property sheet for control 720, its “ID” window shows “720”. We can change it to<br />
“COLOR_BOX1=720”. By doing this, the control will have an ID symbol “COLOR_BOX1”, whose value is<br />
720. In the sample, most of the controls are assigned ID symbols.<br />
We can hide certain unnecessary controls by calling function CWnd::ShowWindow(…) in dialog box’s<br />
initialization stage. However, there is an easier approach to it: we can resize the dialog template and move<br />
the unwanted controls outside the template (Figure 7-7). By doing this, these controls will not be shown in<br />
the dialog box, and therefore, can not respond to mouse clicking events.<br />
These controls<br />
are move outside<br />
the dialog<br />
template<br />
Figure 7-7. Custom color dialog template<br />
However, in Developer Studio, a control is confined within the dialog template and is not allowed to<br />
be moved outside it. A workaround for this is to edit the resource file directly. Actually, a dialog template<br />
is based on a text format resource file. In our sample, all type of resources are stored in file “CDB.rc”. By<br />
opening it in “Text” mode, we can find the session describing the color dialog template:<br />
……<br />
CHOOSECOLOR DIALOG DISCARDABLE 2, 0, 150, 124<br />
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU<br />
CAPTION "Color"<br />
FONT 8, "Helv"<br />
BEGIN<br />
END<br />
LTEXT<br />
CONTROL<br />
"&Basic Colors:",-1,4,4,140,9<br />
"",COLOR_BOX1,"Static",SS_SIMPLE | WS_GROUP | WS_TABSTOP,<br />
59,14,85,86<br />
The first four lines specify the properties of this dialog template, which include its dimension, styles,<br />
caption, and font. Between “BEGIN” and “END” keywords, all controls included in the template are listed.<br />
Each item is defined with a type description followed by a series of styles. In the above example, the first<br />
item is a static text control (LTEXT), which contains text “Basic Colors” ("&Basic Colors:"). It has an ID<br />
of 65535 (-1), located at (4, 4), and its dimension is (140, 9).<br />
In the above example, if we change a control’s horizontal coordinate to a value bigger than 150<br />
(Because the dimension of the dialog template is 150×124), it will be moved outside the template.<br />
In the sample, two such dialog templates are prepared: “CHOOSECOLOR” and “CHOOSECUSCOLOR”. In<br />
“CHOOSECOLOR”, static text control COLOR_BOX1 is inside the template, and the area within this control will<br />
be used to draw base colors. For “CHOOSECUSCOLOR”, static text control COLOR_CUSTOM1 is inside the<br />
template, the area within this control will be used to draw custom colors (COLOR_BOX1 and COLOR_CUSTOM1<br />
are used to define an area where the controls will be created dynamically for displaying colors). In both<br />
cases, frame COLOR_CURRENT is inside the template, which will be used to display the current selected color.<br />
188