Advertisement
TheFanatr

Proper C# Borderless Window Maximization

Mar 15th, 2017
4,368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.76 KB | None | 0 0
  1.         private static IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  2.         {
  3.             switch (msg)
  4.             {
  5.                 case 0x0024:
  6.                     WmGetMinMaxInfo(hwnd, lParam);
  7.                     handled = true;
  8.                     break;
  9.             }
  10.             return (IntPtr)0;
  11.         }
  12.  
  13.         private static void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam)
  14.         {
  15.             MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));
  16.             int MONITOR_DEFAULTTONEAREST = 0x00000002;
  17.             IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
  18.             if (monitor != IntPtr.Zero)
  19.             {
  20.                 MONITORINFO monitorInfo = new MONITORINFO();
  21.                 GetMonitorInfo(monitor, monitorInfo);
  22.                 RECT rcWorkArea = monitorInfo.rcWork;
  23.                 RECT rcMonitorArea = monitorInfo.rcMonitor;
  24.                 mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left);
  25.                 mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top);
  26.                 mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left);
  27.                 mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top);
  28.             }
  29.             Marshal.StructureToPtr(mmi, lParam, true);
  30.         }
  31.        
  32.         [StructLayout(LayoutKind.Sequential)]
  33.         public struct POINT
  34.         {
  35.             /// <summary>x coordinate of point.</summary>
  36.             public int x;
  37.             /// <summary>y coordinate of point.</summary>
  38.             public int y;
  39.             /// <summary>Construct a point of coordinates (x,y).</summary>
  40.             public POINT(int x, int y)
  41.             {
  42.                 this.x = x;
  43.                 this.y = y;
  44.             }
  45.         }
  46.  
  47.         [StructLayout(LayoutKind.Sequential)]
  48.         public struct MINMAXINFO
  49.         {
  50.             public POINT ptReserved;
  51.             public POINT ptMaxSize;
  52.             public POINT ptMaxPosition;
  53.             public POINT ptMinTrackSize;
  54.             public POINT ptMaxTrackSize;
  55.         };
  56.        
  57.         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  58.         public class MONITORINFO
  59.         {        
  60.             public int cbSize = Marshal.SizeOf(typeof(MONITORINFO));
  61.             public RECT rcMonitor = new RECT();
  62.             public RECT rcWork = new RECT();
  63.             public int dwFlags = 0;
  64.         }
  65.  
  66.         [StructLayout(LayoutKind.Sequential, Pack = 0)]
  67.         public struct RECT
  68.         {
  69.             public int left;
  70.             public int top;
  71.             public int right;
  72.             public int bottom;
  73.             public static readonly RECT Empty = new RECT();
  74.             public int Width { get { return Math.Abs(right - left); } }
  75.             public int Height { get { return bottom - top; } }
  76.             public RECT(int left, int top, int right, int bottom)
  77.             {
  78.                 this.left = left;
  79.                 this.top = top;
  80.                 this.right = right;
  81.                 this.bottom = bottom;
  82.             }
  83.             public RECT(RECT rcSrc)
  84.             {
  85.                 left = rcSrc.left;
  86.                 top = rcSrc.top;
  87.                 right = rcSrc.right;
  88.                 bottom = rcSrc.bottom;
  89.             }
  90.             public bool IsEmpty { get { return left >= right || top >= bottom; } }
  91.             public override string ToString()
  92.             {
  93.                 if (this == Empty) { return "RECT {Empty}"; }
  94.                 return "RECT { left : " + left + " / top : " + top + " / right : " + right + " / bottom : " + bottom + " }";
  95.             }
  96.             public override bool Equals(object obj)
  97.             {
  98.                 if (!(obj is Rect)) { return false; }
  99.                 return (this == (RECT)obj);
  100.             }
  101.             /// <summary>Return the HashCode for this struct (not garanteed to be unique)</summary>
  102.             public override int GetHashCode() => left.GetHashCode() + top.GetHashCode() + right.GetHashCode() + bottom.GetHashCode();
  103.             /// <summary> Determine if 2 RECT are equal (deep compare)</summary>
  104.             public static bool operator ==(RECT rect1, RECT rect2) { return (rect1.left == rect2.left && rect1.top == rect2.top && rect1.right == rect2.right && rect1.bottom == rect2.bottom); }
  105.             /// <summary> Determine if 2 RECT are different(deep compare)</summary>
  106.             public static bool operator != (RECT rect1, RECT rect2) { return !(rect1 == rect2); }
  107.         }
  108.  
  109.         [DllImport("user32")]
  110.         internal static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);
  111.  
  112.         [DllImport("User32")]
  113.         internal static extern IntPtr MonitorFromWindow(IntPtr handle, int flags);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement