18

How do you change controls' Z-order in MFC at design time - i.e. I can't use SetWindowPos or do this at runtime - I want to see the changed z-order in the designer (even if I have to resort to direct-editing the .rc code).

I have an MFC dialog to which I am adding controls. If there is overlap between the edges of the controls, I want to bring one to the front of the other. In Windows Forms or WPF, etc. I can Bring to Front, Send to Back, Bring Forward, Send Back. I don't find these options in MFC, nor can I tell how it determines what is in front, as a control just added is often behind a control that was there previously. How can I manipulate the Z-order in MFC? Even if I have to manipulate the .rc file code directly (i.e. end-run around the designer).

Aardvark
  • 8,474
  • 7
  • 46
  • 64
Tim Erickson
  • 2,323
  • 2
  • 22
  • 27

7 Answers7

20

In Visual Studio 6.0 do the following.

Open the dialog screen (in designer view)

Press Ctrl + D

The tab orders will be shown for each control

Start clicking controls in the tab order you expect to see in run-time (ie., the control on which you click first will have tab order set to 1 and so on...)

Senthil
  • 367
  • 2
  • 9
  • 2
    The question regards visual z-index, *not* tab index. – Tim Erickson Jul 09 '13 at 16:21
  • 1
    @TimErickson thanks for pointing out, I had the same thought when answering this question 4 years ago :) But the question was how to do Z Order **at design time**, hence I shared the approach I was using in my MFC Dialog-based app at that time. – Senthil Jul 12 '13 at 13:49
  • 6
    @Tim Erickson: in Visual Studio tab index *is* z-index. – Antony Hatchkins Sep 23 '14 at 04:57
19

I think the control in front will be the last control that occurs in the rc file. In other words, the dialog editor will draw each control as it is encountered from top to bottom in the rc file, overlapping them when necessary.

You can edit the rc file to reorder them, or you can change the tab order in the editor, which does the same thing since tab order is also set based on the order that the controls occur in the file. To my knowledge MFC doesn't offer any other way of layering overlapping controls at design time.

ryan_s
  • 7,826
  • 3
  • 27
  • 28
11
GetDlgItem(IDC_MYCONTROL)->SetWindowPos(HWND_TOP,
                                        0, 0, 0, 0,
                                        SWP_NOMOVE | SWP_NOSIZE);
Adam Pierce
  • 33,531
  • 22
  • 69
  • 89
  • 2
    The question specifies "at design time" *not* at runtime. – Tim Erickson Jul 09 '13 at 16:22
  • (EDIT: MFC-Edit-Browse-Control is my solution),,,,,Thanks, gona have to manually fight this battle. Draw+Click+focus events force their own order and I cant rely on tab order alone. EX: Button ontop a CEdit. The button needs to be Before edit box, or else clicks goto the edit. However, clicking into Edit will cause it to draw ontop of button no matter the order. – diox8tony Feb 02 '18 at 00:41
9

Actually, if you want to do this in the resource editor, you can just cut the item and then paste it back as a quick and dirty solution. Just Ctrl-X then Ctrl-V.

Editing the RC file will also work.

Adam Pierce
  • 33,531
  • 22
  • 69
  • 89
  • 1
    this saved the day. i had an unused textbox that i wanted to remove hidden somewhere and couldn't locate it in designer view for like **half an hour**. so i selected it from the properties, clicked on designer view, `ctrl-x`, `ctrl-v`, (some cursing), `del`. – Sharky Sep 19 '14 at 12:06
1

You can use CWnd::SetWindowPos() to control the Z order of your controls, without changing their position in the parent window.

Franci Penov
  • 74,861
  • 18
  • 132
  • 169
1

In the MSVC 2005 dialog resource editor there is an option to set the tab order. In MSVC 2005 it is found on the Format, Tab Order menu.

The tab order displayed by this menu option is the same order in which the controls are written to the resource file.

jussij
  • 10,370
  • 1
  • 33
  • 49
1
GetDlgItem(IDC_CONTROL1)->SetWindowPos(&wndTop, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE)
linquize
  • 19,828
  • 10
  • 59
  • 83