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:
- Launch WPF application in Screen 1 with maximize state.
- Resize the application in Screen 1.
- Maximize and Close the application in Screen 1.
- Launch WPF application.
WPF application is launched in Screen 2.
WPF application is launched in Screen 1.
WPF application should be launched in the screen where it was closed at the previous time.
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.
- 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.
internal static extern bool GetCursorPos(ref Win32Point pt);
internal struct Win32Point
public Int32 X;
public Int32 Y;
public static Point GetMousePosition()
var w32Mouse = new Win32Point();
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.