【问题标题】:Getting the size of a Windows Form获取 Windows 窗体的大小
【发布时间】:2011-10-30 02:24:39
【问题描述】:

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

【问题讨论】:

    标签: c# winforms plot picturebox


    【解决方案1】:

    对于获取 Windows 窗体的大小

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

    【讨论】:

      【解决方案2】:
          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
      }
      

      【讨论】:

      • 嗯……我试过了,但是当表格被重新调整大小时它不会重新调整行的大小……
      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        将其设置为ClientRectangle.Width

        【讨论】:

        • 嗯,我在代码末尾添加了一个编辑 - 这是你的意思我应该尝试的吗?如果不是,请举个例子。
        • 你读过你刚刚写的吗? X 表示位置,而不是大小。
        猜你喜欢
        • 1970-01-01
        • 2010-11-12
        • 1970-01-01
        • 1970-01-01
        • 2011-12-19
        • 1970-01-01
        • 1970-01-01
        • 2015-10-16
        • 1970-01-01
        相关资源
        最近更新 更多