Skip to content

Commit

Permalink
Refactor Tools
Browse files Browse the repository at this point in the history
  • Loading branch information
ghost1372 committed Mar 30, 2022
1 parent 734af67 commit f1e585a
Show file tree
Hide file tree
Showing 25 changed files with 632 additions and 379 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
namespace HandyControl.Tools.Extension;

/// <summary>
/// 颜色扩展类
/// Color extension class
/// </summary>
public static class ColorExtension
{
/// <summary>
/// 将颜色转换为10进制表示(rgb顺序颠倒)
/// Convert color to decimal representation (rgb order is reversed)
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
public static int ToInt32(this Color color) => color.R << 16 | color.G << 8 | color.B;

/// <summary>
/// 将颜色转换为10进制表示(rgb顺序颠倒)
/// Convert color to decimal representation (rgb order is reversed)
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace HandyControl.Tools.Extension;
public static class GeometryExtension
{
/// <summary>
/// 获取路径总长度
/// Get the total length of the path
/// </summary>
/// <param name="geometry"></param>
/// <returns></returns>
Expand All @@ -24,7 +24,7 @@ public static double GetTotalLength(this Geometry geometry)
}

/// <summary>
/// 获取路径总长度
/// Get the total length of the path
/// </summary>
/// <param name="geometry"></param>
/// <param name="size"></param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ namespace HandyControl.Tools.Extension;
public static class UIElementExtension
{
/// <summary>
/// 显示元素
/// Display element
/// </summary>
/// <param name="element"></param>
public static void Show(this UIElement element) => element.Visibility = Visibility.Visible;

/// <summary>
/// 显示元素
/// Display element
/// </summary>
/// <param name="element"></param>
/// <param name="show"></param>
public static void Show(this UIElement element, bool show) => element.Visibility = show ? Visibility.Visible : Visibility.Collapsed;

/// <summary>
/// 不现实元素,但保留空间
/// Unrealistic elements, but reserve space
/// </summary>
/// <param name="element"></param>
public static void Hide(this UIElement element) => element.Visibility = Visibility.Hidden;

/// <summary>
/// 不显示元素,且不保留空间
/// No elements are displayed, and no space is reserved
/// </summary>
/// <param name="element"></param>
public static void Collapse(this UIElement element) => element.Visibility = Visibility.Collapsed;
Expand Down
19 changes: 0 additions & 19 deletions src/Shared/HandyControl_Shared/Tools/Helper/ColorHelper.cs

This file was deleted.

8 changes: 4 additions & 4 deletions src/Shared/HandyControl_Shared/Tools/Helper/ConfigHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@
#endif
using System.Windows;
using System.Windows.Markup;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using HandyControl.Data;
using HandyControl.Properties.Langs;

using Timeline = System.Windows.Media.Animation.Timeline;
namespace HandyControl.Tools;

public class ConfigHelper : INotifyPropertyChanged
{
private ConfigHelper()
{

}

public static ConfigHelper Instance = new Lazy<ConfigHelper>(() => new ConfigHelper()).Value;

private XmlLanguage _lang = XmlLanguage.GetLanguage("zh-cn");
private XmlLanguage _lang = XmlLanguage.GetLanguage("en");

public XmlLanguage Lang
{
Expand All @@ -42,6 +41,7 @@ public void SetLang(string lang)
LangProvider.Culture = new CultureInfo(lang);
Application.Current.Dispatcher.Thread.CurrentUICulture = new CultureInfo(lang);
Lang = XmlLanguage.GetLanguage(lang);
LocalizationManager.Instance.OnCultureChanged(new CultureInfo(lang));
}

public void SetConfig(HandyControlConfig config)
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/HandyControl_Shared/Tools/Helper/DpiHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace HandyControl.Tools;

internal static class DpiHelper
public static class DpiHelper
{
private const double LogicalDpi = 96.0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace HandyControl.Controls;
/// 采用设置窗口位置和尺寸,确保盖住整个屏幕的方式来实现全屏
/// 目前已知需要满足的条件是:窗口盖住整个屏幕、窗口没有WS_THICKFRAME样式、窗口不能有标题栏且最大化
/// </summary>
internal static class FullScreenHelper
public static class FullScreenHelper
{
/// <summary>
/// 用于记录窗口全屏前位置的附加属性
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/HandyControl_Shared/Tools/Helper/IconHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace HandyControl.Tools;

[SuppressMessage("ReSharper", "ArrangeRedundantParentheses")]
[SuppressMessage("ReSharper", "IntVariableOverflowInUncheckedContext")]
internal static class IconHelper
public static class IconHelper
{
private static Size SmallIconSize;

Expand Down
43 changes: 4 additions & 39 deletions src/Shared/HandyControl_Shared/Tools/Helper/ResourceHelper.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
using System;
using System.Reflection;
using System.Windows;
using HandyControl.Data;
using System.Windows;

namespace HandyControl.Tools;

/// <summary>
/// 资源帮助类
/// Resource help class
/// </summary>
public class ResourceHelper
{
private static ResourceDictionary _theme;

/// <summary>
/// 获取资源
/// Get Resource
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
Expand All @@ -37,38 +34,6 @@ internal static T GetResourceInternal<T>(string key)
return default;
}

/// <summary>
/// 获取皮肤
/// </summary>
public static ResourceDictionary GetSkin(Assembly assembly, string themePath, SkinType skin)
{
try
{
var uri = new Uri($"pack://application:,,,/{assembly.GetName().Name};component/{themePath}/Skin{skin}.xaml");
return new ResourceDictionary
{
Source = uri
};
}
catch
{
return new ResourceDictionary
{
Source = new Uri($"pack://application:,,,/{assembly.GetName().Name};component/{themePath}/Skin{SkinType.Default}.xaml")
};
}
}

/// <summary>
/// get HandyControl skin
/// </summary>
/// <param name="skin"></param>
/// <returns></returns>
public static ResourceDictionary GetSkin(SkinType skin) => new()
{
Source = new Uri($"pack://application:,,,/HandyControl;component/Themes/Skin{skin}.xaml")
};

/// <summary>
/// get HandyControl theme
/// </summary>
Expand All @@ -78,7 +43,7 @@ public static ResourceDictionary GetStandaloneTheme()
{
return new()
{
Source = new Uri("pack://application:,,,/HandyControl;component/Themes/Theme.xaml")
Source = ApplicationHelper.GetAbsoluteUri("Themes/Theme.xaml")
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace HandyControl.Tools;

internal class ScreenHelper
public class ScreenHelper
{
internal static void FindMaximumSingleMonitorRectangle(Rect windowRect, out Rect screenSubRect, out Rect monitorRect)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
namespace HandyControl.Tools;

/// <summary>
/// 该类可以为可视化元素提供单开的功能
/// This class can provide single-open functions for visual elements
/// </summary>
public class SingleOpenHelper
{
private static readonly Dictionary<string, ISingleOpen> OpenDic = new();

/// <summary>
/// 根据指定的类型创建实例
/// Create an instance based on the specified type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
Expand Down
28 changes: 0 additions & 28 deletions src/Shared/HandyControl_Shared/Tools/Helper/SystemHelper.cs

This file was deleted.

20 changes: 10 additions & 10 deletions src/Shared/HandyControl_Shared/Tools/Helper/WindowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

namespace HandyControl.Tools;

public static class WindowHelper
public static partial class WindowHelper
{
/// <summary>
/// 获取当前应用中处于激活的一个窗口
/// Get the active window in the current application
/// </summary>
/// <returns></returns>
public static Window GetActiveWindow() => Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
Expand Down Expand Up @@ -145,7 +145,7 @@ public static Thickness WindowMaximizedPadding
InteropMethods.ReleaseDC(IntPtr.Zero, hdc);
return WindowResizeBorderThickness.Add(new Thickness((autoHide ? -4 : 4) * scale));
#else
return WindowResizeBorderThickness.Add(new Thickness(autoHide ? -4 : 4));
return WindowResizeBorderThickness.Add(new Thickness(autoHide ? -4 : 4));
#endif
}
}
Expand All @@ -157,12 +157,12 @@ public static Thickness WindowMaximizedPadding
public static HwndSource GetHwndSource(this Window window) => HwndSource.FromHwnd(window.GetHandle());

/// <summary>
/// 让窗口激活作为前台最上层窗口
/// Make the window active as the topmost window in the foreground
/// </summary>
/// <param name="window"></param>
public static void SetWindowToForeground(Window window)
{
// [WPF 让窗口激活作为前台最上层窗口的方法 - lindexi - 博客园](https://www.cnblogs.com/lindexi/p/12749671.html)
// [WPF Make the window active as the uppermost window in the foreground - lindexi - Blog](https://www.cnblogs.com/lindexi/p/12749671.html)
var interopHelper = new WindowInteropHelper(window);
var thisWindowThreadId = InteropMethods.GetWindowThreadProcessId(interopHelper.Handle, out _);
var currentForegroundWindow = InteropMethods.GetForegroundWindow();
Expand All @@ -171,11 +171,11 @@ public static void SetWindowToForeground(Window window)
// [c# - Bring a window to the front in WPF - Stack Overflow](https://stackoverflow.com/questions/257587/bring-a-window-to-the-front-in-wpf )
// [SetForegroundWindow的正确用法 - 子坞 - 博客园](https://www.cnblogs.com/ziwuge/archive/2012/01/06/2315342.html )
/*
1.得到窗口句柄FindWindow
2.切换键盘输入焦点AttachThreadInput
3.显示窗口ShowWindow(有些窗口被最小化/隐藏了)
4.更改窗口的Z Order,SetWindowPos使之最上,为了不影响后续窗口的Z Order,改完之后,再还原
5.最后SetForegroundWindow
  1.得到窗口句柄FindWindow
    2.切换键盘输入焦点AttachThreadInput
    3.显示窗口ShowWindow(有些窗口被最小化/隐藏了)
    4.更改窗口的Z Order,SetWindowPos使之最上,为了不影响后续窗口的Z Order,改完之后,再还原
    5.最后SetForegroundWindow
*/

InteropMethods.AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, true);
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/HandyControl_Shared/Tools/Hook/MouseHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace HandyControl.Tools;

internal class MouseHook
public class MouseHook
{
public static event EventHandler<MouseHookEventArgs> StatusChanged;

Expand Down
24 changes: 23 additions & 1 deletion src/Shared/HandyControl_Shared/Tools/Hook/SystemMenuHook.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using HandyControl.Tools.Interop;

Expand All @@ -9,9 +10,20 @@ namespace HandyControl.Tools;
public class SystemMenuHook
{
private static readonly Dictionary<int, HwndSource> DataDic = new();

private static ContextMenu context = null;
public static event Action<int> Click;

public static void SetCustomContextMenu(Window window, ContextMenu contextMenu)
{
context = contextMenu;
var hookId = window.GetHandle();
var source = HwndSource.FromHwnd(hookId);
if (source != null)
{
source.AddHook(new HwndSourceHook(WndProc));
}
}

public static void Insert(int index, int id, string text, Window window)
{
var hookId = window.GetHandle();
Expand Down Expand Up @@ -49,4 +61,14 @@ private static IntPtr WinProc(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam
}
return IntPtr.Zero;
}

private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if ((msg == InteropValues.WM_NCRBUTTONDOWN) && (wParam.ToInt32() == InteropValues.HTCAPTION))
{
context.IsOpen = true;
handled = true;
}
return IntPtr.Zero;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace HandyControl.Tools.Interop;

[SuppressMessage("ReSharper", "UnusedMember.Local")]
internal sealed class BitmapHandle : WpfSafeHandle
public sealed class BitmapHandle : WpfSafeHandle
{
[SecurityCritical]
private BitmapHandle() : this(true)
Expand Down
Loading

0 comments on commit f1e585a

Please sign in to comment.