[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

mfc/vc++ subclassing

Posted on 1998-10-13
5
Medium Priority
?
972 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:bculver
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 2

Expert Comment

by:SamratAshok
ID: 1175050
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.
0
 
LVL 2

Expert Comment

by:SamratAshok
ID: 1175051
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.
0
 
LVL 1

Author Comment

by:bculver
ID: 1175052
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. :|
0
 
LVL 6

Expert Comment

by:WxW
ID: 1175053
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 .
0
 
LVL 2

Accepted Solution

by:
SamratAshok earned 600 total points
ID: 1175054
Iam slightly confused with your class1 and class2, so this is what Iam assuming them.
class1 is an instance of either a CDialog derived class or CDialog class object.
class2 contains the bitmap painting function derived from class1.

Nevertheless, I think you preety much are on the solution, just
not noticing it. All you need is this:

Derive a class from CDialog say CCustomBackgroundDialog (your generic class).
In this class, override OnInitDialog or OnPaint and just put whatever bitmap you want.

Iam mentioning a OnInitDialog because if you are creating a button with image,
you need to do it only once and then you need to override initdialog function.

If you are drawing bitmap directly on dialog device context, you need to override OnPaint

Now every class that you derive from CCustomBackgroundDialog will have
your bitmap.

Otherwise, sending or posting your code would be big help.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question