12

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

Zakk
  • 758
  • 3
  • 11
  • 20
  • can you show the code you're using to resize? – Jeff R. Aug 16 '18 at 15:42
  • Hi @JeffR. I added the code to the post, thanks – Zakk Aug 16 '18 at 18:44
  • No need for any code, just use Windows key plus arrow shortcuts. Win+left or right to cycle around certain positions across the multiple monitors (try it, you'll see) and Win+up to maximise. – Richardissimo Aug 16 '18 at 19:46
  • @Richardissimo when I delete that code when I maximize my application it go over the taskbar. My application is legacy project, but I have updated its .NET versoin.. – Zakk Aug 17 '18 at 17:50
  • Did you take a look at https://social.msdn.microsoft.com/Forums/vstudio/en-US/a78b04aa-40c8-4f87-8469-e786f8ecf17c/screen-resolution-issue-on-wpf-windowless-application?forum=wpf – Dipen Shah Aug 20 '18 at 21:11
  • Give a try to this, i'm not sure: Window window = new Window(); window.WindowStartupLocation = WindowStartupLocation.CenterScreen; window.SourceInitialized += (s, a) => window.WindowState = WindowState.Maximized; window.Show(); – Marco Salerno Aug 24 '18 at 10:34
  • @Zakk Both monitor DPI is different? if yes you need to first find DPI then according to DPI adjust width,height and top,left – Ankur Tripathi Aug 24 '18 at 11:06
  • DPI is same, resolution is different @AnkurTripathi – Zakk Aug 24 '18 at 20:59

2 Answers2

4

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.

dontbyteme
  • 1,221
  • 1
  • 11
  • 23
  • FromHandle option is only relevant to get the size of the screen that the application exists in. My problem is after moving the application from one monitor to another and trying to maximize. Second option only works if we have only 2 monitors – Zakk Aug 22 '18 at 10:55
  • @Zakk have a look at [this CodeProject article](https://www.codeproject.com/Tips/1184124/Get-Target-Screen-Size-of-the-Hosting-Window-in-WP) (if it does not help check out [this](http://www.codewrecks.com/blog/index.php/2013/01/05/open-a-window-in-fullscreen-on-a-specific-monitor-in-wpf/) and [this](https://blog.onedevjob.com/2010/10/19/fixing-full-screen-wpf-windows/)) – dontbyteme Aug 22 '18 at 11:33
  • @Zakk did you find a solution with the linked CodeProject article? – dontbyteme Aug 24 '18 at 07:58
  • Thanks alot!! the project from codeProject worked. The solution is to add OnLocationChangedEvent and to get screen sizes in it like in the project. Thanks alot for your help. – Zakk Sep 02 '18 at 08:51
3

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.

Or Yaacov
  • 3,597
  • 5
  • 25
  • 49
  • Will this SystemParameter give me the correct size once I move the application to the second monitor and try to maximize it?(which is the size of the second monitor) – Zakk Aug 18 '18 at 20:41
  • I just edited my answer to give you some more details, Since I'm not sure what's plug to what, one of those should do the trick, it will be helpful if you can debug your code and post here the information. – Or Yaacov Aug 18 '18 at 21:10
  • Primary and Workarea return the primary screen size. VirtualScreen return the width and height of all screens combined – Zakk Aug 19 '18 at 07:52
  • If you have only 2 monitors then your length will be the virtual minus the primary. You shold give it a try just to see the if it solve your minimize and maximize problem, until ill edit the comment with a multi screen solution – Or Yaacov Aug 19 '18 at 07:58
  • I must create solution that works for all of the cases, not only 2 monitors. I know with 2 monitors I can make VirtualScreen - PrimaryScreen to get the second one.. – Zakk Aug 19 '18 at 08:00
  • Yes I know, I suggested it as a test only. – Or Yaacov Aug 19 '18 at 08:09
  • hi @Or Yaacov. The solution above worked for me, thanks for help anyways :) – Zakk Sep 02 '18 at 08:51