Click here to Skip to main content
14,937,037 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:

I have an old SDI application under VS 7.1 in C++ using MFC.
And I would like to derive CEdit class to add some features (e.g. CEditEx).

I did not find how to use this CEditEx class in my CEditView derived class; it refers to CEdit capabilities only.

Thank you in advance for your help (with, if possible, some additional explanation about link between CEdit and CEditView),

What I have tried:

I tried to overload GetEditCtrl().
Updated 4-Aug-18 23:23pm

explanation about link between CEdit and CEditView
CEditView is a bit special. While not inherited form CEdit, it is behaving like one. See afxext.inl:
// CEditView
// NOTE: The cast in GetEditCtrl is ugly, but must be preserved for compatibility.
// CEdit is not related to CEditView by inheritance so we must be careful to ensure 
// that CEdit remains a binary compatible subset of CEditView.
_AFXEXT_INLINE CEdit& CEditView::GetEditCtrl() const
	{ return *(CEdit*)this; }

The solution is to implement the required functionality in your CEditView derived class.

If you have the source code of an extended CEdit class, copy the required code portions to your CEditView based class and prefix access to all CEdit members with GetEditCtrl().
Member 13916604 6-Aug-18 7:16am
Thank you, I will try by this way
Member 13916604 8-Aug-18 4:56am
To keep you in touch; I am implementing features in doc and view. It's quite done, but, to have a clean design I am wondering if it is possible to send a message from view to doc (with parameters)? As it seems that WM_MESSAGE are reserved to view and wnd.
Jochen Arndt 8-Aug-18 5:41am
The common method is to access the document using CView::GetDocument() and cast it to your document class (or better implement a function like GetMyDocument() in your view class returning already your document class type) and call the member functions of your document class.

Because CDocument is CCmdTarget based, it can handle messages. For application specific messages define your own ones using WM_APP plus offsets (see and send them to the main frame window. If no other window (main frame itself, active child frame, view of active child frame) handles the message, it is finally routed to the document.
Member 13916604 9-Aug-18 7:33am

Your first proposal is what I've done, but I would like to keep doc functions private.

I am trying your second proposal which is exactly what I am looking for.
If my handler function is in CMainFrame it works, but not in doc.

Here is my doc's message map:

But, I have the following cast issue:
error C2440: 'static_cast' : cannot convert from 'LRESULT (__thiscall CMyDoc::* )(WPARAM,LPARAM)' to 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)'

I tried to force cast using reinterpret_cast<LRESULT (CWnd::*)(WPARAM, LPARAM)>(); it compiles, but my function is not called.

Perhaps, I missed something...
Jochen Arndt 9-Aug-18 7:50am
It is not enough code to determine your fault.

But with the MFC document view model, the derived view classes are connected to the derived doc class. They are related and belong together. Therefore, it is no problem to call doc functions from the view directly. That is also the common method. So why do it different and more complicated than all other's do it?

If you need to protect the doc functions, make them protected and declare your view class(es) as friend(s). Then only the doc itself and the friends (views) can call these functions.
Member 13916604 25-Aug-18 6:29am
Job is done in this way ^^
Thank you for your support and explanations
See CEditView Class[^], where it shows the parent class, so you can see what it inherits.
Member 13916604 6-Aug-18 7:18am
In fact CEdit is neither a member of CEditView nor having inheritance relationship. That's why it is tricky...
Richard MacCutchan 6-Aug-18 8:56am
That was the point of my response and the link I gave you.
Member 13916604 8-Aug-18 4:58am
Sorry, I read your answer a little fast and didn't catch your point
Richard MacCutchan 8-Aug-18 5:11am
You need to look at the CEditView class to see its relationship with the CEdit object. Since the two are closely bound together, it would be quite difficult to inject your own class instead. You need to look at the constructors of the CEditView and its parent classes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900