?
Solved

Move child dialog to background

Posted on 2007-08-09
18
Medium Priority
?
567 Views
Last Modified: 2013-11-20
I'm working on an MFC dialog based program in Visual Studio 2005 under Windows XP. From the main window I launch a child window. The I need the child window to go behind the parent when the parent gets focus. The way it is now, when the parent gets focus I can interact with it, but the child is still on top and needs to be moved to the side so I can see all of the parent.

Here's how I'm creating the child:

CDialog* gchild;

void CWinGPIODlg::OnToolsShowOverview()
{            
            
            gchild = new CChildGPIODisplay;            
            gchild->Create(CChildGPIODisplay::IDD,NULL);            
            gchild->ShowWindow(SW_SHOW);
            
            }
0
Comment
Question by:apostrophe27
  • 7
  • 6
  • 2
  • +1
16 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 19666044
Check the window styles of the child dialog (is it topmost for example).


When the parent gets the focus you could minimise/hide the child dialog or use SetWindowPos to make it lower in the z-ordering.
0
 
LVL 3

Expert Comment

by:Dimkov
ID: 19666938
void BringWindowToTop( );


The BringWindowToTop member function should be used to uncover any window that is partially or completely obscured by any overlapping windows.


BOOL SetWindowPos(
   const CWnd* pWndInsertAfter,
   int x,
   int y,
   int cx,
   int cy,
   UINT nFlags
);

pWndInsertAfter
Identifies the CWnd object that will precede this CWnd object in the Z-order. This parameter can be a pointer to a CWnd or a Pointer to one of the following values:


Hope this helps
0
 

Author Comment

by:apostrophe27
ID: 19669844
In the child dialog proerties, topmost is false.

I'd like the child to still be visible, not minimized or hidden, so the user can easily switch between the child and parent.

How do I implement the SetWindowPos or SetWindowPos function?
0
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.

 
LVL 45

Expert Comment

by:AndyAinscow
ID: 19669979
Just try changing the following

gchild = new CChildGPIODisplay;
gchild->Create(CChildGPIODisplay::IDD,NULL);  
 
to this

gchild = new CChildGPIODisplay(this);
gchild->Create(CChildGPIODisplay::IDD,this);    

first - maybe that will solve it (rather than patch it)
0
 

Author Comment

by:apostrophe27
ID: 19670273
No good, same problem.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 19670362
OK - when your main window gets the focus try this
SetWindowPos(HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE |  SWP_SIZE);
0
 

Author Comment

by:apostrophe27
ID: 19671154

>>SetWindowPos(HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE |  SWP_SIZE);

I received a compiler error:
.\WinGPIODlg.cpp(849) : error C2065: 'SWP_SIZE' : undeclared identifier
.\WinGPIODlg.cpp(849) : error C2664: 'CWnd::SetWindowPos' : cannot convert parameter 1 from 'HWND' to 'const CWnd *'

Also, I assume I'm putting that call in my CWinGPIODlg::OnSetFocus( CWnd* pWndChild) function. When I break in the :OnSetFocus function, the program doesn't go there when I switch focus from the child to the parent. I do hit the break if I exit a file dialog box and return to the parent.

0
 

Author Comment

by:apostrophe27
ID: 19671740
I think I have a solution:

Changing this:
gchild->Create(CChildGPIODisplay::IDD,NULL);  

to

gchild->Create(CChildGPIODisplay::IDD,GetDesktopWindow());

gives me the behavior I want.

Is there anything wrong with this? Are these two windows now siblings?
0
 
LVL 3

Expert Comment

by:Dimkov
ID: 19671873
I don't thinks so
Now the desktop window is the father of gchild.... maybe tou should use this pointer instead,
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 19675054
Is the dialog set as a CHILD or a POPUP ?  I think it should be as a POPUP (and you wouldn't require the SetWindowPos code).

ps. compiler error SWP_SIZE should be SW_NOSIZE
0
 

Author Comment

by:apostrophe27
ID: 19684122
The follow methods both give me the functionality I want:

//with child style set as child
      gchild = new CChildGPIODisplay(this);            
      gchild->Create(CChildGPIODisplay::IDD,this);      
      gchild->SetParent(NULL);
      gchild->ShowWindow(SW_SHOW);
      
//with child style set as child or popup      
      gchild = new CChildGPIODisplay(this);//CDialog*
      gchild->Create(CChildGPIODisplay::IDD,GetDesktopWindow( ));      
      gchild->SetParent(NULL);
      gchild->ShowWindow(SW_SHOW);

Unless I do gchild->SetParent(NULL); neither method works. Is there anything wrong with using one of these methods?
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 19684241
One shouldn't need to do such hacks, I still suspect you have an incorrect setting.
Have you checked the 'style' of this child dialog.  

Is the dialog set as a CHILD or a POPUP ?  I think it should be as a POPUP
0
 

Author Comment

by:apostrophe27
ID: 19684406
>>Is the dialog set as a CHILD or a POPUP ?  I think it should be as a POPUP

I've tried both CHILD and POPUP, and neither works except for with the code I posted above - one with child style set as child, the other with child style set as child or popup. I even created a new project with just a parent and child, and I get the same results.  

I'd prefer to do it the "correct" way, without hacks.
0
 

Accepted Solution

by:
apostrophe27 earned 0 total points
ID: 19685545
I asked the same question in the microsoft.public.vc.mfc newsgroup, and here's the answer I got

A child window can never go behind it's parent window.  The windows have to
be sibiling windows.

If the child window is modeless you can pass the desktop window as its
parent,GetDesktopWindow().

The other way is to have a hidden dialog box as your applications main
window, and then you would create your current main dialog box and the child
dialog with the hidden dialog as their parent.

0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 19689997
Interesting.  Glad you have it working.
0
 
LVL 1

Expert Comment

by:Vee_Mod
ID: 20327932
Closed, 500 points refunded.
Vee_Mod
Community Support Moderator
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

840 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