[英]Getting the size of a Windows Form
我正在創建一個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;
獲取Windows窗體的大小 :
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;
調整窗口大小時:
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
}
您希望將Dock = DockStyle.Fill
為停靠控件以填充其父級。
將其設置為ClientRectangle.Width
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.