From 5a1da60237a3f33b06b9605c2da1e19a0aa7c5bd Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Fri, 16 Sep 2022 09:21:15 +0800 Subject: [PATCH 1/5] fix: get active window issue. --- src/Shared/HandyControl_Shared/Tools/Helper/WindowHelper.cs | 6 +++++- .../HandyControl_Shared/Tools/Interop/InteropMethods.cs | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Shared/HandyControl_Shared/Tools/Helper/WindowHelper.cs b/src/Shared/HandyControl_Shared/Tools/Helper/WindowHelper.cs index 23ec5e0f..6bab0d0d 100644 --- a/src/Shared/HandyControl_Shared/Tools/Helper/WindowHelper.cs +++ b/src/Shared/HandyControl_Shared/Tools/Helper/WindowHelper.cs @@ -20,7 +20,11 @@ public static class WindowHelper /// 获取当前应用中处于激活的一个窗口 /// /// - public static Window GetActiveWindow() => Application.Current.Windows.OfType().SingleOrDefault(x => x.IsActive); + public static Window GetActiveWindow() + { + var activeWindow = InteropMethods.GetActiveWindow(); + return Application.Current.Windows.OfType().FirstOrDefault(x => x.GetHandle() == activeWindow); + } private static readonly BitArray _cacheValid = new((int) InteropValues.CacheSlot.NumSlots); diff --git a/src/Shared/HandyControl_Shared/Tools/Interop/InteropMethods.cs b/src/Shared/HandyControl_Shared/Tools/Interop/InteropMethods.cs index 4749a44e..23fec702 100644 --- a/src/Shared/HandyControl_Shared/Tools/Interop/InteropMethods.cs +++ b/src/Shared/HandyControl_Shared/Tools/Interop/InteropMethods.cs @@ -348,6 +348,9 @@ internal class InteropMethods [DllImport(InteropValues.ExternDll.User32)] internal static extern IntPtr GetWindow(IntPtr hwnd, int nCmd); + [DllImport(InteropValues.ExternDll.User32)] + internal static extern IntPtr GetActiveWindow(); + [DllImport(InteropValues.ExternDll.User32)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool IsWindowVisible(IntPtr hwnd); From 25f4f9618afd5ff277d759b479e900015c01c37a Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Tue, 27 Sep 2022 23:17:16 +0800 Subject: [PATCH 2/5] fix: fixed #1162 --- .../Controls/Input/NumericUpDown.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Shared/HandyControl_Shared/Controls/Input/NumericUpDown.cs b/src/Shared/HandyControl_Shared/Controls/Input/NumericUpDown.cs index a8aa262d..81c1fa7d 100644 --- a/src/Shared/HandyControl_Shared/Controls/Input/NumericUpDown.cs +++ b/src/Shared/HandyControl_Shared/Controls/Input/NumericUpDown.cs @@ -45,6 +45,12 @@ public class NumericUpDown : Control public override void OnApplyTemplate() { + if (_textBox != null) + { + _textBox.PreviewKeyDown -= TextBox_PreviewKeyDown; + _textBox.TextChanged -= TextBox_TextChanged; + } + base.OnApplyTemplate(); _textBox = GetTemplateChild(ElementTextBox) as TextBox; @@ -59,10 +65,23 @@ public class NumericUpDown : Control _textBox.SetBinding(CaretBrushProperty, new Binding(CaretBrushProperty.Name) { Source = this }); _textBox.PreviewKeyDown += TextBox_PreviewKeyDown; + _textBox.TextChanged += TextBox_TextChanged; _textBox.Text = CurrentText; } } + private void TextBox_TextChanged(object sender, TextChangedEventArgs e) + { + if (string.IsNullOrWhiteSpace(_textBox.Text)) + { + SetCurrentValue(ValueProperty, ValueBoxes.Int0Box); + } + else if (double.TryParse(_textBox.Text, out var value)) + { + SetCurrentValue(ValueProperty, value); + } + } + private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e) { if (IsReadOnly) return; From d664289be6cbc3f0359597483a0e22b1457962bc Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Tue, 27 Sep 2022 23:22:06 +0800 Subject: [PATCH 3/5] chore: change default value of DropDownElement.ConsistentWidth. --- .../HandyControl_Shared/Controls/Attach/DropDownElement.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Shared/HandyControl_Shared/Controls/Attach/DropDownElement.cs b/src/Shared/HandyControl_Shared/Controls/Attach/DropDownElement.cs index 20d80110..a32e99b1 100644 --- a/src/Shared/HandyControl_Shared/Controls/Attach/DropDownElement.cs +++ b/src/Shared/HandyControl_Shared/Controls/Attach/DropDownElement.cs @@ -7,7 +7,7 @@ public class DropDownElement { public static readonly DependencyProperty ConsistentWidthProperty = DependencyProperty.RegisterAttached( "ConsistentWidth", typeof(bool), typeof(DropDownElement), - new FrameworkPropertyMetadata(ValueBoxes.TrueBox, FrameworkPropertyMetadataOptions.Inherits)); + new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.Inherits)); public static void SetConsistentWidth(DependencyObject element, bool value) => element.SetValue(ConsistentWidthProperty, ValueBoxes.BooleanBox(value)); From 8a66f92089efe65cdd586c2be1c0d95f38627b0d Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Wed, 28 Sep 2022 10:16:06 +0800 Subject: [PATCH 4/5] fix: NumericUpDown issue. --- .../Controls/Input/NumericUpDown.cs | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/Shared/HandyControl_Shared/Controls/Input/NumericUpDown.cs b/src/Shared/HandyControl_Shared/Controls/Input/NumericUpDown.cs index 81c1fa7d..9c878bb7 100644 --- a/src/Shared/HandyControl_Shared/Controls/Input/NumericUpDown.cs +++ b/src/Shared/HandyControl_Shared/Controls/Input/NumericUpDown.cs @@ -58,7 +58,7 @@ public class NumericUpDown : Control if (_textBox != null) { _textBox.SetBinding(SelectionBrushProperty, new Binding(SelectionBrushProperty.Name) { Source = this }); -#if !(NET40 || NET45 || NET451 || NET452 || NET46 || NET461 || NET462 || NET47 || NET471 || NET472) +#if NET48_OR_GREATER _textBox.SetBinding(SelectionTextBrushProperty, new Binding(SelectionTextBrushProperty.Name) { Source = this }); #endif _textBox.SetBinding(SelectionOpacityProperty, new Binding(SelectionOpacityProperty.Name) { Source = this }); @@ -66,19 +66,31 @@ public class NumericUpDown : Control _textBox.PreviewKeyDown += TextBox_PreviewKeyDown; _textBox.TextChanged += TextBox_TextChanged; + _textBox.LostFocus += TextBox_LostFocus; _textBox.Text = CurrentText; } } + private void TextBox_LostFocus(object sender, RoutedEventArgs e) + { + if (string.IsNullOrWhiteSpace(_textBox.Text)) + { + SetCurrentValue(ValueProperty, ValueBoxes.Double0Box); + } + else if (double.TryParse(_textBox.Text, out double value)) + { + SetCurrentValue(ValueProperty, value); + } + } + private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { - if (string.IsNullOrWhiteSpace(_textBox.Text)) + if (double.TryParse(_textBox.Text, out double value)) { - SetCurrentValue(ValueProperty, ValueBoxes.Int0Box); - } - else if (double.TryParse(_textBox.Text, out var value)) - { - SetCurrentValue(ValueProperty, value); + if (value >= Minimum && value <= Maximum) + { + SetCurrentValue(ValueProperty, value); + } } } @@ -328,7 +340,7 @@ public class NumericUpDown : Control set => SetValue(SelectionBrushProperty, value); } -#if !(NET40 || NET45 || NET451 || NET452 || NET46 || NET461 || NET462 || NET47 || NET471 || NET472) +#if NET48_OR_GREATER public static readonly DependencyProperty SelectionTextBrushProperty = TextBoxBase.SelectionTextBrushProperty.AddOwner(typeof(NumericUpDown)); From 4c09f67e9233a6f76081eda8eda10c75a3c5b3f9 Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Wed, 28 Sep 2022 22:23:41 +0800 Subject: [PATCH 5/5] enhance: optimized Divider. --- .../UserControl/Controls/DividerDemoCtl.xaml | 4 +++- src/Shared/HandyControl_Shared/Controls/Other/Divider.cs | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Shared/HandyControlDemo_Shared/UserControl/Controls/DividerDemoCtl.xaml b/src/Shared/HandyControlDemo_Shared/UserControl/Controls/DividerDemoCtl.xaml index 62c81bf1..13349584 100644 --- a/src/Shared/HandyControlDemo_Shared/UserControl/Controls/DividerDemoCtl.xaml +++ b/src/Shared/HandyControlDemo_Shared/UserControl/Controls/DividerDemoCtl.xaml @@ -11,7 +11,9 @@ - + +