Thomas Kjørnes
Thomas Kjørnes

Reputation: 1927

How do I determine an MDI child form's screen location?

I have a main form 'MainForm' with IsMdiContainer = true

I have one or more dynamically created child forms where I set MdiParent = MainForm

Now what I want to do is to be able to detach these child forms by setting MdiParent = null, but with maintaining the exact same screen location.

I've tried to use ChildForm.PointToScreen(ChildForm.Location), but that gives me the screen location relative to the client area of the form.

EDIT

PointToScreen() on the form itself gives me almost the correct location, except that it gives the screen location of 0,0 inside the form, while .Location refers to the outer edge of the form.

Upvotes: 0

Views: 4707

Answers (1)

Hans Passant
Hans Passant

Reputation: 942099

You have to use the parent's mdi client window's PointToScreen() method:

    private void button1_Click(object sender, EventArgs e) {
        if (this.MdiParent != null) {
            MdiClient client = null;
            foreach (Control ctl in this.MdiParent.Controls) {
                if (ctl is MdiClient) { client = ctl as MdiClient; break; }
            }
            this.WindowState = FormWindowState.Normal;
            Point loc = client.PointToScreen(this.Location);
            this.MdiParent = null;
            this.Location = loc;
        }
    }

You cannot avoid the slight offset you get on Aero, nor the flicker.

Upvotes: 3

Related Questions