简体   繁体   中英

C# - How to set up a small Mdi “area” for MdiChildren?

I have a Mdi parent form and some Mdi child forms. It works good so far but I want to define a smaller area on the parent form where the Mdi children can be moved around. It looks like the property MdiParent is of type Form . Now I wonder how can I make the children move inside a specific area but not inside the whole parent window.

class MdiParentForm: Form
{
    public MdiParentForm()
    {
        this.IsMdiContainer = true;
        Form form = new Form();
        this.AddOwnedForm(form);
        form.MdiParent = this;
        form.Show();
    }
}

You can just set the Padding of your Mdi form. The padding is the distance between the control (as a container) and its child controls. There are 4 sides: left, top, right, bottom. This code just makes all the sides the same:

Padding = new Padding(50);

You can notice that the BackColor around the MdiClient is not affected. To affect the BackColor around the MdiClient , we have to override the OnPaint so that the default behavior is not processed:

BackColor = Color.Green;//try setting the BackColor of the Mdi form to Color.Green
protected override void OnPaint(PaintEventArgs e){
   RaisePaintEvent(this, e); //remove the base.OnPaint(e)
}

If you want to take full control over the MdiClient , just declare a variable to hold the MdiClient :

MdiClient client = Controls.OfType<MdiClient>().First();

Then you can use its Properties and methods like as you do on a form, such as client.Dock = DockStyle.Left , client.Width = 400; , ...

在此输入图像描述

It is automatic when you Dock other controls to the edges of the MDI parent form. The dark-gray MDI client window shrinks to fit the remaining space. Using the Dock property is the essential part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM