Solved

Creating a child window

Posted on 1998-03-10
10
925 Views
Last Modified: 2013-11-20
I want to create a simple child. For this purpose I created a new class derived from CWnd. I wrote a new Create method:
RECT WindowRect;
WindowRect.left=0;
WindowRect.top=0;
WindowRect.right=400;
WindowRect.bottom=400;
if(CWnd::Create(NULL,"AviWindow",WS_OVERLAPPED|WS_BORDER|WS_CAPTION|WS_VISIBLE|WS_CHILD,WindowRect,AfxGetMainWnd(),NULL,NULL))
      {            
      return(1);
      }

But I do not get a visible new child window when I call this method. I can call the program but nothing happens despite the fact that I get a warning:
Warning: calling DestroyWindow in CWnd::~CWnd; OnDestroy or PostNcDestroy in derived class will not be called.
What is wrong ?
0
Comment
Question by:MarcoHelmers
  • 5
  • 4
10 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 70 total points
Comment Utility
You're not creating the window correctly.  The code below will create a window but it's not a very useful one.  What are you trying to do in the window?


      RECT WindowRect;
      WindowRect.left=0;
      WindowRect.top=0;
      WindowRect.right=400;
      WindowRect.bottom=400;

      CWnd *myWnd = new CWnd();
      if(!myWnd->Create(NULL, "AviWindow", WS_OVERLAPPED|WS_BORDER|WS_CAPTION|WS_VISIBLE | WS_CHILD,WindowRect,AfxGetMainWnd(),NULL,NULL)){
            AfxMessageBox("Error in Create", MB_OK);
            delete myWnd;
      }
      
      myWnd->ShowWindow(SW_SHOWNORMAL);
0
 

Author Comment

by:MarcoHelmers
Comment Utility
This does not help me very much. I am trying to play a Video in Fullscreen Mode. Right now I am creating a child mci window and use Direct Draw to switch this window to Fullcreen Mode. Everything is alright. The video plays in Fullscreen Mode and stops when it is finished or 'ESC' is pressed. But if I try to do multitasking the applications hangs up. So I thought that it might help if I would switch back to normal mode when multitasking is performed. I know that I have to write this code into WM_ACTIVATEAPP (right?) but I can only do so if I have a new class in the class wizard which belongs to the Fullscreen Window. I cannot use the CMainFrame because in Fullscreen Mode I would have the Menu and the window frame and that does not look very good (or is there any possibility to hide those two things).
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
I'm afraid you've lost me.  The code I posted above will create a CWnd window on the screen of your specified size (400 x 400) as a child to the application's main window.  This is what you asked for.

If you do the following:

myWnd->Create(NULL, "AviWindow", WS_OVERLAPPED |WS_VISIBLE | WS_CHILD,WindowRect, AfxGetMainWnd(),NULL,NULL);

myWnd->ShowWindow(SW_SHOWMAXIMIZED);

You'll get a "blank" window without title bar or window controls.  It will also completely fill the owner window.
0
 

Author Comment

by:MarcoHelmers
Comment Utility
But I do not have a class to receive the WM_ACTIVATEAPP message with the code you offer, right ?
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
You didn't show your CWnd derived window class in the example above. Assuming that you have added a handler to that class for the WM_ACTIVATEAPP message and that your derived class is called MyDerivedCWnd, you create the window like this:

CWnd *myWnd = new MyDerivedCWnd();
if(!myWnd->Create(NULL, "AviWindow", WS_OVERLAPPED|WS_BORDER|WS_CAPTION|WS_VISIBLE | WS_CHILD,WindowRect,AfxGetMainWnd(),NULL,NULL)){
AfxMessageBox("Error in Create", MB_OK);
delete myWnd;
}

myWnd->ShowWindow(SW_SHOWNORMAL);


0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 4

Expert Comment

by:jtwine100697
Comment Utility
  Please describe the extent of the multitasking that you are trying to do.  If you are creating a worker thread, remember the threads get their own message queue, and you will need to pump messages with it.

-=- James.
0
 

Author Comment

by:MarcoHelmers
Comment Utility
I found out that it is not possible to create a new parent window with the create command of CWnd. Can you tell me how to do this with the CreateEx command ? I do not know which parameters to use when I only want to create a new parent window.
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
Here's an example of using the CWnd::CreateEx.  Again, this example uses CWnd but you should derive your own window class from CWnd and use it instead:


      CWnd *myWnd = new CWnd;
      if(!myWnd->CreateEx(
            0,
            AfxRegisterWndClass(0),
            "MyWindowName",
            WS_OVERLAPPEDWINDOW,
            0, 0, 500, 500,
            NULL,
            NULL,
            NULL
      )){
            AfxMessageBox("ERROR: CreateEx() failed!", MB_OK);
            delete myWnd;
            return;
      }
      myWnd->ShowWindow(SW_NORMAL);
0
 

Author Comment

by:MarcoHelmers
Comment Utility
Everything works fine now. The last small problem I have is the same I already described above : I receive a warning :

Warning: calling DestroyWindow in CWnd::~CWnd; OnDestroy or PostNcDestroy in derived class will not be called.

Why do I receive this warning and what can I do to avoid this ? Despite this warning everything works fine, but I am interested in getting to know what it means.
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
Microsoft Knowledge Base
PRB: "Warning: Calling DestroyWindow in CWnd::~CWnd" Message
Last reviewed: May 28, 1997
Article ID: Q105081  
The information in this article applies to:
The Microsoft Foundation Classes (MFC) included with:

    - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, 1.52
    - Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, 2.1, 4.0



SYMPTOMS
The following warnings are displayed when a debug version of a program is executed and TRACE() diagnostics are enabled:


   Warning: calling DestroyWindow in CWnd::~CWnd
   OnDestroy or PostNcDestroy in derived class will not be called

CAUSE
The above warnings are displayed by the CWnd::~CWnd() destructor. The warnings indicate the following:

The programmer has most likely called "delete" on a CWnd object rather than DestroyWindow().
An object derived from CWnd is not performing a DestroyWindow() call in its destructor.
Because the DestroyWindow() call is executed in the destructor for CWnd, only the CWnd portion of the CWnd-derived object is remaining when the DestroyWindow() is called. Thus, if you have a CWnd-derived object and receive this warning, the derived object's virtual functions will not exist and the CWnd functions will be called instead.

RESOLUTION
MFC Technote #17 contains more information on how to properly destroy a Window object and states the following cardinal rule:


   To destroy a C++ Windows object, use DestroyWindow, not "delete".

Also, examining CWnd::~CWnd() will reveal that the foremost task of the destructor is to ensure that the associated Windows window object has been destroyed. If a program calls DestroyWindow() before the CWnd destructor is called, the m_hWnd data member will be NULL, the TRACE0() messages will not occur, and DestroyWindow() will not be called in CWnd::~CWnd().  

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now