Setting the Z-Order for forms

  • Thread starter Thread starter Jonathan Bindel
  • Start date Start date
J

Jonathan Bindel

I have an MDI app that contains several children and I have been trying
to find a way to display an MDI child form behind the current active
child. I don't want to have to call all of the other forms SendToBack
methods because I want the user to have a more seamless experience.
There must be a property or method in the Form class that will allow for
it to become shown behind the active one, but I have yet to find a good
way to do this. Does anyone know how?

Thanks,
Jonathan
 
Z order management is one of the crappiest part of win32....

Anyway you could try some unmanaged win32 function like:
SetWindowPos

tip: to get the HWND of a window: form.Handle
 
I just tryed ShowWindow at the suggestion of a friend with both
SHOWMINNOACTIVE and SHOWNA, and both ways, it didn't take the
WindowState I had given it beforehand. It still came up on top, just
not activated. You could move it around, but it never activated. I'll
try your suggestion using the SetWindowPos function, but I suspect it
will produce the same results.
 
It turns out that solved most of my problem, so thanks Lloyd. It now
works when the form is not maximized with the following code:

[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern bool SetWindowPos(
int hWnd, // window handle
int hWndInsertAfter, // placement-order handle
int X, // horizontal position
int Y, // vertical position
int cx, // width
int cy, // height
uint uFlags); // window positioning flags

const uint SWP_NOSIZE = 0x1;
const uint SWP_NOMOVE = 0x2;
const uint SWP_SHOWWINDOW = 0x40;
const uint SWP_NOACTIVATE = 0x10;

private static void ZOrder(Form form, Form insertAfter)
{
SetWindowPos((int)form.Handle,
(int)insertAfter.Handle,
0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE |
SWP_SHOWWINDOW | SWP_NOACTIVATE);
}

//Elsewhere
if(MainForm.MdiChildren.Length >= 2)
ZOrder(form, MainForm.MdiChildren[MainForm.MdiChildren.Length - 2]);


But what do I do about the maximized case? If you use this code, all of
the forms' states return to FormWindowState.Normal, rather than being
maximized behind the active maximized form.
 
may be you could mix SetWindowPos with a ShowWindow (maximize) ?

for the unactivated state, even when you click on it, it's puzzling...
it behave like your window is made the foremost one without being
activated...
(for some reason you can't activate on the formost window, you have to click
on another window first...)
 
Back
Top