I'm creating a Windows Forms application. How do I capture the size of the windows form?
Currently I have something that looks like this in my code:
PictureBox display = new PictureBox();
display.Width = 360;
display.Height = 290;
this.Controls.Add(display);
display.Image = bmp;
However, the size of my display is hard-coded to a specific value.
I know that if I want to draw a square that re-sizes I can use something like this:
private Rectangle PlotArea;
private int offset = 30;
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
//// Calculate the location and size of the plot area
//// within which we want to draw the graphics:
Rectangle ChartArea = ClientRectangle;
PlotArea = new Rectangle(ChartArea.Location, ChartArea.Size);
PlotArea.Inflate(-offset, -offset);
Draw PlotArea:
g.DrawRectangle(Pens.Black, PlotArea);
}
Is there a way for me to use method one and grab the size of the form for Height and Width?
I've tried the following code, but it doesn't work...
PictureBox display = new PictureBox();
display.Width = ClientRectangle.Y;
display.Height = ClientRectangle.X;
this.Controls.Add(display);
display.Image = bmp;
By dragging either the right edge, bottom edge, or the corner, you can resize the form. The second way you can resize the form while the designer is open, is through the properties pane. Select the form, then find the Properties pane in Visual Studio. Scroll down to size and expand it.
The dimensions of a form depend on the size that is going to be used, and they measure 8.5 inches in width by 11 inches in height for US Letter Standards and 8.27 inches in width by 11.69 inches in height for A4 standard.
You can restrict the size of a form by setting it's MaximumSize and MinimumSize properties. This will help you control the maximum and minimum size the form can be resized to. Also note that WindowState property of the form plays a part in how the form can be resized.
For Getting the size of a Windows Form:
int formHeight = this.Height;
int formWidth = this.Width;
PictureBox display = new PictureBox();
display.Width = ClientRectangle.Width;
display.Height = ClientRectangle.Height;
this.Controls.Add(display);
display.Image = bmp;
When you resize the window:
private void Form1_Resize(object sender, System.EventArgs e)
{
Control control = (Control)sender;
// set the PictureBox width and heigh here using control.Size.Height
// and control.Size.Width
}
You want to set Dock = DockStyle.Fill
to dock the control to fill its parent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With