1
1
using System ;
2
+ using System . ComponentModel ;
2
3
using System . Drawing ;
4
+ using System . Runtime . InteropServices ;
3
5
using System . Windows ;
4
6
using System . Windows . Forms ;
5
7
using System . Windows . Interop ;
@@ -56,18 +58,17 @@ public unsafe static bool IsWindowFullscreen()
56
58
}
57
59
58
60
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 ;
61
64
fixed ( char * pBuffer = buffer )
62
65
{
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 ) ;
69
67
}
70
68
69
+ windowClass = buffer [ ..validLength ] . ToString ( ) ;
70
+
71
+
71
72
//for Win+Tab (Flip3D)
72
73
if ( windowClass == WINDOW_CLASS_WINTAB )
73
74
{
@@ -87,14 +88,15 @@ public unsafe static bool IsWindowFullscreen()
87
88
{
88
89
var hWndDesktop = PInvoke . FindWindowEx ( hWnd , HWND . Null , "SHELLDLL_DefView" , null ) ;
89
90
hWndDesktop = PInvoke . FindWindowEx ( hWndDesktop , HWND . Null , "SysListView32" , "FolderView" ) ;
90
- if ( ! hWndDesktop . Equals ( IntPtr . Zero ) )
91
+ if ( hWndDesktop . Value != ( IntPtr . Zero ) )
91
92
{
92
93
return false ;
93
94
}
94
95
}
95
96
96
97
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 ;
98
100
}
99
101
100
102
/// <summary>
@@ -104,7 +106,23 @@ public unsafe static bool IsWindowFullscreen()
104
106
public static void DisableControlBox ( Window win )
105
107
{
106
108
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
+ }
108
126
}
109
127
110
128
/// <summary>
@@ -127,6 +145,7 @@ public static Point TransformPixelsToDIP(Visual visual, double unitX, double uni
127
145
using var src = new HwndSource ( new HwndSourceParameters ( ) ) ;
128
146
matrix = src . CompositionTarget . TransformFromDevice ;
129
147
}
148
+
130
149
return new Point ( ( int ) ( matrix . M11 * unitX ) , ( int ) ( matrix . M22 * unitY ) ) ;
131
150
}
132
151
}
0 commit comments