Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the size of a Windows Form

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;
like image 593
BigBug Avatar asked Oct 30 '11 02:10

BigBug


People also ask

How do I resize Winforms?

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.

What is form size?

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.

How do I create a fixed size form in Windows?

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.


3 Answers

For Getting the size of a Windows Form:

int formHeight = this.Height;
int formWidth = this.Width;
like image 134
T30 Avatar answered Oct 01 '22 05:10

T30


    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
}
like image 20
Damith Avatar answered Oct 01 '22 04:10

Damith


You want to set Dock = DockStyle.Fill to dock the control to fill its parent.

like image 31
SLaks Avatar answered Oct 01 '22 04:10

SLaks