Link to home
Start Free TrialLog in
Avatar of bculver
bculver

asked on

mfc/vc++ subclassing

I am trying to subclass a CWnd class1,not just a control from another CWnd class2 to take over paint messages among others. First attempts failed because CWnd::SubclassWindow() requires the HWND be detached from the CWnd class. (can't do because then class1 will no longer have a valid HWND) Have had better success with the following:

OldProc = SetWindowLong(hwnd,GWL_WNDPROC,fn);
where hwnd= the handle of the windows to subclass
and fn = the address of the new message handler.

Now for the problem: the new message handler (fn) is
a function in a class also. (a class trying to subclass another class) class2::fn actually does get called however the "this" point is invalid, so no member variables are accessible. The "this" pointer passed to class2::fn apparently is the class1::m_hWnd of the original CWnd.

My question is 2 part; first is this a valid approch or is there an easier way of doing this ( or is this posible). Second (vc++ question) how do you obtain a "this" pointer for a class. I can think of a few other needs. mainly C/asm functions calling a C++ class function. thx!

bculver@hathawaycorp.com
Avatar of SamratAshok
SamratAshok

I can't imagine under what conditions you should have HWND already attached. Probably
you need two CWND (or derived) class objects defined. In any case, detaching old one
is not bad, cause you will be re attaching next one anyway.

If if does not help, please post your code-snippet.
I can't imagine under what conditions you should have HWND already attached. Probably
you need two CWND (or derived) class objects defined. In any case, detaching old one
is not bad, cause you will be re attaching next one anyway.

If if does not help, please post your code-snippet.
Avatar of bculver

ASKER

HWND is attached because CWnd::class1 is actually a dialog (CDialog) derived from
CWnd. You can't detach because the dialog class would have no HWND to work
with. I am actually tring to create a generic class to handle painting pix's on dialog
backgrounds. I don't want to take over complete control, ie. detaching HWND and
reattaching to the new class, just intercept some paint/palette messages. Class2
is actually derived from class1 and I could trap each message from class1 and make
the appropriate calls to class2, however I was hoping for something a little more elegant
like:  class2::SetBackgroundPic(picture);  in the OnInitDialog() have class2 subclass class1 functions and be done with it. :|
I suggest you don't combine MFC with API that way . Create a regular ( not in the class ) function to handle the messages and store any "this" pointer to a global variable .
ASKER CERTIFIED SOLUTION
Avatar of SamratAshok
SamratAshok

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial