WPF application in Dual monitor environment, application is not launching in proper screen.

Deepak Hirapur 1 Reputation point
2021-03-04T04:47:40.837+00:00

Hi all,

Our requirement is as below.
In dual monitors(screens) use case, our WPF application should launch in the monitor (screen) where the application was closed at the previous time.

Steps to execute the issue:

  1. Launch WPF application in Screen 1 with maximize state.
  2. Resize the application in Screen 1.
  3. Maximize and Close the application in Screen 1.
  4. Launch WPF application.

[Actual Result]
WPF application is launched in Screen 2.

[Expected Result]
WPF application is launched in Screen 1.
WPF application should be launched in the screen where it was closed at the previous time.

Note:
However, if WPF application is executed in Screen 2 with the above same steps, WPF application is launched in Screen 2.

Other standard applications i.e. Explorer, MS Paint etc.., behavior is same as our requirement.

We have tried below following approaches.

  1. Updating Window Left and Top properties in the MainWindow constructor, Which is retrieved from Config Xml file.
    this.Left = Config.Left;
    this.Top = Config.Right;

2) Retrieving Window Left & Top value from GetMousePosition () API method and updating in Window Loaded event method.
DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetCursorPos(ref Win32Point pt);

    [StructLayout(LayoutKind.Sequential)]
    internal struct Win32Point
    {
        public Int32 X;
        public Int32 Y;
    };

    public static Point GetMousePosition()
    {
        var w32Mouse = new Win32Point();
        GetCursorPos(ref w32Mouse);

        return new Point(w32Mouse.X, w32Mouse.Y);
    }

3) Retrieving Screen Bounds based on screen value & Updating Window Left and Top properties. Screen value is stored in Config xml file.
0=> Primary Screen, 1=>Secondary Screen.
System.Drawing.Rectangle screenBounds = System.Windows.Forms.Screen.AllScreens[].Bounds;
this.Left = screenBounds.Left;
this.Top = screenBounds.Top;

Please help us to resolve the issue to meet our requirement.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,708 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
789 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Will Baldwin 0 Reputation points
    2023-04-26T14:28:57.9766667+00:00
    using System.Runtime.InteropServices;
    [DllImport("user32.dll")]
        public static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, MonitorEnumDelegate lpfnEnum, IntPtr dwData);
    

    Call EnumDisplayMonitors(...) to enumerate monitors. The lpfnEnum parameter will end up being set to a pointer to a rectangle for each monitor that can be used to set the bounds of your window to the desired monitor.

    0 comments No comments