获取Windows窗体的大小
我正在创建一个Windows窗体应用程序。 如何捕获窗体的大小?
目前我的代码中有一些看起来像这样的东西:
PictureBox display = new PictureBox(); display.Width = 360; display.Height = 290; this.Controls.Add(display); display.Image = bmp;
但是,我的显示器的大小硬编码为特定值。
我知道如果我想绘制一个重新调整尺寸的正方形,我可以使用这样的东西:
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); }
有没有办法让我使用方法一并获取高度和宽度的表单大小?
我尝试了以下代码,但它不起作用……
PictureBox display = new PictureBox(); display.Width = ClientRectangle.Y; display.Height = ClientRectangle.X; this.Controls.Add(display); display.Image = bmp;
PictureBox display = new PictureBox(); display.Width = ClientRectangle.Width; display.Height = ClientRectangle.Height; this.Controls.Add(display); display.Image = bmp;
调整窗口大小时:
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 }
获取Windows窗体的大小 :
int formHeight = this.Height; int formWidth = this.Width;
您希望将Dock = DockStyle.Fill
为停靠控件以填充其父级。
将其设置为ClientRectangle.Width
。