1
- using SoftwareHelper . Views ;
1
+ using SoftwareHelper . ViewModels ;
2
+ using SoftwareHelper . Views ;
2
3
using System ;
3
4
using System . Drawing ;
4
5
using System . Runtime . InteropServices ;
5
6
using System . Text ;
7
+ using System . Threading ;
8
+ using System . Threading . Tasks ;
6
9
using System . Windows ;
7
10
using System . Windows . Input ;
8
11
using System . Windows . Interop ;
9
12
using System . Windows . Media ;
10
13
using System . Windows . Media . Animation ;
11
14
using System . Windows . Media . Imaging ;
15
+ using System . Windows . Threading ;
12
16
13
17
namespace SoftwareHelper . Helpers
14
18
{
@@ -183,6 +187,15 @@ internal struct RECT
183
187
#endregion
184
188
185
189
190
+ static readonly IntPtr GWL_EXSTYLE = new IntPtr ( - 20 ) ;
191
+ static readonly UInt32 WS_EX_TOPMOST = 0x0008 ;
192
+
193
+ [ DllImport ( "user32.dll" , SetLastError = true ) ]
194
+ static extern UInt32 GetWindowLong ( IntPtr hWnd , IntPtr nIndex ) ;
195
+ static bool IsTopMost ( IntPtr hwnd )
196
+ {
197
+ return ( GetWindowLong ( hwnd , GWL_EXSTYLE ) & ( int ) WS_EX_TOPMOST ) != 0 ;
198
+ }
186
199
187
200
[ DllImport ( "shell32.dll" ) ]
188
201
public static extern UInt32 SHAppBarMessage ( UInt32 dwMessage , ref APPBARDATA pData ) ;
@@ -220,6 +233,7 @@ public struct RECT1
220
233
public int bottom ;
221
234
}
222
235
private static EmbedDeasktopView _window ;
236
+ private static HwndSource mainWindowSrc ;
223
237
/// <summary>
224
238
/// 注册
225
239
/// </summary>
@@ -229,16 +243,26 @@ public static void RegisterDesktop(EmbedDeasktopView window = null)
229
243
if ( window != null )
230
244
_window = window ;
231
245
WindowInteropHelper helper = new WindowInteropHelper ( _window ) ;
232
- HwndSource mainWindowSrc = ( HwndSource ) HwndSource . FromHwnd ( helper . Handle ) ;
233
-
246
+ mainWindowSrc = ( HwndSource ) HwndSource . FromHwnd ( helper . Handle ) ;
247
+ if ( task != null )
248
+ {
249
+ cancelTokenSource . Cancel ( ) ;
250
+ task . Wait ( ) ;
251
+ task . Dispose ( ) ;
252
+ task = null ;
253
+ }
254
+ MoveWindowDPI ( ) ;
255
+ }
256
+ static void MoveWindowDPI ( bool isDpi = false )
257
+ {
234
258
APPBARDATA abd = new APPBARDATA ( ) ;
235
259
abd . cbSize = ( uint ) Marshal . SizeOf ( abd ) ;
236
260
abd . hWnd = mainWindowSrc . Handle ;
237
261
abd . uEdge = 2 ;
238
262
abd . rc . top = 0 ;
239
263
240
264
var source = PresentationSource . FromVisual ( _window ) ;
241
- var dpiX = source . CompositionTarget . TransformToDevice . M11 ;
265
+ var dpiX = source . CompositionTarget . TransformToDevice . M11 ;
242
266
var dpiY = source . CompositionTarget . TransformToDevice . M22 ;
243
267
244
268
_window . Width = 90 ;
@@ -256,33 +280,55 @@ public static void RegisterDesktop(EmbedDeasktopView window = null)
256
280
else
257
281
abd . rc . bottom = workingAreaSize . Height - ( desktopSize . Height - workingAreaSize . Height ) ;
258
282
abd . rc . left = abd . rc . right - ( int ) _window . ActualWidth ;
259
-
260
- //注册新的应用栏,并指定系统应用于向应用栏发送通知消息的消息标识符。
261
- SHAppBarMessage ( ( UInt32 ) AppBarMessages . New , ref abd ) ;
262
- //请求应用栏的大小和屏幕位置。
263
- SHAppBarMessage ( ( UInt32 ) AppBarMessages . QueryPos , ref abd ) ;
264
- //设置应用栏的大小和屏幕位置。
265
- SHAppBarMessage ( ( UInt32 ) AppBarMessages . SetPos , ref abd ) ;
266
- //设置应用所在平面位置。
283
+ if ( ! isDpi )
284
+ {
285
+ //注册新的应用栏,并指定系统应用于向应用栏发送通知消息的消息标识符。
286
+ SHAppBarMessage ( ( UInt32 ) AppBarMessages . New , ref abd ) ;
287
+ //请求应用栏的大小和屏幕位置。
288
+ SHAppBarMessage ( ( UInt32 ) AppBarMessages . QueryPos , ref abd ) ;
289
+ //设置应用栏的大小和屏幕位置。
290
+ SHAppBarMessage ( ( UInt32 ) AppBarMessages . SetPos , ref abd ) ;
291
+ //设置应用所在平面位置。
292
+ }
267
293
MoveWindow ( abd . hWnd , abd . rc . left , 0 , ( int ) _window . ActualWidth , abd . rc . bottom , 1 ) ;
268
294
}
295
+
296
+
297
+ static Task task ;
298
+ static CancellationTokenSource cancelTokenSource ;
269
299
/// <summary>
270
300
/// 卸载
271
301
/// </summary>
272
302
/// <param name="window"></param>
273
- public static void UnRegisterDesktop ( )
303
+ public static void UnRegisterDesktop ( bool isExit = false )
274
304
{
275
- WindowInteropHelper helper = new WindowInteropHelper ( _window ) ;
276
- HwndSource mainWindowSrc = ( HwndSource ) HwndSource . FromHwnd ( helper . Handle ) ;
277
-
305
+ if ( mainWindowSrc == null ) return ;
278
306
APPBARDATA abd = new APPBARDATA ( ) ;
279
307
abd . cbSize = ( uint ) Marshal . SizeOf ( abd ) ;
280
308
abd . hWnd = mainWindowSrc . Handle ;
281
309
282
310
SHAppBarMessage ( ( UInt32 ) AppBarMessages . Remove , ref abd ) ;
283
311
284
312
_window . ExitEmbedded ( ) ;
285
- _window . Topmost = true ;
313
+ if ( isExit ) return ;
314
+
315
+ cancelTokenSource = new CancellationTokenSource ( ) ;
316
+ task = new Task ( ( ) =>
317
+ {
318
+ while ( true )
319
+ {
320
+ if ( cancelTokenSource . IsCancellationRequested ) break ;
321
+ //if (IsTopMost(mainWindowSrc.Handle)) continue;
322
+ _window . Dispatcher . BeginInvoke ( new Action ( delegate
323
+ {
324
+ _window . Topmost = false ;
325
+ _window . Topmost = true ;
326
+ } ) ) ;
327
+ Thread . Sleep ( 1000 ) ;
328
+ }
329
+ } , cancelTokenSource . Token ) ;
330
+ task . Start ( ) ;
331
+
286
332
}
287
333
[ DllImport ( "gdi32.dll" ) ]
288
334
static extern int GetDeviceCaps (
0 commit comments