1
1
using System ;
2
2
using System . Drawing ;
3
- using System . Runtime . InteropServices ;
4
- using System . Text ;
5
3
using System . Windows ;
6
4
using System . Windows . Forms ;
7
5
using System . Windows . Interop ;
8
6
using System . Windows . Media ;
7
+ using Windows . Win32 ;
8
+ using Windows . Win32 . Foundation ;
9
+ using Windows . Win32 . UI . WindowsAndMessaging ;
9
10
using Point = System . Windows . Point ;
10
11
11
12
namespace Flow . Launcher . Helper ;
12
13
13
14
public class WindowsInteropHelper
14
15
{
15
- private const int GWL_STYLE = - 16 ; //WPF's Message code for Title Bar's Style
16
- private const int WS_SYSMENU = 0x80000 ; //WPF's Message code for System Menu
17
- private static IntPtr _hwnd_shell ;
18
- private static IntPtr _hwnd_desktop ;
16
+ private static HWND _hwnd_shell ;
17
+ private static HWND _hwnd_desktop ;
19
18
20
19
//Accessors for shell and desktop handlers
21
20
//Will set the variables once and then will return them
22
- private static IntPtr HWND_SHELL
21
+ private static HWND HWND_SHELL
23
22
{
24
23
get
25
24
{
26
- return _hwnd_shell != IntPtr . Zero ? _hwnd_shell : _hwnd_shell = GetShellWindow ( ) ;
25
+ return _hwnd_shell != HWND . Null ? _hwnd_shell : _hwnd_shell = PInvoke . GetShellWindow ( ) ;
27
26
}
28
27
}
29
- private static IntPtr HWND_DESKTOP
28
+
29
+ private static HWND HWND_DESKTOP
30
30
{
31
31
get
32
32
{
33
- return _hwnd_desktop != IntPtr . Zero ? _hwnd_desktop : _hwnd_desktop = GetDesktopWindow ( ) ;
33
+ return _hwnd_desktop != HWND . Null ? _hwnd_desktop : _hwnd_desktop = PInvoke . GetDesktopWindow ( ) ;
34
34
}
35
35
}
36
36
37
- [ DllImport ( "user32.dll" , SetLastError = true ) ]
38
- internal static extern int GetWindowLong ( IntPtr hWnd , int nIndex ) ;
39
-
40
- [ DllImport ( "user32.dll" ) ]
41
- internal static extern int SetWindowLong ( IntPtr hWnd , int nIndex , int dwNewLong ) ;
42
-
43
- [ DllImport ( "user32.dll" ) ]
44
- internal static extern IntPtr GetForegroundWindow ( ) ;
45
-
46
- [ DllImport ( "user32.dll" ) ]
47
- internal static extern IntPtr GetDesktopWindow ( ) ;
48
-
49
- [ DllImport ( "user32.dll" ) ]
50
- internal static extern IntPtr GetShellWindow ( ) ;
51
-
52
- [ DllImport ( "user32.dll" , SetLastError = true ) ]
53
- internal static extern int GetWindowRect ( IntPtr hwnd , out RECT rc ) ;
54
-
55
- [ DllImport ( "user32.dll" , SetLastError = true , CharSet = CharSet . Auto ) ]
56
- internal static extern int GetClassName ( IntPtr hWnd , StringBuilder lpClassName , int nMaxCount ) ;
57
-
58
- [ DllImport ( "user32.DLL" ) ]
59
- public static extern IntPtr FindWindowEx ( IntPtr hwndParent , IntPtr hwndChildAfter , string lpszClass , string lpszWindow ) ;
60
-
61
-
62
37
const string WINDOW_CLASS_CONSOLE = "ConsoleWindowClass" ;
63
38
const string WINDOW_CLASS_WINTAB = "Flip3D" ;
64
39
const string WINDOW_CLASS_PROGMAN = "Progman" ;
65
40
const string WINDOW_CLASS_WORKERW = "WorkerW" ;
66
41
67
- public static bool IsWindowFullscreen ( )
42
+ public unsafe static bool IsWindowFullscreen ( )
68
43
{
69
44
//get current active window
70
- IntPtr hWnd = GetForegroundWindow ( ) ;
45
+ var hWnd = PInvoke . GetForegroundWindow ( ) ;
71
46
72
- if ( hWnd . Equals ( IntPtr . Zero ) )
47
+ if ( hWnd . Equals ( HWND . Null ) )
73
48
{
74
49
return false ;
75
50
}
@@ -80,38 +55,44 @@ public static bool IsWindowFullscreen()
80
55
return false ;
81
56
}
82
57
83
- StringBuilder sb = new StringBuilder ( 256 ) ;
84
- GetClassName ( hWnd , sb , sb . Capacity ) ;
85
- string windowClass = sb . ToString ( ) ;
58
+ string windowClass ;
59
+ int capacity = 256 ;
60
+ char [ ] buffer = new char [ capacity ] ;
61
+ fixed ( char * pBuffer = buffer )
62
+ {
63
+ PInvoke . GetClassName ( hWnd , pBuffer , capacity ) ;
64
+ int validLength = Array . IndexOf ( buffer , '\0 ' ) ;
65
+ if ( validLength < 0 ) validLength = capacity ;
66
+ windowClass = new string ( buffer , 0 , validLength ) ;
67
+ }
86
68
87
69
//for Win+Tab (Flip3D)
88
70
if ( windowClass == WINDOW_CLASS_WINTAB )
89
71
{
90
72
return false ;
91
73
}
92
74
93
- RECT appBounds ;
94
- GetWindowRect ( hWnd , out appBounds ) ;
75
+ PInvoke . GetWindowRect ( hWnd , out var appBounds ) ;
95
76
96
77
//for console (ConsoleWindowClass), we have to check for negative dimensions
97
78
if ( windowClass == WINDOW_CLASS_CONSOLE )
98
79
{
99
- return appBounds . Top < 0 && appBounds . Bottom < 0 ;
80
+ return appBounds . top < 0 && appBounds . bottom < 0 ;
100
81
}
101
82
102
83
//for desktop (Progman or WorkerW, depends on the system), we have to check
103
84
if ( windowClass is WINDOW_CLASS_PROGMAN or WINDOW_CLASS_WORKERW )
104
85
{
105
- IntPtr hWndDesktop = FindWindowEx ( hWnd , IntPtr . Zero , "SHELLDLL_DefView" , null ) ;
106
- hWndDesktop = FindWindowEx ( hWndDesktop , IntPtr . Zero , "SysListView32" , "FolderView" ) ;
86
+ var hWndDesktop = PInvoke . FindWindowEx ( hWnd , HWND . Null , "SHELLDLL_DefView" , null ) ;
87
+ hWndDesktop = PInvoke . FindWindowEx ( hWndDesktop , HWND . Null , "SysListView32" , "FolderView" ) ;
107
88
if ( ! hWndDesktop . Equals ( IntPtr . Zero ) )
108
89
{
109
90
return false ;
110
91
}
111
92
}
112
93
113
94
Rectangle screenBounds = Screen . FromHandle ( hWnd ) . Bounds ;
114
- return ( appBounds . Bottom - appBounds . Top ) == screenBounds . Height && ( appBounds . Right - appBounds . Left ) == screenBounds . Width ;
95
+ return ( appBounds . bottom - appBounds . top ) == screenBounds . Height && ( appBounds . right - appBounds . left ) == screenBounds . Width ;
115
96
}
116
97
117
98
/// <summary>
@@ -120,8 +101,8 @@ public static bool IsWindowFullscreen()
120
101
/// </summary>
121
102
public static void DisableControlBox ( Window win )
122
103
{
123
- var hwnd = new WindowInteropHelper ( win ) . Handle ;
124
- SetWindowLong ( hwnd , GWL_STYLE , GetWindowLong ( hwnd , GWL_STYLE ) & ~ WS_SYSMENU ) ;
104
+ var hwnd = new HWND ( new WindowInteropHelper ( win ) . Handle ) ;
105
+ PInvoke . SetWindowLong ( hwnd , WINDOW_LONG_PTR_INDEX . GWL_STYLE , PInvoke . GetWindowLong ( hwnd , WINDOW_LONG_PTR_INDEX . GWL_STYLE ) & ~ ( int ) WINDOW_STYLE . WS_SYSMENU ) ;
125
106
}
126
107
127
108
/// <summary>
@@ -146,14 +127,4 @@ public static Point TransformPixelsToDIP(Visual visual, double unitX, double uni
146
127
}
147
128
return new Point ( ( int ) ( matrix . M11 * unitX ) , ( int ) ( matrix . M22 * unitY ) ) ;
148
129
}
149
-
150
-
151
- [ StructLayout ( LayoutKind . Sequential ) ]
152
- public struct RECT
153
- {
154
- public int Left ;
155
- public int Top ;
156
- public int Right ;
157
- public int Bottom ;
158
- }
159
130
}
0 commit comments