Using a derived class from CEdit in my DIalog

I am using an application using MFC. I just created a class that was obtained from CEdit, so I could intercept OnChar () and perform data validation. How do I replace the edit control in my application with a derivative that I made?

+4
source share
2 answers

I have found a solution. The reason why it was so hard for me was that I did not use the Class Wizard to create a new class, which complicated the situation. If you just use the Class Wizard, you can add control variables to derived classes, for example, if they were ordinary classes, if the base class is the correct class for your element. It's not obligatory. All you have to do is create a pointer to the type of your derived class and throw the element you are trying to get, for example, you usually do with a non-derived class.

An example of accessing an edit control using a class derived from CEdit

CMyCustomEdit * editPtr = (CMyCustomEdit*)GetDlgItem(IDC_EDIT1); 

As mentioned by another member below (thanks for that), using GetDlgItem is not a good idea. I actually, in my code, finished Sub-Classing so that I could use my new class with my Edit Controls that already existed. As mentioned earlier, I did not understand that the Edit control is not necessarily bound to CEdit, so the above example should give a clear idea that your IDC_EDIT can be used as CMyCustomEdit, as well as CWnd, etc .; it will behave naturally as long as you refer to it with the right classes.

Now for the subclass. If you really want you to edit Control to always call a derived class in front of your base class, you will have to make it a subclass. Do not think of it as an object-oriented concept, it means that messages (for example, WN_CHAR) will first go through your derived class and then call the base class.

An example of a subclass of CMyCustomEdit in an Edit control:

First you need to include the .h file of your new class in the .cpp and .h of your dialog box. These are the ones that usually have the same name as your project. Here will be MyMainDialog.

 #include "CMyCustomEdit.h" 

In a derived dialog class, a variable of the type of your new derived class is included:

 class MyMainDialog : public CDialogEx { protected: CMyCustomEdit m_cmCEdit; } 

Then, in OnInitDialog () of your derived dialog class (MyMainDialog) subclass your edit control. For security, add this after regular code to the function and before returning (as usual):

 m_cmCEdit.SubclassDlgItem(IDC_EDIT1, this); 

After that, when you do something in your Edit control with IDC_EDIT1, the messages will go through CMyCustomEdit before going to CEdit. This is usually necessary when you need to overwrite messages from base classes.

Hope this helps anyone who has a similar question.

+1
source

Do NOT use GetDlgItem !!

GetDlgItem() returns a CWnd -pointer and nothing else. This means that you have a chopped pointer CMyCustomEdit . Of course, it works in all cases when your method sends a message to the underlying HWND . But this is just luck! Read more about the problem here .

The right solution is to subclass your edit control using DDX_Control .

+1
source

Source: https://habr.com/ru/post/1385301/


All Articles