1- using SoftwareHelper . Views ;
1+ using SoftwareHelper . ViewModels ;
2+ using SoftwareHelper . Views ;
23using System ;
34using System . Drawing ;
45using System . Runtime . InteropServices ;
56using System . Text ;
7+ using System . Threading ;
8+ using System . Threading . Tasks ;
69using System . Windows ;
710using System . Windows . Input ;
811using System . Windows . Interop ;
912using System . Windows . Media ;
1013using System . Windows . Media . Animation ;
1114using System . Windows . Media . Imaging ;
15+ using System . Windows . Threading ;
1216
1317namespace SoftwareHelper . Helpers
1418{
@@ -183,6 +187,15 @@ internal struct RECT
183187 #endregion
184188
185189
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+ }
186199
187200 [ DllImport ( "shell32.dll" ) ]
188201 public static extern UInt32 SHAppBarMessage ( UInt32 dwMessage , ref APPBARDATA pData ) ;
@@ -220,6 +233,7 @@ public struct RECT1
220233 public int bottom ;
221234 }
222235 private static EmbedDeasktopView _window ;
236+ private static HwndSource mainWindowSrc ;
223237 /// <summary>
224238 /// 注册
225239 /// </summary>
@@ -229,16 +243,26 @@ public static void RegisterDesktop(EmbedDeasktopView window = null)
229243 if ( window != null )
230244 _window = window ;
231245 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+ {
234258 APPBARDATA abd = new APPBARDATA ( ) ;
235259 abd . cbSize = ( uint ) Marshal . SizeOf ( abd ) ;
236260 abd . hWnd = mainWindowSrc . Handle ;
237261 abd . uEdge = 2 ;
238262 abd . rc . top = 0 ;
239263
240264 var source = PresentationSource . FromVisual ( _window ) ;
241- var dpiX = source . CompositionTarget . TransformToDevice . M11 ;
265+ var dpiX = source . CompositionTarget . TransformToDevice . M11 ;
242266 var dpiY = source . CompositionTarget . TransformToDevice . M22 ;
243267
244268 _window . Width = 90 ;
@@ -256,33 +280,55 @@ public static void RegisterDesktop(EmbedDeasktopView window = null)
256280 else
257281 abd . rc . bottom = workingAreaSize . Height - ( desktopSize . Height - workingAreaSize . Height ) ;
258282 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+ }
267293 MoveWindow ( abd . hWnd , abd . rc . left , 0 , ( int ) _window . ActualWidth , abd . rc . bottom , 1 ) ;
268294 }
295+
296+
297+ static Task task ;
298+ static CancellationTokenSource cancelTokenSource ;
269299 /// <summary>
270300 /// 卸载
271301 /// </summary>
272302 /// <param name="window"></param>
273- public static void UnRegisterDesktop ( )
303+ public static void UnRegisterDesktop ( bool isExit = false )
274304 {
275- WindowInteropHelper helper = new WindowInteropHelper ( _window ) ;
276- HwndSource mainWindowSrc = ( HwndSource ) HwndSource . FromHwnd ( helper . Handle ) ;
277-
305+ if ( mainWindowSrc == null ) return ;
278306 APPBARDATA abd = new APPBARDATA ( ) ;
279307 abd . cbSize = ( uint ) Marshal . SizeOf ( abd ) ;
280308 abd . hWnd = mainWindowSrc . Handle ;
281309
282310 SHAppBarMessage ( ( UInt32 ) AppBarMessages . Remove , ref abd ) ;
283311
284312 _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+
286332 }
287333 [ DllImport ( "gdi32.dll" ) ]
288334 static extern int GetDeviceCaps (
0 commit comments