添加项目文件。

This commit is contained in:
GG Z
2024-09-22 11:05:41 +08:00
parent fb5d55723a
commit 49ceaae6a8
764 changed files with 78850 additions and 0 deletions

View File

@@ -0,0 +1,121 @@
using System;
using System.Linq;
using System.Windows.Media;
using Wpf.Ui.Extend.Models;
namespace Wpf.Ui.Extend.Extensions
{
public static class HsbExtensions
{
/// <summary>
/// 将 HSB色相、饱和度、亮度颜色空间转换为 Color 对象。
/// </summary>
/// <param name="hsb"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public static Color ToColor(this Hsb hsb)
{
//0~1
var hue = hsb.Hue;
var saturation = hsb.Saturation;
var brightness = hsb.Brightness;
var opacity = hsb.Opacity;
brightness *= 255;
//饱和度为0直接返回亮度值
if (saturation.IsCloseTo(0))
return Color.FromArgb((byte)brightness, (byte)brightness, (byte)brightness, (byte)brightness);
opacity *= 255;
//处理色相超出范围
if (hue.IsCloseTo(360))
hue = 0;
while (hue > 360)
hue -= 360;
while (hue < 0)
hue += 360;
//通过色相返回颜色
hue /= 60;
//以60作为一个区间通过switch判断
var i = (int)Math.Floor(hue);
//计算出色相值在当前区间内的偏移量
var f = hue - i;
//计算出低饱和度情况下的亮度值
var p = brightness * (1 - saturation);
//计算出中间饱和度情况下的亮度值
var q = brightness * (1 - saturation * f);
//计算出高饱和度情况下的亮度值
var t = brightness * (1 - saturation * (1 - f));
return i switch
{
//[0~60)红色区间
0 => Color.FromArgb((byte)opacity, (byte)brightness, (byte)t, (byte)p),
//[60~120)黄色区间
1 => Color.FromArgb((byte)opacity, (byte)q, (byte)brightness, (byte)p),
//[120~180)绿色区间
2 => Color.FromArgb((byte)opacity, (byte)p, (byte)brightness, (byte)t),
//[180~240)青色区间
3 => Color.FromArgb((byte)opacity, (byte)p, (byte)q, (byte)brightness),
//[240~300)蓝色区间
4 => Color.FromArgb((byte)opacity, (byte)t, (byte)p, (byte)brightness),
//[300~360)洋红色区间
5 => Color.FromArgb((byte)opacity, (byte)brightness, (byte)p, (byte)q),
_ => throw new InvalidOperationException("不可用的HSB值"),
};
}
/// <summary>
/// 将 Color 对象转换为 HSB色相、饱和度、亮度颜色空间。
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
public static Hsb ToHsb(this Color color)
{
double r = color.R;
double g = color.G;
double b = color.B;
double a = color.A;
r /= 255;
g /= 255;
b /= 255;
a /= 255;
var rgb = new[] { r, g, b };
var max = rgb.Max();
var min = rgb.Min();
var brightness = max;
var hue = max;
var delta = max - min;
var saturation = max.IsCloseTo(0) ? 0 : delta / max;
if (max.IsCloseTo(min))
{
hue = 0; // 无色,未定义
}
else
{
if (max.IsCloseTo(r))
{
hue = (g - b) / delta + (g < b ? 6 : 0);
}
else if (max.IsCloseTo(g))
{
hue = (b - r) / delta + 2;
}
else if (max.IsCloseTo(b))
{
hue = (r - g) / delta + 4;
}
hue *= 60;
}
return new Hsb(hue, saturation, brightness,a);
}
private static bool IsCloseTo(this double value, double target, double tolerance = double.Epsilon) =>
Math.Abs(value - target) < tolerance;
}
}

View File

@@ -0,0 +1,97 @@
using System.Threading.Tasks;
using Wpf.Ui;
using Wpf.Ui.Controls;
using Wpf.Ui.Extend.Controls;
using Wpf.Ui.Extensions;
namespace Sai.RvKits;
public static class MessageExtensions
{
private static readonly SnackbarService SnackbarService = new();
private static readonly IContentDialogService ContentDialogService = new ContentDialogService();
public static void ShowDanger(this SnackbarPresenter SnackbarPresenter, string title, string message)
{
SnackbarService.SetSnackbarPresenter(SnackbarPresenter);
SnackbarService.Show(
title,
message,
ControlAppearance.Danger,
new SymbolIcon(SymbolRegular.Alert12),
SnackbarService.DefaultTimeOut
);
}
public static void ShowInfo(this SnackbarPresenter SnackbarPresenter, string title, string message)
{
SnackbarService.SetSnackbarPresenter(SnackbarPresenter);
SnackbarService.Show(
title,
message,
ControlAppearance.Info,
new SymbolIcon(SymbolRegular.Info12),
SnackbarService.DefaultTimeOut
);
}
public static void ShowCaution(this SnackbarPresenter SnackbarPresenter, string title, string message)
{
SnackbarService.SetSnackbarPresenter(SnackbarPresenter);
SnackbarService.Show(
title,
message,
ControlAppearance.Caution,
new SymbolIcon(SymbolRegular.Question16),
SnackbarService.DefaultTimeOut
);
}
public static async void ShowMessage(string content = "内容...", string title = "消息")
{
Wpf.Ui.Controls.MessageBox uiMessageBox = new()
{
Title = title,
Content = content,
MinWidth=200,
//PrimaryButtonIcon = SymbolRegular.Button16,
//PrimaryButtonAppearance = ControlAppearance.Primary,
//PrimaryButtonText = "确定",
//SecondaryButtonIcon = SymbolRegular.Button20,
//SecondaryButtonAppearance = ControlAppearance.Secondary,
//SecondaryButtonText = "取消",
CloseButtonAppearance = ControlAppearance.Primary,
//CloseButtonIcon = SymbolRegular.CalendarCancel16,
CloseButtonText = "确定",
WindowStartupLocation = System.Windows.WindowStartupLocation.Manual,
};
await uiMessageBox.ShowDialogAsync();
}
public static async Task<ContentDialogResult> ShowTaskDialog(
string title,
object content,
string primaryButtonText = "确定",
string secondaryButtonText = "取消",
string closeButtonText = "关闭"
)
{
return await ContentDialogService.ShowSimpleDialogAsync(
new SimpleContentDialogCreateOptions()
{
Title = title,
Content = content,
PrimaryButtonText = primaryButtonText,
SecondaryButtonText = secondaryButtonText,
CloseButtonText = closeButtonText,
}
);
//result switch
//{
// ContentDialogResult.Primary => "User saved their work",
// ContentDialogResult.Secondary => "User did not save their work",
// _ => "User cancelled the dialog"
//};
}
}

View File

@@ -0,0 +1,334 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
namespace Wpf.Ui.Extend.Extensions
{
public static class VisualExtensions
{
/// <summary>
/// 查找元素的子元素
/// </summary>
/// <typeparam name="T">子元素类型</typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static T FindVisualChild<T>(this DependencyObject obj) where T : DependencyObject
{
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
var child = VisualTreeHelper.GetChild(obj, i);
if (child is not null and T)
return (T)child;
else
{
var childOfChild = FindVisualChild<T>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
/// <summary>
/// 得到指定元素的集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="depObj"></param>
/// <returns></returns>
public static IEnumerable<T> FindVisualChildren<T>(this DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);
if (child is not null and T)
{
yield return (T)child;
}
foreach (var childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
/// <summary>
/// 获取属性
/// </summary>
public static object GetPropertyValue(this object obj, string path)
{
if (obj == null) return string.Empty;
var flag = !string.IsNullOrEmpty(path);
object result;
if (flag)
{
var property = obj.GetType().GetProperty(path);
var flag2 = property != null;
if (flag2)
{
try
{
result = property.GetValue(obj, null);
return result;
}
catch
{
try
{
var type = obj.GetType();
var mInfo = type.GetMethod("get_" + path);
if (mInfo != null)
{
result = mInfo.Invoke(obj, null);
return result;
}
}
catch { }
}
}
}
result = obj;
return result;
}
/// <summary>
/// 利用visualtreehelper寻找对象的子级对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static List<T> FindVisualChildrenEx<T>(this DependencyObject obj) where T : DependencyObject
{
try
{
List<T> TList = [];
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
var child = VisualTreeHelper.GetChild(obj, i);
if (child is not null and T)
{
TList.Add((T)child);
var childOfChildren = FindVisualChildrenEx<T>(child);
if (childOfChildren != null)
{
TList.AddRange(childOfChildren);
}
}
else
{
var childOfChildren = FindVisualChildrenEx<T>(child);
if (childOfChildren != null)
{
TList.AddRange(childOfChildren);
}
}
}
return TList;
}
catch
{
return null;
}
}
/// <summary>
/// 查找元素的父元素
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="i_dp"></param>
/// <returns></returns>
public static T FindParent<T>(this DependencyObject i_dp) where T : DependencyObject
{
var dobj = (DependencyObject)VisualTreeHelper.GetParent(i_dp);
if (dobj != null)
{
if (dobj is T t)
{
return t;
}
else
{
dobj = FindParent<T>(dobj);
if (dobj is not null and T)
{
return (T)dobj;
}
}
}
return null;
}
public static T FindParent<T>(this DependencyObject i_dp, string elementName) where T : DependencyObject
{
var dobj = (DependencyObject)VisualTreeHelper.GetParent(i_dp);
if (dobj != null)
{
if (dobj is T && ((System.Windows.FrameworkElement)(dobj)).Name.Equals(elementName))
{
return (T)dobj;
}
else
{
dobj = FindParent<T>(dobj);
if (dobj is not null and T)
{
return (T)dobj;
}
}
}
return null;
}
public static IntPtr GetHandle(this Visual visual)
{
return (PresentationSource.FromVisual(visual) as HwndSource)?.Handle ?? IntPtr.Zero;
}
/// <summary>
/// 查找指定名称的元素
/// </summary>
/// <typeparam name="childItem">元素类型</typeparam>
/// <param name="obj"></param>
/// <param name="elementName">元素名称及xaml中的Name</param>
/// <returns></returns>
public static childItem FindVisualElement<childItem>(this DependencyObject obj, string elementName) where childItem : DependencyObject
{
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
var child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is childItem && ((System.Windows.FrameworkElement)(child)).Name.Equals(elementName))
return (childItem)child;
else
{
IEnumerator j = FindVisualChildren<childItem>(child).GetEnumerator();
while (j.MoveNext())
{
var childOfChild = (childItem)j.Current;
if (childOfChild != null && !(childOfChild as FrameworkElement).Name.Equals(elementName))
{
FindVisualElement<childItem>(childOfChild, elementName);
}
else
{
return childOfChild;
}
}
}
}
return null;
}
/// <summary>
/// 命中测试。根据当前选中元素,查找视觉树父节点与子节点,看是否存在指定类型的元素
/// </summary>
/// <typeparam name="T">想命中的元素类型</typeparam>
/// <param name="dp">当前选中元素</param>
/// <returns>true命中成功</returns>
public static bool HitTest<T>(this DependencyObject dp) where T : DependencyObject
{
return FindParent<T>(dp) != null || FindVisualChild<T>(dp) != null;
}
public static T FindEqualElement<T>(DependencyObject source, DependencyObject element) where T : DependencyObject
{
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(source); i++)
{
var child = VisualTreeHelper.GetChild(source, i);
if (child != null && child is T && child == element)
{
return (T)child;
}
else
{
var childOfChild = FindVisualChild<T>(child);
if (childOfChild != null)
{
return childOfChild;
}
}
}
return null;
}
/// <summary>
/// Gets the dependency property according to its name.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="propertyName">Name of the property.</param>
/// <returns></returns>
public static DependencyProperty GetDependencyProperty(Type type, string propertyName)
{
DependencyProperty prop = null;
if (type != null)
{
var fieldInfo = type.GetField(propertyName + "Property", BindingFlags.Static | BindingFlags.Public);
if (fieldInfo != null)
{
prop = fieldInfo.GetValue(null) as DependencyProperty;
}
}
return prop;
}
/// <summary>
/// Retrieves a <see cref="DependencyProperty"/> using reflection.
/// </summary>
/// <param name="o"></param>
/// <param name="propertyName"></param>
/// <returns></returns>
public static DependencyProperty GetDependencyProperty(this DependencyObject o, string propertyName)
{
DependencyProperty prop = null;
if (o != null)
{
prop = GetDependencyProperty(o.GetType(), propertyName);
}
return prop;
}
/// <summary>
/// 设置 <paramref name="property"/> 只有在没有明确设置的情况下才会使用。
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="o">The object.</param>
/// <param name="property">The property.</param>
/// <param name="value">The value.</param>
/// <returns></returns>
public static bool SetIfDefault<T>(this DependencyObject o, DependencyProperty property, T value)
{
if (o == null) throw new ArgumentNullException("o", "DependencyObject cannot be null");
if (property == null) throw new ArgumentNullException("property", "DependencyProperty cannot be null");
if (!property.PropertyType.IsAssignableFrom(typeof(T)))
{
throw new ArgumentException(
string.Format("Expected {0} to be of type {1} but was {2}",
property.Name, typeof(T).Name, property.PropertyType));
}
if (DependencyPropertyHelper.GetValueSource(o, property).BaseValueSource == BaseValueSource.Default)
{
o.SetValue(property, value);
return true;
}
return false;
}
}
}