Search code examples
c#wpfwindowsize

C# WPF Window: Wrong Width and ActualWidth values


I just wrote a small program in C# that displays quotations on my desktop. My problem is to center the window of the application. To calculate the wanted location of the window i'm using this formula:

        Left = (Screen.PrimaryScreen.Bounds.Width - this.ActualWidth) / 2;
        Top = (Screen.PrimaryScreen.Bounds.Height - this.ActualHeight) / 2;

Unfortunately this.ActualWidth and this.ActualHeight give wrong values as you can see in the screenshot. 188,4 and 189 instead of 1000(+).

http://www.directupload.net/file/d/4044/4x2tgadg_png.htm

Here is the GUI:

http://www.directupload.net/file/d/4044/63rx2sul_png.htm

The code in text form:

public partial class QuotePresenter : Window
{
    public QuotePresenter()
    {
        InitializeComponent();
        setSize();
    }

    private void setSize()
    {
        MaxWidth = Screen.PrimaryScreen.Bounds.Width * 0.8;
        MaxHeight = Screen.PrimaryScreen.Bounds.Height * 0.8;
    }

    private void setLocation()
    {
        Left = (Screen.PrimaryScreen.Bounds.Width - this.ActualWidth) / 2;
        Top = (Screen.PrimaryScreen.Bounds.Height - this.ActualHeight) / 2;

        System.Windows.MessageBox.Show("Actual Width: " + this.ActualWidth + "\nWidth: " + this.Width);
    }

    public void SetQuote(Quotation quote)
    {
        Dispatcher.Invoke(new Action(() =>
        {
            tb_content.Text = quote.Content;
            tb_author.Text = "- " + quote.Author;

            setLocation();
        }));
    }
}

Can somebody tell me what went wrong here?

Thanks in advance!


Solution

  • I see two issues with your code. First of all, you're attempting to grab the new width of the window before it has resized itself to accommodate the updated text. In other words, you're getting the old width (before you inserted the quote) instead of the new width. To force the window to resize itself immediately, call UpdateLayout() prior to your setLocation() method.

    The second problem is that you've got a potential unit mismatch. WPF window sizes are specified in layout units equal to 1/96th of an inch (which may or may not be a pixel, depending on your DPI settings). But the desktop screen resolution is being queried in pixels. So depending on your user's display settings you might be mixing units and ending up with an incorrect location for your window.