This commit is contained in:
GG Z
2025-07-31 20:12:24 +08:00
parent 4f6cd2137c
commit f209e7d3ad
426 changed files with 15854 additions and 6612 deletions

View File

@@ -0,0 +1,34 @@
namespace AntDesignWPF.Extensions;
/// <summary>
/// A collection of several extensions to the <see cref="DateTime"/> class.
/// </summary>
public static class DateTimeExtensions
{
/// <summary>
/// Gets the number of microseconds that have elapsed since the Unix epoch, excluding leap seconds. The Unix epoch
/// is 00:00:00 UTC on 1 January 1970.
/// </summary>
public static long GetMicroTimestamp(this DateTime dateTime)
{
// Should be 10^-6
return dateTime.Subtract(new DateTime(1970, 1, 1)).Ticks / (TimeSpan.TicksPerMillisecond / 1000);
}
/// <summary>
/// Gets the number of milliseconds that have elapsed since the Unix epoch, excluding leap seconds. The Unix epoch
/// is 00:00:00 UTC on 1 January 1970.
/// </summary>
public static long GetMillisTimestamp(this DateTime dateTime)
{
// Should be 10^-3
return (long)dateTime.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
}
/// <summary>
/// Gets the number of seconds that have elapsed since the Unix epoch, excluding leap seconds. The Unix epoch is
/// 00:00:00 UTC on 1 January 1970.
/// </summary>
public static long GetTimestamp(this DateTime dateTime)
{ return (long)dateTime.Subtract(new DateTime(1970, 1, 1)).TotalSeconds; }
}

View File

@@ -0,0 +1,22 @@
#if NETFRAMEWORK
using System.Diagnostics.Contracts;
namespace AntDesignWPF.Extensions;
public static class StringExtensions
{
/// <summary>
/// 使用指定的比较规则,返回指定字符串是否出现在此字符串中的值。
/// </summary>
/// <param name="source">源字符串。</param>
/// <param name="value">要查找的字符串。</param>
/// <param name="comparison">枚举值之一,用于指定比较中使用的规则。</param>
/// <returns>如果参数 value 出现在该字符串中,或 value 为空字符串 ("") 则为 true否则为 false。</returns>
[Pure]
public static bool Contains(this string source, string value, StringComparison comparison)
{ return source.IndexOf(value, comparison) >= 0; }
}
#endif

View File

@@ -0,0 +1,37 @@
using System.Windows;
namespace AntDesignWPF.Extensions;
internal static class UiElementExtensions
{
private static int Get_X_LParam(IntPtr lParam) { return (short)(lParam.ToInt32() & 0xFFFF); }
private static int Get_Y_LParam(IntPtr lParam) { return (short)(lParam.ToInt32() >> 16); }
/// <summary>
/// 不要在 NCHITTEST、NCLBUTTONUP、NCLBUTTONDOWN 消息之外调用它!
/// </summary>
/// <returns>如果鼠标位于元素上<see langword="true"/>。 否则,<see langword="false"/> </returns>
public static bool IsMouseOverElement(this UIElement element, IntPtr lParam)
{
// 此方法将非常频繁地调用,并且必须尽可能简单。
if (lParam == IntPtr.Zero)
{
return false;
}
try
{
var mousePosScreen = new Point(Get_X_LParam(lParam), Get_Y_LParam(lParam));
var bounds = new Rect(default, element.RenderSize);
Point mousePosRelative = element.PointFromScreen(mousePosScreen);
return bounds.Contains(mousePosRelative);
} catch
{
return false;
}
}
}

View File

@@ -0,0 +1,335 @@
using System;
using System.Collections;
using System.Reflection;
using System.Windows.Interop;
using System.Windows.Media;
namespace AntDesignWPF.Extensions
{
public static class VisualExtensions
{
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 t && child == element)
{
return t;
}
else
{
var childOfChild = FindVisualChild<T>(child);
if (childOfChild != null)
{
return childOfChild;
}
}
}
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 = 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 = 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;
}
/// <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>
/// 利用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="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>
/// 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;
}
public static IntPtr GetHandle(this Visual visual)
{ return (PresentationSource.FromVisual(visual) as HwndSource)?.Handle ?? IntPtr.Zero; }
/// <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>
/// 命中测试。根据当前选中元素,查找视觉树父节点与子节点,看是否存在指定类型的元素
/// </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; }
/// <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(nameof(o), "DependencyObject cannot be null");
if (property == null)
throw new ArgumentNullException(nameof(property), "DependencyProperty cannot be null");
if (!property.PropertyType.IsAssignableFrom(typeof(T)))
{
throw new ArgumentException(
$"Expected {property.Name} to be of type {typeof(T).Name} but was {property.PropertyType}");
}
if (DependencyPropertyHelper.GetValueSource(o, property).BaseValueSource == BaseValueSource.Default)
{
o.SetValue(property, value);
return true;
}
return false;
}
}
}