Does the size of a Windows Form include its Border

2019-02-19 15:39发布

问题:

When you set the size of a windows form, ie;

Form1.Size = new System.Drawing.Size(700, 500);

Does this include the border which windows puts around the form? Because I have added images (via pictureBoxes) which are 700x500 to my form, and they have been cut off by the border.

Also: When I say the border, I mean the default windows border which you can drag the edges of to resize it, as well as contain the red X, Minimize, and Maximize buttons.

回答1:

That depends, you'll get a different size when you target .NET 4.5 for example. The border is always included but you don't really know how much of the border is included. An issue with the fat borders you get with Aero and the skinny ones you get in XP.

It is almost always the wrong thing to do. You always want to assign the ClientSize property instead. Important, it doesn't include the borders so you can be sure that controls still fit.

And hard-coding the size is always wrong as well. Your form will be rescaled, depending on the video adapter's dots-per-inch setting. The larger the DPI setting, the bigger the form needs to be to still fit its content. So the correct statement ought to resemble this:

  this.ClientSize = new Size(PictureBox3.Right, PictureBox3.Bottom);

On the assumption that "PictureBox3" is the control in the far right-bottom corner that you want to keep visible. This statement needs to appear in the Load event handler to ensure that rescaling was done, it can't work in the constructor. One of the very few good reasons to use the Load event.