Subclassing MFC windows 
Author Message
 Subclassing MFC windows

You might want to take a look at PreTranslateMsg. This is very useful for
"catching" msgs before MFC gets a shot at them.

Tom Archer

Quote:
>Hello-

>I need to subclass a window created within MFC.  I have no
>problem subclassing "normal" (non-mfc) windows using SetWindowLong().
>Everything works fine in the "normal" case.

>In the MFC my subclass procedure doesn't get any window messages.
>Wandering through the MFC source, it would seem that they're
re-subclassing
>the window for thier own purposes.

>My question:
>How do I either
>a) subclass an MFC window in such a way that I'm guaranteed to get all
the
>   messages first
>   -- or --
>b) let MFC know that there are certain messages I want dropped through to
>   my subclass, and it should let them go (it appears in the MFC source
>that
>  most messages get filtered off).

>Thanks,
>Derek




Tue, 01 Feb 2000 03:00:00 GMT  
 Subclassing MFC windows

Hello-

I need to subclass a window created within MFC.  I have no
problem subclassing "normal" (non-mfc) windows using SetWindowLong().
Everything works fine in the "normal" case.

In the MFC my subclass procedure doesn't get any window messages.
Wandering through the MFC source, it would seem that they're re-subclassing
the window for thier own purposes.

My question:
How do I either
a) subclass an MFC window in such a way that I'm guaranteed to get all the
   messages first
   -- or --
b) let MFC know that there are certain messages I want dropped through to
   my subclass, and it should let them go (it appears in the MFC source
that
  most messages get filtered off).

Thanks,
Derek



Tue, 01 Feb 2000 03:00:00 GMT  
 Subclassing MFC windows

Hi Derek,

Along with the reply by Tom Archer, windows in MFC are subclassed by the
CWnd function SubclassWindow.  If you use the Win32 SubclassWindow MFC
won't know about your subclass.

HTH
--
===================
Frank Hickman
SecurTech, Inc.


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



Quote:
> Hello-

> I need to subclass a window created within MFC.  I have no
> problem subclassing "normal" (non-mfc) windows using SetWindowLong().
> Everything works fine in the "normal" case.

> In the MFC my subclass procedure doesn't get any window messages.
> Wandering through the MFC source, it would seem that they're
re-subclassing
> the window for thier own purposes.

> My question:
> How do I either
> a) subclass an MFC window in such a way that I'm guaranteed to get all
the
>    messages first
>    -- or --
> b) let MFC know that there are certain messages I want dropped through to
>    my subclass, and it should let them go (it appears in the MFC source
> that
>   most messages get filtered off).

> Thanks,
> Derek




Wed, 02 Feb 2000 03:00:00 GMT  
 Subclassing MFC windows



Quote:
>I need to subclass a window created within MFC.  I have no
>problem subclassing "normal" (non-mfc) windows using SetWindowLong().
>Everything works fine in the "normal" case.

The problem is that MFC subclasses every window it creates and it uses
a 1-to-1 handle-to-object map so CWnd::FromHandle() will work.  If you
try to use SubclassWindow to subclass a window already subclassed by
MFC it will cause an assertion.  The toolkit my company offers
provides a class that allows you to subclass any window in your
application but that isn't the only way to get the job done.  If you
are doing SetWindowLong() subclassing successfully for non-MFC windows
but having trouble with the MFC ones, the problem is most likely due
to the ramifications of the 1-to-1 handle mapping and the assumptions
that MFC makes about who owns what.

If you're creating the window with the MFC Create() functionality, but
not seeing the messages you want in your derived class, you can
override either CWnd::PreTranslateMessage or CWnd::WindowProc.

It's not entirely clear to me from your posting why you need to
subclass the window if MFC has created it; wouldn't it be easier to
let MFC create a derived class with your special processing in it?  I
realize there are cases when this isn't the way to go, just don't have
enough information to offer more.  hth.  -steve
____________________________________________________________________
McAdams Research - http://www.CodeTools.com/showcase
  MFC Toolkit: flat-style toolbar, resizable docking windows, more



Thu, 03 Feb 2000 03:00:00 GMT  
 Subclassing MFC windows


Quote:
>Hi Derek,

>Along with the reply by Tom Archer, windows in MFC are subclassed by the
>CWnd function SubclassWindow.  If you use the Win32 SubclassWindow MFC
>won't know about your subclass.

>HTH

..or you can use DDX_Control in DoDataExchange().

--------------------------------------------------------------------------------


XIOtech Corporation               Personal Home Page:   http://www.visi.com/~nde
Eden Prairie, MN

"Question authority...and the authorities will end up questioning YOU!"
--------------------------------------------------------------------------------



Thu, 03 Feb 2000 03:00:00 GMT  
 Subclassing MFC windows



Quote:
>Hello-

>I need to subclass a window created within MFC.  I have no
>problem subclassing "normal" (non-mfc) windows using SetWindowLong().
>Everything works fine in the "normal" case.

>In the MFC my subclass procedure doesn't get any window messages.
>Wandering through the MFC source, it would seem that they're re-subclassing
>the window for thier own purposes.

>My question:
>How do I either
>a) subclass an MFC window in such a way that I'm guaranteed to get all the
>   messages first
>   -- or --
>b) let MFC know that there are certain messages I want dropped through to
>   my subclass, and it should let them go (it appears in the MFC source
>that
>  most messages get filtered off).

>Thanks,
>Derek


Well, a *lot* of the window subclassing you want to do is intrinsic in
the MFC architecture.  For example, if you have a window class that
you want to subclass, just declare a class
        class MyWindow : public CWnd { ...}

and if you use ClassWizard to do it, you'll get much of what
subclassing normally buys you.  A phrase I love to use about MFC is
that the C++ object model is almost, but not quite, totally unlike the
Windows object model; the subclassing Microsoft does is an amazing
tour-de-force that makes these two nearly disjoint object models
cooperate.

Another case, let's say you want to subclass a listbox.  Use
classWizard to declare your new class, essentially
        class MyListBox : public CListBox {...}

Now for most of the events, you just use the ClassWizard to build
handlers for the events.  Let's say this is in a dialog which you've
derived from CDialog.  Use the ClassWizard to declare a variable of
the type MyListBox, let's say 'whatever',  which will put the
following declaration in your .h file:
        MyListBox whatever;

Once your dialog has initialized, you can use the MyListBox:: methods
(and even add your own cool methods to this class), and you will get
just about everything you need.

This works well for *almost* everything.  Unfortunately, it is
sometimes necessary to really subvert the MFC mechanisms for authentic

"Windows" subclassing.  This is only moderately tricky.  I do
something like this:

add to your class definition
        DWORD m_oldproc;

then add this function

        void CMyListBox::subclass(WNDPROC proc)
                      {
                        m_oldproc = ::GetWindowLong(m_hWnd,
GWL_WNDPROC);
                        ::SetWindowLong(m_hWnd, GWL_WNDPROC,
(DWORD)proc);
                        ::SetWindowLong(m_hWnd, GWL_USERDATA,
(LPARAM)this);
                      }

Now your callback works like you'd expect....

static CALLBACK MyWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM
lParam)
     {
       CMyListBox * object = (CMyListBox *)GetWindowLong(hWnd,
GWL_USERDATA);
       ....// do your usual window stuff here
       return ::CallWindowProc(object->m_oldproc, hWmd, msg, wParam,
lParam);
     }

Note that because you've stored the 'this' pointer in GWL_USERDATA,
you can now call methods of the class to do whatever you want while
working within the MFC structure.  

But before you go through all of this, make sure that the ordinary MFC
subclassing isn't adequate for the job.  I can do probably 99% of the
subclassing I need.

(At the risk of sounding commercial, I recently completed a book on
Win32 programming that has a CDROM with 150K+ lines of MFC examples
just full of neat tricks)
                                joe



Tue, 15 Feb 2000 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Subclassing Windows class in MFC

2. subclassing a MFC subclassed window

3. Subclassing in MFC

4. newbie: subclassing richedit in an ActiveX MFC ctl

5. MFC Subclassing

6. Subclassing Activex controls from commctrl (treectrl) in MFC

7. Subclassing (superclassing?) MFC control from commctrl (treectrl)

8. Subclassing MFC

9. MFC OCX Subclassing Problem

10. MFC Clist control subclassing

11. Subclassing window

12. Subclassing foreign window and changing memory in it

 

 
Powered by phpBB® Forum Software