Zakk
Zakk

Reputation: 768

C# WPF fit application depending on monitor size after maximize/minimize after moving between monitors

I have two monitors:

Screen 1: which is secondary screen with 1920x1080

Screen 2: which is primary screen with 1600x900

Screen 1 is larger then Screen 2.

When I open my application in Screen 2, and then move it from screen 2 to screen 1 and try to minimize and then maximize my application, the maximum size is taken by screen 2, and not by current monitor size(it doesn't appear maximized related to monitor size)

How I can edit my code so in maximize and minimize to take the screen resolution where the application exist now instead of taking it depending on the primary monitor?

I am using the code in this thread for the matter of resizing: https://blogs.msdn.microsoft.com/llobo/2006/08/01/maximizing-window-with-windowstylenone-considering-taskbar/

Which is same as this reply: https://stackoverflow.com/a/6315427/5825468

Thanks

Upvotes: 12

Views: 2867

Answers (2)

dontbyteme
dontbyteme

Reputation: 1261

To get the size of the current screen you probably have to use the windows forms method FromHandle like

using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;

namespace TestApp
{
    public partial class MainWindow : Window
    {           
        public Screen GetCurrentScreen(Window window)
        {
            return Screen.FromHandle(new WindowInteropHelper(window).Handle);
        }

        public MainWindow()
        {
            InitializeComponent();

            var screen = GetCurrentScreen(this);
            var height = screen.Bounds.Height;
            var width = screen.Bounds.Width;

            // ...
        }     
    }
}

Another option would be to subscribe to the LocationChanged event of the window to know when your application window has been moved to a secondary screen, see this answer of StepUp.

Upvotes: 4

Or Yaacov
Or Yaacov

Reputation: 3880

first subscribe to the state changed event:

public MainWindow()
{
   InitializeComponent();
   this.StateChanged += MainWindow_StateChanged;
}

and then only after minimize, restore the virtual/primary screen sizes as follow: and since I don't know what is your default app size and if it's not a full screen, at the end you can restore them easily by the SystemParameters screen size, with a little logic.

private void MainWindow_StateChanged(object sender, EventArgs e)
{
   if (this.WindowState != WindowState.Minimized)
   {
                        this.Width = SystemParameters.PrimaryScreenWidth;
                        this.Height = SystemParameters.PrimaryScreenHeight;
                        //this.Width = SystemParameters.VirtualScreenWidth;
                        //this.Height =SystemParameters.VirtualScreenHeight;
                        //this.Width = SystemParameters.WorkArea.Width                            
                        //this.Height =SystemParameters.WorkArea.Height;
                     }
   }

that should handle the situation, the SystemParameters expose the following data:

PrimeryScreenHeight/Width:

The height/Width of the screen.

VirtualScreenWidth/Height:

The width/height of the virtual screen.

WorkArea:

A RECT structure that receives the work area coordinates, expressed as virtual screen coordinates.

Upvotes: 3

Related Questions