problem with CWnd and FindWindow

Posted on 1998-07-26
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??
Question by:benjaam
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
  • 6
  • 4

Author Comment

ID: 1168673
Edited text of question
LVL 22

Accepted Solution

nietod earned 50 total points
ID: 1168674
answer coming.
LVL 22

Expert Comment

ID: 1168675
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.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 22

Expert Comment

ID: 1168676
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?


Author Comment

ID: 1168677
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?
LVL 22

Expert Comment

ID: 1168678
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.

Author Comment

ID: 1168679
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.)
LVL 22

Expert Comment

ID: 1168680
>>  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);
LVL 22

Expert Comment

ID: 1168681
>> 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.

Author Comment

ID: 1168682
ok...thanks for your help!

I think you really earned your points...:)

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create string splitting user defined function in C++ 23 337
Quicksort a dynamic deque 33 79
Socket Programming (Unix) 8 153
Add values of each row in an array 3 70
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

733 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