获取 Windows 窗体的大小

发布于 2024-12-12 19:44:47 字数 1050 浏览 7 评论 0原文

我正在创建一个 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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

世界等同你 2024-12-19 19:44:48

对于获取 Windows 窗体的大小

int formHeight = this.Height;
int formWidth = this.Width;

For Getting the size of a Windows Form:

int formHeight = this.Height;
int formWidth = this.Width;
不再让梦枯萎 2024-12-19 19:44:48
    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
}
    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
}
守护在此方 2024-12-19 19:44:48

您想要设置Dock = DockStyle.Fill 来停靠控件以填充其父级。

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

以酷 2024-12-19 19:44:48

将其设置为ClientRectangle.Width

Set it to ClientRectangle.Width.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文