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!