SubClassing Dialog Ctl - Question ? 
Author Message
 SubClassing Dialog Ctl - Question ?

I am looking at an example program and in it they do something I do
not understand.  Maybe someone can explain why it is done this way.

The program has a dialog.  On it there is a CEdit control.  Usually
this edit control has a member function (something like m_EditCtl) in
the dialog class.  And usually this control passes its value
to-and-from via a DDE function.

But in this case there is no member variable (that is not directly).
There is a variable (also called m_EditCtl) but this variable is
subclassed in the OnInitDialog as:

m_EditCtl.SubclassDlgItem(IDC_EDIT, this);

And there is no DDE for this control.  The value is retreived with:

GetDlgItemText(IDC_EDIT, buf, sizeof(buf));

Can someone explain why this method is used?  Or what the advantage of
subclassing is?  I have read the help on SubclassDlgItem but it did
not help (over my head).



Sat, 16 Jun 2001 03:00:00 GMT  
 SubClassing Dialog Ctl - Question ?
One reason you are probably confused is that there is no aparent reason to do what they did.
The subclass serves no purpose in this case...
I frequently use subclassing but I have the new class hide functionality like this...

//In the .H file
CMyCoolEditCtl m_EditCtl;

//In OnInitDialog()
m_EditCtl.SubclassDlgItem(IDC_EDIT, this);

m_EditCtl = 123.6456; //set it's initial value

//Elsewhere in dlg class say the OnOK()

if( m_EditCtl.GetValue() > m_dMyUperLimit)
{
    Scream at user

Quote:
}

The as you can see the new edit class hides a bunch of functionality and makes life much easier.
You can use Operator overloading to allow the class to take any type of data you want, and it all
looks just like another variable...This is especially nice when you want to apply formatting to your numbers.
Write the code once and away you go.
Quote:

> I am looking at an example program and in it they do something I do
> not understand.  Maybe someone can explain why it is done this way.

> The program has a dialog.  On it there is a CEdit control.  Usually
> this edit control has a member function (something like m_EditCtl) in
> the dialog class.  And usually this control passes its value
> to-and-from via a DDE function.

> But in this case there is no member variable (that is not directly).
> There is a variable (also called m_EditCtl) but this variable is
> subclassed in the OnInitDialog as:

> m_EditCtl.SubclassDlgItem(IDC_EDIT, this);

> And there is no DDE for this control.  The value is retreived with:

> GetDlgItemText(IDC_EDIT, buf, sizeof(buf));

> Can someone explain why this method is used?  Or what the advantage of
> subclassing is?  I have read the help on SubclassDlgItem but it did
> not help (over my head).



Sat, 16 Jun 2001 03:00:00 GMT  
 SubClassing Dialog Ctl - Question ?
Mike:

There are two things going on here:

1. Associating a member variable with the edit control itself.

2. Retrieving the value associated with the edit control.

With regard to the first issue, there are two ways to associate a member
variable with the control itself. The first is to do it with ClassWizard,
which does it using a DDX_Control statement in the DoDataExchange(), and
the other is to manually place a SubclassDlgItem statement in the
OnInitDialog(). These methods are equivalent. In both cases it is possible
for the variable, m_edit say, to be an object of a class derived from
CEdit, rather than just a plain CEdit object. This allows you to provide
added functionality to the control (as Keith explained), but perhaps more
importantly to modify the behavior of the control by intercepting its
windows messages. The latter is thus MFC's way of doing "windows
sub-classing".

There are also two ways of associating a variable, say m_text,  with the
text of the edit control. The first is to use ClassWizard, which does it
using a DDX_Text statement in the DoDataExchange(); this may be wasteful
if you have many controls but only wish to get the data from the one edit
control under consideration (and of course you need to call
UpdateData(TRUE) to effect the exchange). The other way is to just
retrieve the value yourself using GetWindowText() or GetDlgItemText(). Or,
as Keith explained, you can use your derived CEdit to write helper
functions (or operators) to make this easier.

HTH,

David Wilkinson

==============

Quote:

> I am looking at an example program and in it they do something I do
> not understand.  Maybe someone can explain why it is done this way.

> The program has a dialog.  On it there is a CEdit control.  Usually
> this edit control has a member function (something like m_EditCtl) in
> the dialog class.  And usually this control passes its value
> to-and-from via a DDE function.

> But in this case there is no member variable (that is not directly).
> There is a variable (also called m_EditCtl) but this variable is
> subclassed in the OnInitDialog as:

> m_EditCtl.SubclassDlgItem(IDC_EDIT, this);

> And there is no DDE for this control.  The value is retreived with:

> GetDlgItemText(IDC_EDIT, buf, sizeof(buf));

> Can someone explain why this method is used?  Or what the advantage of
> subclassing is?  I have read the help on SubclassDlgItem but it did
> not help (over my head).



Sat, 16 Jun 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Ctl-C, Ctl-X and Ctl-V in RichEditCtrl

2. newbie: subclassing richedit in an ActiveX MFC ctl

3. Subclassing the procedure belonging to a dialog of another application

4. Strange behavior when subclassing a static control in modal dialog

5. subclassing controls in dynamically biult dialog box

6. Subclassing dialog boxes

7. New MVP Essay: subclassing dialogs

8. Subclassing all dialogs edits

9. Subclassing Dialogs

10. Subclassing a Dialog

11. Subclassing dialog

12. Global Subclassing a common Dialog

 

 
Powered by phpBB® Forum Software