2015-07-10 22 views
1

我使用全屏設置C# - 最大限度地提高我的表格後,寬度和高度值返回錯誤

this.WindowState = FormWindowState.Maximized; 

執行它之後,我想形式的新的寬度和高度,但當我使用this.width這個高度時,它只是返回在最大化之前設置的值

這裏是我的代碼:

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

可能需要[ActualWidth的(https://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.actualwidth(v = vs.110).aspx)和[ActualHeight](https://msdn.microsof t.com/zh-cn/library/system.windows.frameworkelement.actualheight(v=vs.110).aspx) – Grundy

+0

我沒有看到你在哪裏使用this.Width和this.Height。你在哪裏調用這個代碼?你的表單是否有邊框?發佈重複問題的代碼。 – LarsTech

+0

該代碼是Graffitos回答後編輯的代碼。之前我通常用這個來試試.Width – Joey

回答

0

當窗體最大化,從Screen.WorkingArea大小獲取它的寬度和高度。

不要使用:

Screen.GetWorkingArea(this) 

但:

Screen.WorkingArea 
+0

這還是不行,同樣的問題 – Joey

+0

WinForms應用程序? – Graffito

+0

是的,這是一個WinForms – Joey

0

我無法重現您的問題。我只是用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); 
+0

現在很奇怪.. – Joey

+0

您是否確定在最大化之前沒有獲取高度和寬度?或者事先儲存它們,忘記重新設置它們? –

+0

是的,我100%確定,這是絕對不可能的 – Joey

相關問題