Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Programmatically adjust MainWindow size/position?

$
0
0

I'm trying a simple experiment (programmatically adjust the MainWindow's size and position) but the results are not what I expected. A bunch of searching online has not turned up anything useful. Here's what I've done:

1) Create a new WPF application.

2) In the MainWindow XAML file add:  WindowState="Maximized"

3) Drop 2 buttons onto the MainWindow, call the first "Store" and the second "Restore" and add their click event handlers.

4) In the "Store" button event handler, save the MainWindow size and location to a global System.Windows.Rect struct called "MainWindowRect":

private void StoreButton_Click(object sender, RoutedEventArgs e)
{
    MainWindowRect = new Rect(new Point(this.Left, this.Top), new Size(this.ActualWidth, this.ActualHeight));
}

5) In the "Restore" button handler, attempt to reposition and size the MainWindow:

private void RestoreButton_Click(object sender, RoutedEventArgs e)
{
    this.Width = MainWindowRect.Width;
    this.Height = MainWindowHeight.Height;
    this.Left = MainWindowRect.Left;
    this.Top = MainWindowRect.Top;
}

If I run this simple test and store the maximized MainWindow, then manually size the MainWindow to something smaller and shifted, then attempt to restore it via the code above, the MainWindow is sized slightly too large and shifted slightly up and to the left of its maximized location (the one that was stored). Upon storing, the maximized MainWindow reports the following values:

Left = -8.0
Top = -8.0
ActualWidth = 1936.0
ActualHeight = 1096.0

These values are odd since the monitor is 96 dpi and 1080p. Does anyone know what's going on here? Thanks in advance.

-L




Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>