@@ -151,16 +151,17 @@ public static Point TransformPixelsToDIP(Visual visual, double unitX, double uni
151
151
152
152
#region Alt Tab
153
153
154
- private static IntPtr SetWindowLong ( HWND hWnd , WINDOW_LONG_PTR_INDEX nIndex , int dwNewLong )
154
+ private static int SetWindowLong ( HWND hWnd , WINDOW_LONG_PTR_INDEX nIndex , int dwNewLong )
155
155
{
156
156
PInvoke . SetLastError ( WIN32_ERROR . NO_ERROR ) ; // Clear any existing error
157
157
158
- return PInvoke . SetWindowLong ( hWnd , nIndex , dwNewLong ) ;
159
- }
158
+ var result = PInvoke . SetWindowLong ( hWnd , nIndex , dwNewLong ) ;
159
+ if ( result == 0 && Marshal . GetLastPInvokeError ( ) != 0 )
160
+ {
161
+ throw new Win32Exception ( Marshal . GetLastPInvokeError ( ) ) ;
162
+ }
160
163
161
- private static int IntPtrToInt32 ( IntPtr intPtr )
162
- {
163
- return unchecked ( ( int ) intPtr . ToInt64 ( ) ) ;
164
+ return result ;
164
165
}
165
166
166
167
/// <summary>
@@ -169,13 +170,12 @@ private static int IntPtrToInt32(IntPtr intPtr)
169
170
/// <param name="window">To hide a window</param>
170
171
public static void HideFromAltTab ( Window window )
171
172
{
172
- var helper = new WindowInteropHelper ( window ) ;
173
- var exStyle = PInvoke . GetWindowLong ( new ( helper . Handle ) , WINDOW_LONG_PTR_INDEX . GWL_EXSTYLE ) ;
173
+ var exStyle = GetCurrentWindowStyle ( window ) ;
174
174
175
175
// Add TOOLWINDOW style, remove APPWINDOW style
176
176
var newExStyle = ( ( uint ) exStyle | ( uint ) WINDOW_EX_STYLE . WS_EX_TOOLWINDOW ) & ~ ( uint ) WINDOW_EX_STYLE . WS_EX_APPWINDOW ;
177
177
178
- SetWindowLong ( new ( helper . Handle ) , WINDOW_LONG_PTR_INDEX . GWL_EXSTYLE , ( int ) newExStyle ) ;
178
+ SetWindowLong ( new ( new WindowInteropHelper ( window ) . Handle ) , WINDOW_LONG_PTR_INDEX . GWL_EXSTYLE , ( int ) newExStyle ) ;
179
179
}
180
180
181
181
/// <summary>
@@ -184,24 +184,27 @@ public static void HideFromAltTab(Window window)
184
184
/// <param name="window">To restore the displayed window</param>
185
185
public static void ShowInAltTab ( Window window )
186
186
{
187
- var helper = new WindowInteropHelper ( window ) ;
188
- var exStyle = PInvoke . GetWindowLong ( new ( helper . Handle ) , WINDOW_LONG_PTR_INDEX . GWL_EXSTYLE ) ;
187
+ var exStyle = GetCurrentWindowStyle ( window ) ;
189
188
190
189
// Remove the TOOLWINDOW style and add the APPWINDOW style.
191
190
var newExStyle = ( ( uint ) exStyle & ~ ( uint ) WINDOW_EX_STYLE . WS_EX_TOOLWINDOW ) | ( uint ) WINDOW_EX_STYLE . WS_EX_APPWINDOW ;
192
191
193
- SetWindowLong ( new ( helper . Handle ) , WINDOW_LONG_PTR_INDEX . GWL_EXSTYLE , ( int ) newExStyle ) ;
192
+ SetWindowLong ( new ( new WindowInteropHelper ( window ) . Handle ) , WINDOW_LONG_PTR_INDEX . GWL_EXSTYLE , ( int ) newExStyle ) ;
194
193
}
195
194
196
195
/// <summary>
197
196
/// To obtain the current overridden style of a window.
198
197
/// </summary>
199
198
/// <param name="window">To obtain the style dialog window</param>
200
199
/// <returns>current extension style value</returns>
201
- public static int GetCurrentWindowStyle ( Window window )
200
+ private static int GetCurrentWindowStyle ( Window window )
202
201
{
203
- var helper = new WindowInteropHelper ( window ) ;
204
- return PInvoke . GetWindowLong ( new ( helper . Handle ) , WINDOW_LONG_PTR_INDEX . GWL_EXSTYLE ) ;
202
+ var style = PInvoke . GetWindowLong ( new ( new WindowInteropHelper ( window ) . Handle ) , WINDOW_LONG_PTR_INDEX . GWL_EXSTYLE ) ;
203
+ if ( style == 0 && Marshal . GetLastPInvokeError ( ) != 0 )
204
+ {
205
+ throw new Win32Exception ( Marshal . GetLastPInvokeError ( ) ) ;
206
+ }
207
+ return style ;
205
208
}
206
209
207
210
#endregion
0 commit comments