C# - 最大化我的形状后,宽度和宽度高度值返回错误

时间:2015-07-10 16:52:56

标签: c#

我通过设置

使用全屏
this.WindowState = FormWindowState.Maximized;

执行后,我想获得表单的新宽度和高度,但是当我使用this.width& this.height,它只返回在最大化之前设置的值。

这是我的代码:

this.WindowState = FormWindowState.Maximized;
g.DrawImage(app.Properties.Resources.bg, 0, 0, Screen.GetWorkingArea(this).Width, Screen.GetWorkingArea(this).Height);

3 个答案:

答案 0 :(得分:1)

我无法重新创建您的问题。我刚刚使用Form(最初为300x300),Button和TextBox制作了一个快速程序。它按预期工作。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.WindowState = FormWindowState.Maximized;
        textBox1.Text = this.Width + " " + this.Height;
    }
}

我的textBox(textBox1)肯定不会显示“300 300”

修改:根据您更新的答案和下面的评论,试试这个,看看它是否按预期工作(也可以看看手动输入的宽度和高度是否有效?):

this.WindowState = FormWindowState.Maximized;
int width = Screen.GetWorkingArea(this).Width;
int height = Screen.GetWorkingArea(this).Height;
g.DrawImage(app.Properties.Resources.bg, 0, 0, width, height);

答案 1 :(得分:0)

当表单最大化时,从Screen.WorkingArea大小获取其宽度和高度。

不要使用:

Screen.GetWorkingArea(this)

但:

Screen.WorkingArea

答案 2 :(得分:0)

我有一个类似的问题,但仅适用于.net核心winforms。 尽管最大化了屏幕,但如果我使用this.width和this.height,它仍使用以前的值。

this.Width = Screen.GetWorkingArea(this).Width; this.Height = Screen.GetWorkingArea(this).Height; 修复了