Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 422
Zakk Avatar asked Aug 16 '18 14:08

Zakk


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

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.

like image 87
dontbyteme Avatar answered Oct 19 '22 16:10

dontbyteme


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.

like image 42
Or Yaacov Avatar answered Oct 19 '22 16:10

Or Yaacov