We help IT Professionals succeed at work.

problem with CWnd and FindWindow

benjaam asked
Medium Priority
Last Modified: 2008-02-26
I have a problem with VC++6. I'm a real newbie to C++
and my problem is the following:
If I write something like this

CWnd Window;

Cwnd* hAOL = Window.FindWindow (NULL, "AOL");

I think it should place into Window the handle of
AOL's window. But it just puts the handle into hAOL
and if I try to make something like this

Window.m_hWnd = hAOL;

after the code above I got the problem that
C++ cannot convert from class CWnd* into struct

Who can help me??
Watch Question


Edited text of question
Unlock this solution and get a sample of our free trial.
(No credit card required)

strange but true:   The code

   Cwnd* hAOL = Window.FindWindow (NULL, "AOL");

doesn't use the object Window.  FindWindow() is a static member function of CWnd.  Static member functions do not operate on objects, that is they do not need to be called for a paraticular object, like you appear to be doing there.  Now since they are part if a class, you do need to indicate the class to the compiler, this is because other classes could have functions called FindWindow() so you need to tell the compiler which class's FindWindow()  you are wanting to use.  This is usually done as

   Cwnd* hAOL = CWnd::FindWindow (NULL, "AOL");

That says use the FindWindow() that is part of CWnd.  Note that it is not providing a CWnd for the function to work with.  It doesn't need one.  Now what you had does the same thing, but is missleading to the reader (it certainly was to you)

   Cwnd* hAOL = Window.FindWindow (NULL, "AOL");

That says to the compiler, that you want to use the FindWindow() that is part of the same class as the class that Window belongs to.  It doesn't use Window in any way at run-time.  It just uses it at compile time to figure out what class is involved.

Thus the code

CWnd Window;

   Cwnd* hAOL = Window.FindWindow (NULL, "AOL");

will not change Window in any way.


next problem:  the code

Window.m_hWnd = hAOL;

is not goign to work.  First you should nto change m_hWnd() directly.  (it won't let you anyways).  It must be changed using the Attach() and Dettach() procedures.   like


However, these attach and detach window handles.  You don't have window handles.  You have CWnds

You are given a pointer to a CWnd, why don't you just use that pointer?


In the example I gave you (with AOL) it would be possible to use
this pointer. But if I, for example want to change the shape of a window I need


So I need a handle to a window in Window because I want to change this specific window's region.
And is there basically a possibility to give Window a handle with the FindWindow() function?

In MFC you will never have to use a window handle.  (unless maybe you are doing somthing really weird)  You will use window objects instead.

to do


you don't have to worry about the window handle, as long as you obtained "Window" through some sort of MFC mechanism that gives you window objects that are attached to window (that have their window handles set).  

If for some reason you had a window handle and don't have a window object for it you can attach a window object to the handle using Attach().  However, if you are find yourself using attach it probably means that you are making a mistake and are circumventign MFC rather than using it.  There may be exceptions where you really do have to use Attach(), but they should be rare.


So please tell me how to obtain Window through a MFC mechanism.
Let's say I want to get AOL's main window (class "AOL Frame25")
into Window.
How do I get it so that I can go on and work with Window to find child windows, change shapes, modify menus or so on?
(C++ is a bit confusing to me because I first made programs using VB and in VB the programs are completely different.)

>>  tell me how to obtain Window through a MFC mechanism
that depends on the circumstances.  ussuallly it is just passed as a parameter to a message handler.
>> Let's say I want to get AOL's main window (class "AOL Frame25")  into Window.
use FindWindow to get a pointer to the CWnd.

>> How do I get it so that I can go on and work with Window to find child windows
use the GetWindow() member function.  You call it once to get the first child using the parent CWnd pointer and GW_CHILD.  Then to get the other children you call it repeatidly using the last child found and GW_HWNDNEXT.  something like

CWnd* Par = CWnd::FindWindow (NULL, "AOL");
CWnd* Child = Par->GetWndow(GW_CHILD);
while (Child)
   // Do something with Child.
   Child = Child->GetWindow(GW_HWNDNEXT);

>> change shapes, modify menus or so on
on AOL's windows?  you can use the standard MFC commands to do this.  However should you?  If you add new options to AOL's menus will it be able to handle them?  If you resize AOL's windows will it handle it okay?  In general you don't want to muck around in someone else's program unless you absolutely have to.


ok...thanks for your help!

I think you really earned your points...:)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.