Solved

SetWindowPos confusion

Posted on 2006-10-27
9
1,145 Views
Last Modified: 2013-11-20
Ah hello

I have obtained the following code for creating a sub dialog on a main dialog.  Basically, I have a combo box on the main dialog that determines which child dialog to display on the main dialog.  The child dialog is created and positioned to be within an area defined by a picture control on the main dialog.  Have a look at this:

            pNewDialog->Create( this );

            CRect rc;
            m_wndSubDialogArea.GetWindowRect(rc);
            ScreenToClient(rc);
            pNewDialog->SetWindowPos(&m_wndSubDialogArea, rc.left, rc.top, rc.Width(), rc.Height(), SWP_NOACTIVATE | SWP_NOSENDCHANGING);      

            pNewDialog->ShowWindow(SW_SHOW);

Assume that pNewDialog is a CDialog derived class, and m_wndSubDialogArea is the picture control where the sub dialog sits.

OK then.  Having looked at this code, I am not sure on what the first parameter of SetWindowPos is doing exactly.  I have read the docs, and it talks about Z order.  Is this the Z order that is iterated through when we press Alt-Tab ?

Now then.  I changed this parameter from my picture control to

&wndTopMost

and instead of the child dialog being fit nicely into my picture control, it appeared with its top left corner in the main dialog's top left corner.

?!?

I experimented with other values: wndBottom works fine, wndTop works but I can still see the outline of my picture control (black), and wndNoTopMost does the same as wndTopMost.

Can someone please explain these results ?

I need to understand this as I have a second question which can be answered when this first one has.  

75pts/question

TIA
0
Comment
Question by:mrwad99
  • 4
  • 3
  • 2
9 Comments
 
LVL 22

Accepted Solution

by:
mahesh1402 earned 150 total points
ID: 17817847
>>Is this the Z order that is iterated through when we press Alt-Tab ?

No its not. Z-Order controls how windows are drawn on top of one another.....one over other.. top most is top always on all...


>>instead of the child dialog being fit nicely into my picture control, it appeared with its top left corner in the main dialog's top left corner

Have you checked with other flags like SWP_NOMOVE which retains the current position (ignores X and Y parameters)... first move window to desired x, y positions and then invoke

  pNewDialog->SetWindowPos(&CWnd::wndTopMost, rc.left, rc.top, rc.Width(), rc.Height(), SWP_NOACTIVATE | SWP_NOSENDCHANGING|SWP_NOMOVE);    

-MAHESH
0
 
LVL 19

Author Comment

by:mrwad99
ID: 17818101
Thanks for that.

I tried the code suggestion with using the SWP_NOMOVE flag and it still has the same effect.  I then tried

            pNewDialog->MoveWindow ( rc.left, rc.top, rc.Width(), rc.Height() );
            pNewDialog->SetWindowPos(&CWnd::wndNoTopMost, -1, -1, -1, -1, SWP_NOACTIVATE | SWP_NOSENDCHANGING | SWP_NOMOVE | SWP_NOSIZE );    
            pNewDialog->ShowWindow(SW_SHOW);

and it does work.  So why does SetWindowPos on its own not work ?

Where I use m_wndSubDialogArea as the first parameter to SetWindowPos, I take it that means that the child dialog is one above the main dialog, i.e. it that is why it appears on top ?

My second question is related to showing and hiding the child dialogs.  When I create a child dialog, and then I create another (when the user selects a different element in my combo box ) I don't destroy the first one, I simply hide it.  Then, if I have not already created the new dialog to be displayed, I create it, otherwise I just show it (as I will have previously hidden it).  I found that just hiding and showing the dialogs resulted in still seeing the hidden dialog behind the one that is currently visible.  Say I hide a dialog that contains an edit control, then show one that contains a combo in the same place as the edit control.  When I click the combo, I suddenly see the edit control and focus gets set to it, rendering my combo unusable.  I then changed the Z order of the dialog that I hide to be &CWnd::wndBottom, and this fixed the issue.

Is this the best way of solving this, or can you see a better way ?

Thanks again.
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17818120
>>So why does SetWindowPos on its own not work ?


SetWindowPos() is used to set  SIZE ,POSITION and Z-Order of a window...

if you use SWP_NOSIZE ...current size will keep unchanged...

if you use SWP_NOMOVE ...current positions will keep unchanged...

if you use both SWP_NOSIZE | SWP_NOMOVE position and size both will keep unchaged but then to work this you have to manually first set window size and positions..... after setting window to desired size and position you are calling SetWindowPos here just to change Z-Order because you have changed size and position with MoveWindow().. call to SetWindowPos with SWP_NOMOVE | SWP_NOSIZE will keep your size and positions intact but &CWnd::wndNoTopMost flag will set Z-Order.

2nd Q:  When you create 2nd dialog runtime then z-order of windows changes.. and you need to manually set top and bottom window order manually.

-MAHESH
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 19

Author Comment

by:mrwad99
ID: 17818223
Thanks.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 17818382
IMHO contrary to the first comment from Mahesh the alt-Tab does work through the Z-order of the controls on the dialog, when you create/position a window on a dialog with other controls you can use the first parameter to control where the window is in the internal tab order.
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17818521
ALT+TAB : well as per wordings yes.. But for explanations purpose I said Z-Order as top - bottom..one over another.. just to understand it better.

-MAHESH
0
 
LVL 19

Author Comment

by:mrwad99
ID: 17818550
>> the alt-Tab does work through the Z-order of the controls on the dialog

Thanks Andy.

When I press Alt-Tab I get a window with all the currently running apps displayed as icons.  This dialog app I talked about in this question appears there, but the child dialog does not.  I geuss if I created the child dialog as a separate dialog it would appear in the currently running apps list; or is that not what you meant ?
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17818582
ALT+TAB : list appears main process NOT child dialogs.

-MAHESH
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 17818629
My fault - I meant TAB not Alt-TAB to cycle through sibling controls which is based on their Z-ordering.
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to create frequencies of a variable from SAS dataset? 10 125
Hibernate methods 2 72
wait notify demo infinite loop 3 110
Window placement 17 83
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

803 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