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

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

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

Saved successfully!

Ooh no, something went wrong!