Now *this* is scary 
Author Message
 Now *this* is scary

Have any of you looked in the MFC source for the implementation of the
CCtrlView class and its derivitives?

I thought something smelled funny when the CTreeView's implementation of
GetTreeCtrl() returned a reference to *itself* yet neither CTreeView nor
CTreeCtrl are derived from each other.  The m_hWnd for the CTreeView object
actually refers to a tree control window and the MFC CTreeCtrl wrapper is
never actually instantiated even though it pretends that there is one.

The CTreeView and CTreeCtrl classes have a common ancestor - CWnd.  The hack
works only because:

CTreeCtrl doesn't contain any additional member variables from CWnd so it
looks sort of like a base class in memory to CTreeView.  

CTreeCtrl also doesn't add any new virtual methods, so using CTreeView's vtbl
for calling CTreeCtrl's inherited virtual methods is only a "small" problem.

WM_NOTIFY messages are reflected so that your CTreeView-derived view can get
them.  That's because your CTreeView window doesn't really contain the tree
control, it *is* the tree control, and your CTreeView class is pretending to
inherit from CTreeCtrl even though it doesn't.

Some folks might think that's "clever".  I have a different word in mind.

Scott Amspoker          |

http://www.*-*-*.com/ |



Fri, 20 Jul 2001 03:00:00 GMT  
 Now *this* is scary

Quote:

>Have any of you looked in the MFC source for the implementation of the
>CCtrlView class and its derivitives?

>I thought something smelled funny when the CTreeView's implementation of
>GetTreeCtrl() returned a reference to *itself* yet neither CTreeView nor
>CTreeCtrl are derived from each other.  The m_hWnd for the CTreeView object
>actually refers to a tree control window and the MFC CTreeCtrl wrapper is
>never actually instantiated even though it pretends that there is one.

>The CTreeView and CTreeCtrl classes have a common ancestor - CWnd.  The hack
>works only because:

>CTreeCtrl doesn't contain any additional member variables from CWnd so it
>looks sort of like a base class in memory to CTreeView.  

>CTreeCtrl also doesn't add any new virtual methods, so using CTreeView's vtbl
>for calling CTreeCtrl's inherited virtual methods is only a "small" problem.

>WM_NOTIFY messages are reflected so that your CTreeView-derived view can get
>them.  That's because your CTreeView window doesn't really contain the tree
>control, it *is* the tree control, and your CTreeView class is pretending to
>inherit from CTreeCtrl even though it doesn't.

>Some folks might think that's "clever".  I have a different word in mind.

MFC does this because it doesn't support multiple inheritance from two
classes derived from CObject, and CCmdTarget in particular. I know of two
major consequences, from the perspective of a user:

1. RTTI can fail.
2. It's impossible to design a new view class by mixing derived CView and/or
CWnd classes. I've worked around this problem by making my custom window a
child of the view, which isn't as good.

--
Doug Harrison



Fri, 20 Jul 2001 03:00:00 GMT  
 Now *this* is scary

Quote:

>This is very interessting, I wondered about it, too, when I was searching for a
>way to
>use the MSFLEXGRID control as a view in a MDI-app.

I discovered it because I had created a CTreeCtrl-derived control that I
wanted to use in various places - including on my SDI frame.  I was trying to
figure out how to make it use my derived control class instead of the
CTreeCtrl.  That's when I made the grizzly discovery.

Quote:
>Is it possible to do this hack with an Active-X control?
>Or do I have to create the controll in the view and resize it whenever the
>windows
>changes in size?

Yep.  I created an SDI frame with a vanilla CView window and simply plastered
my tree control on it.  The CView window needs to create/destroy the tree
control and resize it when necessary.  Pretty simple, actually.

Scott Amspoker          |

http://www.rt66.com/sda |



Sun, 22 Jul 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Scary UB

2. This is scary

3. Top 10 scariest posters to comp.lang.c

4. Want to hear something scary?

5. I am new to programming and am lost

6. how good am I? Am I Good Enough????

7. Determine where I am running

8. System.Threading.Timer , am I doing this correctly ?

9. Am i connected?

10. Request - Opinions on Book I am considering:

11. I am puzzled: __nogc new = LNK2001

12. Am I in the wrong NG???

 

 
Powered by phpBB® Forum Software