11using System ;
2+ using System . ComponentModel ;
23using System . Drawing ;
4+ using System . Runtime . InteropServices ;
35using System . Windows ;
46using System . Windows . Forms ;
57using System . Windows . Interop ;
@@ -56,18 +58,17 @@ public unsafe static bool IsWindowFullscreen()
5658 }
5759
5860 string windowClass ;
59- int capacity = 256 ;
60- char [ ] buffer = new char [ capacity ] ;
61+ const int capacity = 256 ;
62+ Span < char > buffer = stackalloc char [ capacity ] ;
63+ int validLength ;
6164 fixed ( char * pBuffer = buffer )
6265 {
63- PInvoke . GetClassName ( hWnd , pBuffer , capacity ) ;
64-
65- // Truncate the buffer to the actual length of the string
66- int validLength = Array . IndexOf ( buffer , '\0 ' ) ;
67- if ( validLength < 0 ) validLength = capacity ;
68- windowClass = new string ( buffer , 0 , validLength ) ;
66+ validLength = PInvoke . GetClassName ( hWnd , pBuffer , capacity ) ;
6967 }
7068
69+ windowClass = buffer [ ..validLength ] . ToString ( ) ;
70+
71+
7172 //for Win+Tab (Flip3D)
7273 if ( windowClass == WINDOW_CLASS_WINTAB )
7374 {
@@ -87,14 +88,15 @@ public unsafe static bool IsWindowFullscreen()
8788 {
8889 var hWndDesktop = PInvoke . FindWindowEx ( hWnd , HWND . Null , "SHELLDLL_DefView" , null ) ;
8990 hWndDesktop = PInvoke . FindWindowEx ( hWndDesktop , HWND . Null , "SysListView32" , "FolderView" ) ;
90- if ( ! hWndDesktop . Equals ( IntPtr . Zero ) )
91+ if ( hWndDesktop . Value != ( IntPtr . Zero ) )
9192 {
9293 return false ;
9394 }
9495 }
9596
9697 Rectangle screenBounds = Screen . FromHandle ( hWnd ) . Bounds ;
97- return ( appBounds . bottom - appBounds . top ) == screenBounds . Height && ( appBounds . right - appBounds . left ) == screenBounds . Width ;
98+ return ( appBounds . bottom - appBounds . top ) == screenBounds . Height &&
99+ ( appBounds . right - appBounds . left ) == screenBounds . Width ;
98100 }
99101
100102 /// <summary>
@@ -104,7 +106,23 @@ public unsafe static bool IsWindowFullscreen()
104106 public static void DisableControlBox ( Window win )
105107 {
106108 var hwnd = new HWND ( new WindowInteropHelper ( win ) . Handle ) ;
107- PInvoke . SetWindowLong ( hwnd , WINDOW_LONG_PTR_INDEX . GWL_STYLE , PInvoke . GetWindowLong ( hwnd , WINDOW_LONG_PTR_INDEX . GWL_STYLE ) & ~ ( int ) WINDOW_STYLE . WS_SYSMENU ) ;
109+
110+ var style = PInvoke . GetWindowLong ( hwnd , WINDOW_LONG_PTR_INDEX . GWL_STYLE ) ;
111+
112+ if ( style == 0 )
113+ {
114+ throw new Win32Exception ( Marshal . GetLastPInvokeError ( ) ) ;
115+ }
116+
117+ style &= ~ ( int ) WINDOW_STYLE . WS_SYSMENU ;
118+
119+ var previousStyle = PInvoke . SetWindowLong ( hwnd , WINDOW_LONG_PTR_INDEX . GWL_STYLE ,
120+ style ) ;
121+
122+ if ( previousStyle == 0 )
123+ {
124+ throw new Win32Exception ( Marshal . GetLastPInvokeError ( ) ) ;
125+ }
108126 }
109127
110128 /// <summary>
@@ -127,6 +145,7 @@ public static Point TransformPixelsToDIP(Visual visual, double unitX, double uni
127145 using var src = new HwndSource ( new HwndSourceParameters ( ) ) ;
128146 matrix = src . CompositionTarget . TransformFromDevice ;
129147 }
148+
130149 return new Point ( ( int ) ( matrix . M11 * unitX ) , ( int ) ( matrix . M22 * unitY ) ) ;
131150 }
132151}
0 commit comments