Advertisement
TheFanatr

Proper C# Borderless Window Maximization v1.2

Mar 9th, 2018
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.23 KB | None | 0 0
  1. CompositionTarget WindowCompositionTarget { get; set; }
  2.  
  3. double CachedMinWidth { get; set; }
  4.  
  5. double CachedMinHeight { get; set; }
  6.  
  7. POINT CachedMinTrackSize { get; set; }
  8.  
  9. IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  10. {
  11.     switch (msg)
  12.     {
  13.         case 0x0024:
  14.             MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));
  15.             IntPtr monitor = MonitorFromWindow(hwnd, 0x00000002 /*MONITOR_DEFAULTTONEAREST*/);
  16.             if (monitor != IntPtr.Zero)
  17.             {
  18.                 MONITORINFO monitorInfo = new MONITORINFO { };
  19.                 GetMonitorInfo(monitor, monitorInfo);
  20.                 RECT rcWorkArea = monitorInfo.rcWork;
  21.                 RECT rcMonitorArea = monitorInfo.rcMonitor;
  22.                 mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left);
  23.                 mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top);
  24.                 mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left);
  25.                 mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top);
  26.                 if (!CachedMinTrackSize.Equals(mmi.ptMinTrackSize) || CachedMinHeight != MinHeight && CachedMinWidth != MinWidth)
  27.                 {
  28.                     mmi.ptMinTrackSize.x = (int)((CachedMinWidth = MinWidth) * WindowCompositionTarget.TransformToDevice.M11);
  29.                     mmi.ptMinTrackSize.y = (int)((CachedMinHeight = MinHeight) * WindowCompositionTarget.TransformToDevice.M22);
  30.                     CachedMinTrackSize = mmi.ptMinTrackSize;
  31.                 }
  32.             }
  33.             Marshal.StructureToPtr(mmi, lParam, true);
  34.             handled = true;
  35.             break;
  36.     }
  37.     return IntPtr.Zero;
  38. }
  39.  
  40. [StructLayout(LayoutKind.Sequential)]
  41. public struct POINT
  42. {
  43.     public int x;
  44.     public int y;
  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. }
  74.  
  75. [DllImport("user32")]
  76. internal static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);
  77.  
  78. [DllImport("User32")]
  79. internal static extern IntPtr MonitorFromWindow(IntPtr handle, int flags);
  80.  
  81. public MainWindow()
  82. {
  83.     InitializeComponent();
  84.     MinimizeButton.Click += (s, e) => WindowState = WindowState.Minimized;
  85.     MaximizeButton.Click += (s, e) => WindowState = WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
  86.     CloseButton.Click += (s, e) => Close();
  87.     SourceInitialized += (s, e) =>
  88.     {
  89.         WindowCompositionTarget = PresentationSource.FromVisual(this).CompositionTarget;
  90.         HwndSource.FromHwnd(new WindowInteropHelper(this).Handle).AddHook(WindowProc);
  91.     };
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement