traversing children forms mdi according to the current z-order

0

I have an MDI application where I have a parent Form and several open child forms as shown in the image.

the opening sequence of the children forms was: form1 form2 form3 form4

Then I change the order of the forms interposing them in different ways, but when I scroll through the children forms showing a message for each iteration, they do not show me the current order as they are in the image, the following order is always shown as they were created Initially:

form1 form2 form3 form4

instead of that, what I want is to be shown in the following order as the image:

form1 form3 form2 form4

some idea of how to get it

my code is on the button of the child form:

private void button1_Click(object sender, EventArgs e)
{
    for (int x = 0; x <= MdiParent.MdiChildren.Count() - 1; x++) {
        MessageBox.Show(MdiParent.MdiChildren[x].Text);
    }
}

Thank you very much!

    
asked by Manuel Paz 07.04.2018 в 08:04
source

1 answer

0

Well I found this solution, it works well to the exposed

[DllImport("user32.dll")]
        private static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);

        const uint GW_HWNDFIRST = 0;
        const uint GW_HWNDLAST = 1;
        const uint GW_HWNDNEXT = 2;
        const uint GW_HWNDPREV = 3;



        private void button1_Click(object sender, EventArgs e)
        {
            Form fr;
            var hwndTmp = GetWindow(Handle, GW_HWNDFIRST);

            while (hwndTmp != IntPtr.Zero)
            {
                if (Handle == hwndTmp)
                {
                    fr = (Form)Form.FromHandle(hwndTmp);
                    MessageBox.Show(fr.Text);
                }

                hwndTmp = GetWindow(hwndTmp, GW_HWNDNEXT);
                if (hwndTmp != (IntPtr)0)
                {
                    fr = (Form)Form.FromHandle(hwndTmp);
                    MessageBox.Show(fr.Text);
                }

            }

        }
    
answered by 11.04.2018 в 01:38