From a061384ba628a09bf4df91f50321ab22c03129aa Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期二, 18 三月 2025 20:58:36 +0800
Subject: [PATCH] Winform支持遮罩层;
---
HMI.UI/DpUtil/GridHelper.cs | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 115 insertions(+), 3 deletions(-)
diff --git a/HMI.UI/DpUtil/GridHelper.cs b/HMI.UI/DpUtil/GridHelper.cs
index 70fc48d..d530c00 100644
--- a/HMI.UI/DpUtil/GridHelper.cs
+++ b/HMI.UI/DpUtil/GridHelper.cs
@@ -1,10 +1,14 @@
-锘縰sing System.Windows;
+锘縰sing System.Collections.Generic;
+using System.Windows;
using System.Windows.Controls;
+using System.Windows.Controls.Primitives;
+using System.Windows.Media;
namespace HMI.UI.DpUtil
{
public class GridHelper
{
+ #region Space
public static double GetSpace(DependencyObject obj)
{
return (double)obj.GetValue(SpaceProperty);
@@ -13,11 +17,12 @@
{
obj.SetValue(SpaceProperty, value);
}
+
public static readonly DependencyProperty SpaceProperty =
DependencyProperty.RegisterAttached("Space", typeof(double), typeof(Grid),
- new PropertyMetadata(0d, OnShowBorderChanged));
+ new PropertyMetadata(0d, OnSpaceChanged));
- private static void OnShowBorderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+ private static void OnSpaceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is Grid grid)
{
@@ -62,6 +67,113 @@
(row + rowSpan) == rowCount ? space : 0);
}
}
+ #endregion
+ public static Brush GetInnerBorderBrush(DependencyObject obj)
+ {
+ return (Brush)obj.GetValue(InnerBorderBrushProperty);
+ }
+ public static void SetInnerBorderBrush(DependencyObject obj, Brush value)
+ {
+ obj.SetValue(InnerBorderBrushProperty, value);
+ }
+ public static readonly DependencyProperty InnerBorderBrushProperty =
+ DependencyProperty.RegisterAttached("InnerBorderBrush", typeof(Brush), typeof(Grid),
+ new PropertyMetadata(null, OnInnerBorderChanged));
+
+ public static double GetInnerBorder(DependencyObject obj)
+ {
+ return (double)obj.GetValue(InnerBorderProperty);
+ }
+
+ public static void SetInnerBorder(DependencyObject obj, object value)
+ {
+ double dv = double.Parse(value.ToString());
+ obj.SetValue(InnerBorderProperty, dv);
+ }
+
+ public static readonly DependencyProperty InnerBorderProperty =
+ DependencyProperty.RegisterAttached("InnerBorder", typeof(object), typeof(Grid),
+ new PropertyMetadata(0, OnInnerBorderChanged));
+
+ public static string GetInnerBorderIgnore(DependencyObject obj)
+ {
+ return (string)obj.GetValue(InnerBorderIgnoreProperty);
+ }
+ public static void SetInnerBorderIgnore(DependencyObject obj, string value)
+ {
+ obj.SetValue(InnerBorderIgnoreProperty, value);
+ }
+
+ public static readonly DependencyProperty InnerBorderIgnoreProperty =
+ DependencyProperty.RegisterAttached("InnerBorderIgnore", typeof(string),
+ typeof(Grid), new PropertyMetadata("None", OnInnerBorderChanged));
+
+ private static void OnInnerBorderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+ {
+ if (d is Grid grid)
+ {
+ if (e.Property == InnerBorderProperty || e.Property == InnerBorderIgnoreProperty)
+ {
+ grid.Loaded += GridInnerBorder_Loaded;
+ return;
+ }
+ }
+ }
+
+ private static void GridInnerBorder_Loaded(object sender, RoutedEventArgs e)
+ {
+ Grid grid = sender as Grid;
+ RefreshGridInnerBorder(grid);
+ grid.Loaded -= GridInnerBorder_Loaded;
+ }
+
+ public static void RefreshGridInnerBorder(Grid grid)
+ {
+ double width = GetInnerBorder(grid);
+ if (width <= 0) return;
+ var rowCount = grid.RowDefinitions.Count;
+ var columnCount = grid.ColumnDefinitions.Count;
+ int totalCount = rowCount * columnCount;
+
+ string baseData = GetInnerBorderIgnore(grid);
+ HashSet<string> data = new HashSet<string>(baseData.Split(','));
+ bool ignoreTop = data.Contains("All") || data.Contains("Top"),
+ ignoreBottom = data.Contains("All") || data.Contains("Bottom"),
+ ignoreLeft = data.Contains("All") || data.Contains("Left"),
+ ignoreRight = data.Contains("All") || data.Contains("Right");
+
+ foreach (var child in grid.Children)
+ {
+ if (child is Control control)
+ {
+ Thickness bt;
+ Brush bb = GetInnerBorderBrush(grid) ?? Brushes.Black;
+
+
+ if (GetSpace(grid) > width)
+ {
+ bt = new Thickness(width);
+ }
+ else
+ {
+ int row = Grid.GetRow(control),
+ col = Grid.GetColumn(control),
+ rowSpan = Grid.GetRowSpan(control),
+ colSpan = Grid.GetColumnSpan(control);
+
+ double left = (col == 0 && ignoreLeft) ? 0 : width;
+ double top = (row == 0 && ignoreTop) ? 0 : width;
+
+ double right = ignoreRight ? 0 : ((col + colSpan == columnCount) ? width : 0);
+ double bottom = ignoreBottom ? 0 : ((row + rowSpan == rowCount) ? width : 0);
+
+ bt = new Thickness(left, top, right, bottom);
+ }
+ control.BorderBrush = bb;
+ control.BorderThickness = bt;
+ }
+ }
+ }
}
}
--
Gitblit v1.9.1