Created
November 22, 2019 05:13
-
-
Save Ganeshcse/58b79acc74c852b3c3629debb231db04 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam) | |
{ | |
MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO)); | |
int MONITOR_DEFAULTTONEAREST = 0x00000002; | |
IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); | |
if (monitor != IntPtr.Zero) | |
{ | |
MONITORINFO monitorInfo = new MONITORINFO(); | |
GetMonitorInfo(monitor, monitorInfo); | |
RECT rcWorkArea = monitorInfo.rcWork; | |
RECT rcMonitorArea = monitorInfo.rcMonitor; | |
mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left); | |
mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top); | |
mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left); | |
mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top); | |
} | |
Marshal.StructureToPtr(mmi, lParam, true); | |
} | |
// Below is my MINMAXINFO struct | |
[StructLayout(LayoutKind.Sequential)] | |
public struct MINMAXINFO | |
{ | |
public POINT ptReserved; | |
public POINT ptMaxSize; | |
public POINT ptMaxPosition; | |
public POINT ptMinTrackSize; | |
public POINT ptMaxTrackSize; | |
}; | |
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] | |
public class MONITORINFO | |
{ | |
public int cbSize = Marshal.SizeOf(typeof(MONITORINFO)); | |
public RECT rcMonitor = new RECT(); | |
public RECT rcWork = new RECT(); | |
public int dwFlags = 0; | |
} | |
Hi all, | |
The above piece of code I am using in my application to get the monitor information. But now I wanted to extend the feature to save the state of the Monitor information into Properties settings. | |
And then read the values from Properties settings next time I relaunch the application and launch the application on the same monitor. | |
For this I have followed many articles (mentioned below), | |
https://www.codeproject.com/Articles/50761/Save-and-Restore-WPF-Window-Size-Position-and-or-S | |
https://stackoverflow.com/questions/847752/net-wpf-remember-window-size-between-sessions | |
https://stackoverflow.com/questions/2846662/saving-wpf-window-and-position | |
Based on the articles I made some changes to my above shown method "WmGetMinMaxInfo". Pl see below my code after changes, | |
private static void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam) | |
{ | |
MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO)); | |
SetupMoniter.Load(); | |
int MONITOR_DEFAULTTONEAREST = 0x00000002; | |
IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); | |
if (monitor != IntPtr.Zero) | |
{ | |
if (SetupMoniter.WindowLeft < 0) | |
SetupMoniter.WindowLeft = 0; | |
if (SetupMoniter.WindowTop < 0) | |
SetupMoniter.WindowTop = 0; | |
var screenInfo = ScreenFromPoint1(new Point(Convert.ToInt32(SetupMoniter.WindowLeft), Convert.ToInt32(SetupMoniter.WindowTop))); | |
MONITORINFO monitorInfo = new MONITORINFO(); | |
GetMonitorInfo(monitor, monitorInfo); | |
RECT rcWorkArea = monitorInfo.rcWork; | |
RECT rcMonitorArea = monitorInfo.rcMonitor; | |
mmi.ptMaxPosition.x = Math.Abs(screenInfo.WorkingArea.Left - screenInfo.Bounds.Left); //screenInfo.Bounds.X;//Math.Abs(rcWorkArea.left - rcMonitorArea.left); | |
mmi.ptMaxPosition.y = Math.Abs(screenInfo.WorkingArea.Top - screenInfo.Bounds.Top); //screenInfo.Bounds.Y;//Math.Abs(rcWorkArea.top - rcMonitorArea.top); | |
mmi.ptMaxSize.x = Math.Abs(screenInfo.WorkingArea.Right - screenInfo.WorkingArea.Left); //Math.Abs(rcWorkArea.right - rcWorkArea.left); | |
mmi.ptMaxSize.y = Math.Abs(screenInfo.WorkingArea.Bottom - screenInfo.WorkingArea.Top); | |
} | |
Marshal.StructureToPtr(mmi, lParam, true); | |
} | |
static System.Windows.Forms.Screen ScreenFromPoint1(Point p) | |
{ | |
System.Drawing.Point pt = new System.Drawing.Point((int)p.X, (int)p.Y); | |
return System.Windows.Forms.Screen.AllScreens.Where(scr => scr.Bounds.Contains(pt)).FirstOrDefault(); | |
} | |
What changes I have made to the code - | |
> I am getting Screen instance using the saved Left and Top values. See method ScreenFromPoint1() | |
> Then I am using the screen instance and assigning ptMaxPosition.X, ptMaxPosition.Y, ptMaxSize.X and ptMaxSize.Y with screenInfo instance. | |
> I have made changes to those above lines by looking at old code i.e. old code using workArea.Left, so I am replacing with screenInfo.WorkingArea.Left. | |
> Similarly for others as well. | |
What problems I am facing now - | |
> I have two monitors with 1920x1080 (primary) and 1920x1200 (secondary) resolutions. | |
> First time when my application is launched it opens on primary monitor without any cuts for the height of the window. | |
> Now I am dragging to secondary monitor then it does not maximize to full working area of my secondary monitor. | |
> Now my application is on secondary monitor - I am trying to maximize it, it maximizes but it does not occupy complete space of the secondary monitor | |
- Probably I am guessing I need to save the co-ordinates on maximize as well. | |
> Now I am trying to restore and then maximize on secondary monitor - all works fine but still above point issue is still exists. | |
> Now again I dragged the application onto primary monitor and then did a restore - maximize operations - it worked fine. | |
> I have finally closed my application on secondary monitor which is having 1920x1200 resolution. | |
Relaunch - | |
> Now if I am tring to relaunch the application - it opens on primary monitor, but it should have opened on secondary monitor. | |
> Secondary there is a cut when application is on primary monitor. When I dragged to secondary monitor then also there is a cut to the bottom of the application. | |
> So now I am suspecting that above solution was not the right approach for the fix. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment