From 79b730d878abe199407bb415c9dd570d566cdc91 Mon Sep 17 00:00:00 2001 From: "yanjinhuamail@gmail.com" Date: Thu, 23 Jun 2022 16:59:15 +0800 Subject: [PATCH] update in issues. --- SoftwareHelper/App.xaml.cs | 6 +- SoftwareHelper/Helpers/Win32Api.cs | 80 +++++++++++++++---- SoftwareHelper/Properties/AssemblyInfo.cs | 4 +- SoftwareHelper/SoftwareHelper.csproj | 3 + SoftwareHelper/ViewModels/MainVM.cs | 34 ++++---- .../Views/EmbedDeasktopView.xaml.cs | 22 ++--- SoftwareHelper/Views/WindowColor.xaml | 32 +++++--- SoftwareHelper/Views/WindowColor.xaml.cs | 12 ++- SoftwareHelper/app.manifest | 7 +- 9 files changed, 136 insertions(+), 64 deletions(-) diff --git a/SoftwareHelper/App.xaml.cs b/SoftwareHelper/App.xaml.cs index 9f431d5..24d0d41 100644 --- a/SoftwareHelper/App.xaml.cs +++ b/SoftwareHelper/App.xaml.cs @@ -71,7 +71,11 @@ protected override void OnStartup(StartupEventArgs e) AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; } - + protected override void OnExit(ExitEventArgs e) + { + base.OnExit(e); + Win32Api.UnRegisterDesktop(true); + } private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) { try diff --git a/SoftwareHelper/Helpers/Win32Api.cs b/SoftwareHelper/Helpers/Win32Api.cs index c78dfd5..39eb1ef 100644 --- a/SoftwareHelper/Helpers/Win32Api.cs +++ b/SoftwareHelper/Helpers/Win32Api.cs @@ -1,14 +1,18 @@ -using SoftwareHelper.Views; +using SoftwareHelper.ViewModels; +using SoftwareHelper.Views; using System; using System.Drawing; using System.Runtime.InteropServices; using System.Text; +using System.Threading; +using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; +using System.Windows.Threading; namespace SoftwareHelper.Helpers { @@ -183,6 +187,15 @@ internal struct RECT #endregion + static readonly IntPtr GWL_EXSTYLE = new IntPtr(-20); + static readonly UInt32 WS_EX_TOPMOST = 0x0008; + + [DllImport("user32.dll", SetLastError = true)] + static extern UInt32 GetWindowLong(IntPtr hWnd, IntPtr nIndex); + static bool IsTopMost(IntPtr hwnd) + { + return (GetWindowLong(hwnd, GWL_EXSTYLE) & (int)WS_EX_TOPMOST) != 0; + } [DllImport("shell32.dll")] public static extern UInt32 SHAppBarMessage(UInt32 dwMessage, ref APPBARDATA pData); @@ -220,6 +233,7 @@ public struct RECT1 public int bottom; } private static EmbedDeasktopView _window; + private static HwndSource mainWindowSrc; /// /// 注册 /// @@ -229,8 +243,18 @@ public static void RegisterDesktop(EmbedDeasktopView window = null) if (window != null) _window = window; WindowInteropHelper helper = new WindowInteropHelper(_window); - HwndSource mainWindowSrc = (HwndSource)HwndSource.FromHwnd(helper.Handle); - + mainWindowSrc = (HwndSource)HwndSource.FromHwnd(helper.Handle); + if (task != null) + { + cancelTokenSource.Cancel(); + task.Wait(); + task.Dispose(); + task = null; + } + MoveWindowDPI(); + } + static void MoveWindowDPI(bool isDpi = false) + { APPBARDATA abd = new APPBARDATA(); abd.cbSize = (uint)Marshal.SizeOf(abd); abd.hWnd = mainWindowSrc.Handle; @@ -238,7 +262,7 @@ public static void RegisterDesktop(EmbedDeasktopView window = null) abd.rc.top = 0; var source = PresentationSource.FromVisual(_window); - var dpiX = source.CompositionTarget.TransformToDevice.M11; + var dpiX = source.CompositionTarget.TransformToDevice.M11; var dpiY = source.CompositionTarget.TransformToDevice.M22; _window.Width = 90; @@ -256,25 +280,29 @@ public static void RegisterDesktop(EmbedDeasktopView window = null) else abd.rc.bottom = workingAreaSize.Height - (desktopSize.Height - workingAreaSize.Height); abd.rc.left = abd.rc.right - (int)_window.ActualWidth; - - //注册新的应用栏,并指定系统应用于向应用栏发送通知消息的消息标识符。 - SHAppBarMessage((UInt32)AppBarMessages.New, ref abd); - //请求应用栏的大小和屏幕位置。 - SHAppBarMessage((UInt32)AppBarMessages.QueryPos, ref abd); - //设置应用栏的大小和屏幕位置。 - SHAppBarMessage((UInt32)AppBarMessages.SetPos, ref abd); - //设置应用所在平面位置。 + if (!isDpi) + { + //注册新的应用栏,并指定系统应用于向应用栏发送通知消息的消息标识符。 + SHAppBarMessage((UInt32)AppBarMessages.New, ref abd); + //请求应用栏的大小和屏幕位置。 + SHAppBarMessage((UInt32)AppBarMessages.QueryPos, ref abd); + //设置应用栏的大小和屏幕位置。 + SHAppBarMessage((UInt32)AppBarMessages.SetPos, ref abd); + //设置应用所在平面位置。 + } MoveWindow(abd.hWnd, abd.rc.left, 0, (int)_window.ActualWidth, abd.rc.bottom, 1); } + + + static Task task; + static CancellationTokenSource cancelTokenSource; /// /// 卸载 /// /// - public static void UnRegisterDesktop() + public static void UnRegisterDesktop(bool isExit = false) { - WindowInteropHelper helper = new WindowInteropHelper(_window); - HwndSource mainWindowSrc = (HwndSource)HwndSource.FromHwnd(helper.Handle); - + if (mainWindowSrc == null) return; APPBARDATA abd = new APPBARDATA(); abd.cbSize = (uint)Marshal.SizeOf(abd); abd.hWnd = mainWindowSrc.Handle; @@ -282,7 +310,25 @@ public static void UnRegisterDesktop() SHAppBarMessage((UInt32)AppBarMessages.Remove, ref abd); _window.ExitEmbedded(); - _window.Topmost = true; + if (isExit) return; + + cancelTokenSource = new CancellationTokenSource(); + task = new Task(() => + { + while (true) + { + if (cancelTokenSource.IsCancellationRequested) break; + //if (IsTopMost(mainWindowSrc.Handle)) continue; + _window.Dispatcher.BeginInvoke(new Action(delegate + { + _window.Topmost = false; + _window.Topmost = true; + })); + Thread.Sleep(1000); + } + }, cancelTokenSource.Token); + task.Start(); + } [DllImport("gdi32.dll")] static extern int GetDeviceCaps( diff --git a/SoftwareHelper/Properties/AssemblyInfo.cs b/SoftwareHelper/Properties/AssemblyInfo.cs index b858d5e..67d16f4 100644 --- a/SoftwareHelper/Properties/AssemblyInfo.cs +++ b/SoftwareHelper/Properties/AssemblyInfo.cs @@ -51,5 +51,5 @@ // 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, // 方法是按如下所示使用“*”: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("10.4.3.0")] -[assembly: AssemblyFileVersion("10.4.3.0")] +[assembly: AssemblyVersion("0.4.3.0")] +[assembly: AssemblyFileVersion("0.4.3.0")] diff --git a/SoftwareHelper/SoftwareHelper.csproj b/SoftwareHelper/SoftwareHelper.csproj index 58284d7..c11adda 100644 --- a/SoftwareHelper/SoftwareHelper.csproj +++ b/SoftwareHelper/SoftwareHelper.csproj @@ -63,6 +63,9 @@ SoftwareHelper.App + + app.manifest + False diff --git a/SoftwareHelper/ViewModels/MainVM.cs b/SoftwareHelper/ViewModels/MainVM.cs index a64334a..aef68cd 100644 --- a/SoftwareHelper/ViewModels/MainVM.cs +++ b/SoftwareHelper/ViewModels/MainVM.cs @@ -301,7 +301,7 @@ private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e) public ICommand ScreenCutCommand => new RelayCommand(obj => { IsOpenContextMenu = false; - Thread.Sleep(1000); + Keyboard.ClearFocus(); var screenCut = new WPFDevelopers.Controls.ScreenCut(); screenCut.ShowDialog(); @@ -309,32 +309,38 @@ private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e) #endregion #region 方法 - + private Color color; + private string hex; private void Timer_Tick(object sender, EventArgs e) { var point = new MousePoint.POINT(); var isMouseDown = MousePoint.GetCursorPos(out point); - var color = Win32Api.GetPixelColor(point.X, point.Y); - - if (point.X >= desktopWorkingArea.Width) - colorView.Left = point.X - 40; + color = Win32Api.GetPixelColor(point.X, point.Y); + var source = PresentationSource.FromVisual(colorView); + var dpiX = source.CompositionTarget.TransformToDevice.M11; + var dpiY = source.CompositionTarget.TransformToDevice.M22; + if (point.X / dpiX >= desktopWorkingArea.Width) + colorView.Left = point.X / dpiX - 40; else if (point.X <= 10) - colorView.Left = point.X; + colorView.Left = point.X / dpiX; else - colorView.Left = point.X; - - if (point.Y >= desktopWorkingArea.Height - 40) - colorView.Top = point.Y - 40; + colorView.Left = point.X / dpiX; + if (point.Y / dpiY >= desktopWorkingArea.Height - 40) + colorView.Top = point.Y / dpiY - 40; else - colorView.Top = point.Y; + colorView.Top = point.Y / dpiY; colorView.MouseColor = new SolidColorBrush(color); + hex = color.ToString(); + if (hex.Length > 7) + hex = hex.Remove(1, 2); + colorView.MouseColorText = hex; } private void MouseHook_MouseDown(object sender, MouseEventArgs e) { - Clipboard.SetText(colorView.MouseColor.ToString()); - ShowBalloon("提示", $"已复制到剪切板 {colorView.MouseColor.ToString()} SoftwareHelper"); + Clipboard.SetText(hex); + ShowBalloon("提示", $"已复制到剪切板 {hex} SoftwareHelper"); if (_timer.IsEnabled) { _timer.Stop(); diff --git a/SoftwareHelper/Views/EmbedDeasktopView.xaml.cs b/SoftwareHelper/Views/EmbedDeasktopView.xaml.cs index a65b8cb..e8ebaaa 100644 --- a/SoftwareHelper/Views/EmbedDeasktopView.xaml.cs +++ b/SoftwareHelper/Views/EmbedDeasktopView.xaml.cs @@ -50,8 +50,13 @@ private void OnHookKeyDown(object sender, HookEventArgs e) SetKeyDown(e.Key); if (IsKeyDown(Key.PrintScreen)) { - var screenCut = new ScreenCut(); - screenCut.ShowDialog(); + var screenCut = new ScreenCut() {Topmost = true}; + screenCut.Activate(); + screenCut.Closing += delegate + { + SetKeyUp(Key.PrintScreen); + }; + screenCut.ShowDialog(); } else { @@ -112,16 +117,6 @@ private void IMEUS() Win32Api.LoadKeyboardLayout("0000409", Win32Api.KLF_ACTIVATE)); } - private void EmbedDeasktopView_KeyUp(object sender, KeyEventArgs e) - { - Thread.Sleep(300); - KeyDownPanel.Visibility = Visibility.Collapsed; - } - - private void EmbedDeasktopView_KeyDown(object sender, KeyEventArgs e) - { - } - private void EmbedDeasktopView_Loaded(object sender, RoutedEventArgs e) { #region 注释 @@ -133,10 +128,9 @@ private void EmbedDeasktopView_Loaded(object sender, RoutedEventArgs e) #endregion } - private void EmbedDeasktopView_Closing(object sender, CancelEventArgs e) { - Win32Api.UnRegisterDesktop(); + Win32Api.UnRegisterDesktop(true); } protected override void OnSourceInitialized(EventArgs e) diff --git a/SoftwareHelper/Views/WindowColor.xaml b/SoftwareHelper/Views/WindowColor.xaml index 6a5379f..2d5d387 100644 --- a/SoftwareHelper/Views/WindowColor.xaml +++ b/SoftwareHelper/Views/WindowColor.xaml @@ -7,23 +7,31 @@ xmlns:shell="https://github.com/WPFDevelopersOrg.WPFDevelopers.Minimal" mc:Ignorable="d" Title="WindowColor" - UseLayoutRounding="True" ResizeMode="NoResize" Height="80" Width="80" + UseLayoutRounding="True" ResizeMode="NoResize" Height="60" + SizeToContent="Width" SnapsToDevicePixels="True" WindowStyle="None" Background="Transparent" Topmost="True" ShowInTaskbar="False" > - - - + + + + diff --git a/SoftwareHelper/Views/WindowColor.xaml.cs b/SoftwareHelper/Views/WindowColor.xaml.cs index e4e50db..827b458 100644 --- a/SoftwareHelper/Views/WindowColor.xaml.cs +++ b/SoftwareHelper/Views/WindowColor.xaml.cs @@ -15,11 +15,21 @@ public Brush MouseColor set { SetValue(MouseColorProperty, value); } } - // Using a DependencyProperty as the backing store for MouseColor. This enables animation, styling, binding, etc... public static readonly DependencyProperty MouseColorProperty = DependencyProperty.Register("MouseColor", typeof(Brush), typeof(WindowColor), new PropertyMetadata(null)); + + public string MouseColorText + { + get { return (string)GetValue(MouseColorTextProperty); } + set { SetValue(MouseColorTextProperty, value); } + } + + public static readonly DependencyProperty MouseColorTextProperty = + DependencyProperty.Register("MouseColorText", typeof(string), typeof(WindowColor), new PropertyMetadata(null)); + + public WindowColor() { InitializeComponent(); diff --git a/SoftwareHelper/app.manifest b/SoftwareHelper/app.manifest index f85e57d..de8d4a6 100644 --- a/SoftwareHelper/app.manifest +++ b/SoftwareHelper/app.manifest @@ -39,7 +39,7 @@ - + @@ -48,12 +48,13 @@ 自动缩放。Windows Presentation Foundation (WPF)应用程序自动感知 DPI,无需 选择加入。选择加入此设置的 Windows 窗体应用程序(目标设定为 .NET Framework 4.6 )还应 在其 app.config 中将 "EnableWindowsFormsHighDpiAutoResizing" 设置设置为 "true"。--> + - PerMonitor - true + true/PM +