c# - 用于控制用户可调整大小的 WPF 装饰器
问题描述
我为 TextBlock 元素创建了一个简单的装饰器,它允许用户更改其大小。您可以更改块的四个角的大小,即一次更改两个大小(左上拇指,右上拇指,左下拇指,右下拇指)。一切正常,你可以在第一个 gif 上看到它:
另外,我想添加在按住 shift 键的情况下按比例调整大小的功能。你可以在第二个 gif 上看到这个结果:
如您所见,左上拇指和右下拇指允许您正确更改尺寸。但是,其他两个拇指元素不起作用,我不明白它是如何做到的。
XAML:
<Window x:Class="BagControlResize.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Canvas x:Name="canvas">
<TextBlock x:Name="testBlock" Canvas.Left="250" Canvas.Top="120" Width="300" Height="200" Background="Green"/>
</Canvas>
</Window>
创建装饰器:
using System.Windows;
using System.Windows.Documents;
namespace BagControlResize
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += (sender, e) =>
{
var adorner = AdornerLayer.GetAdornerLayer(canvas);
adorner.Add(new TextBlockAdorner(testBlock));
};
}
}
}
装饰者:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
namespace BagControlResize
{
public class TextBlockAdorner : Adorner
{
private double angle = 0.0;
private Point transformOrigin = new Point(0, 0);
private TextBlock childElement;
private VisualCollection visualChilderns;
private Thumb leftTop, rightTop, leftBottom, rightBottom;
public TextBlockAdorner(UIElement element) : base(element)
{
visualChilderns = new VisualCollection(this);
childElement = element as TextBlock;
CreateThumbPart(ref leftTop);
leftTop.DragDelta += (sender, e) =>
{
double hor = e.HorizontalChange;
double vert = e.VerticalChange;
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
{
double _max = hor > vert ? hor : vert;
hor = _max;
vert = _max;
}
ResizeX(hor);
ResizeY(vert);
e.Handled = true;
};
CreateThumbPart(ref rightTop);
rightTop.DragDelta += (sender, e) =>
{
double hor = e.HorizontalChange;
double vert = e.VerticalChange;
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
{
// THIS: NO WORKED
double _max = Math.Abs(hor) > Math.Abs(vert) ? Math.Abs(hor) : Math.Abs(vert);
if (hor >= 0 && vert <= 0)
{
hor = _max;
vert = -_max;
}
else
{
hor = -_max;
vert = _max;
}
}
ResizeWidth(hor);
ResizeY(vert);
e.Handled = true;
};
CreateThumbPart(ref leftBottom);
leftBottom.DragDelta += (sender, e) =>
{
double hor = e.HorizontalChange;
double vert = e.VerticalChange;
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
{
// THIS: NO WORKED
double _max = Math.Abs(hor) > Math.Abs(vert) ? Math.Abs(hor) : Math.Abs(vert);
if (hor <= 0 && vert >= 0)
{
hor = -_max;
vert = _max;
}
else
{
hor = _max;
vert = -_max;
}
}
ResizeX(hor);
ResizeHeight(vert);
e.Handled = true;
};
CreateThumbPart(ref rightBottom);
rightBottom.DragDelta += (sender, e) =>
{
double hor = e.HorizontalChange;
double vert = e.VerticalChange;
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
{
double _max = hor > vert ? hor : vert;
hor = _max;
vert = _max;
}
ResizeWidth(hor);
ResizeHeight(vert);
e.Handled = true;
};
}
private void ResizeWidth(double e)
{
double deltaHorizontal = Math.Min(-e, childElement.ActualWidth - childElement.MinWidth);
Canvas.SetTop(childElement, Canvas.GetTop(childElement) - transformOrigin.X * deltaHorizontal * Math.Sin(angle));
Canvas.SetLeft(childElement, Canvas.GetLeft(childElement) + (deltaHorizontal * transformOrigin.X * (1 - Math.Cos(angle))));
childElement.Width -= deltaHorizontal;
}
private void ResizeX(double e)
{
double deltaHorizontal = Math.Min(e, childElement.ActualWidth - childElement.MinWidth);
Canvas.SetTop(childElement, Canvas.GetTop(childElement) + deltaHorizontal * Math.Sin(angle) - transformOrigin.X * deltaHorizontal * Math.Sin(angle));
Canvas.SetLeft(childElement, Canvas.GetLeft(childElement) + deltaHorizontal * Math.Cos(angle) + (transformOrigin.X * deltaHorizontal * (1 - Math.Cos(angle))));
childElement.Width -= deltaHorizontal;
}
private void ResizeHeight(double e)
{
double deltaVertical = Math.Min(-e, childElement.ActualHeight - childElement.MinHeight);
Canvas.SetTop(childElement, Canvas.GetTop(childElement) + (transformOrigin.Y * deltaVertical * (1 - Math.Cos(-angle))));
Canvas.SetLeft(childElement, Canvas.GetLeft(childElement) - deltaVertical * transformOrigin.Y * Math.Sin(-angle));
childElement.Height -= deltaVertical;
}
private void ResizeY(double e)
{
double deltaVertical = Math.Min(e, childElement.ActualHeight - childElement.MinHeight);
Canvas.SetTop(childElement, Canvas.GetTop(childElement) + deltaVertical * Math.Cos(-angle) + (transformOrigin.Y * deltaVertical * (1 - Math.Cos(-angle))));
Canvas.SetLeft(childElement, Canvas.GetLeft(childElement) + deltaVertical * Math.Sin(-angle) - (transformOrigin.Y * deltaVertical * Math.Sin(-angle)));
childElement.Height -= deltaVertical;
}
public void CreateThumbPart(ref Thumb cornerThumb)
{
cornerThumb = new Thumb { Width = 25, Height = 25, Background= Brushes.Black };
visualChilderns.Add(cornerThumb);
}
public void EnforceSize(FrameworkElement element)
{
if (element.Width.Equals(Double.NaN))
element.Width = element.DesiredSize.Width;
if (element.Height.Equals(Double.NaN))
element.Height = element.DesiredSize.Height;
FrameworkElement parent = element.Parent as FrameworkElement;
if (parent != null)
{
element.MaxHeight = parent.ActualHeight;
element.MaxWidth = parent.ActualWidth;
}
}
protected override Size ArrangeOverride(Size finalSize)
{
base.ArrangeOverride(finalSize);
double desireWidth = AdornedElement.DesiredSize.Width;
double desireHeight = AdornedElement.DesiredSize.Height;
double adornerWidth = this.DesiredSize.Width;
double adornerHeight = this.DesiredSize.Height;
leftTop.Arrange(new Rect(-adornerWidth / 2 - 15, -adornerHeight / 2 - 15, adornerWidth, adornerHeight));
rightTop.Arrange(new Rect(desireWidth - adornerWidth / 2 + 15, -adornerHeight / 2 - 15, adornerWidth, adornerHeight));
leftBottom.Arrange(new Rect(-adornerWidth / 2 - 15, desireHeight - adornerHeight / 2 + 15, adornerWidth, adornerHeight));
rightBottom.Arrange(new Rect(desireWidth - adornerWidth / 2 + 15, desireHeight - adornerHeight / 2 + 15, adornerWidth, adornerHeight));
return finalSize;
}
protected override int VisualChildrenCount => visualChilderns.Count;
protected override Visual GetVisualChild(int index) => visualChilderns[index];
protected override void OnRender(DrawingContext drawingContext) => base.OnRender(drawingContext);
}
}
我至少展示了一个自给自足的例子,这样你就可以准确地看到我在做什么。代码很容易编译。我在代码中标记了两个我被卡住的地方。
谢谢
更新 1:
解决方案
@Frenchy 的解决方案有帮助,但代码仍然存在问题。尝试按住 shift 单击左上角或左下角并立即直接向上拖动,或以匀速向左缓慢拖动右上角。
该问题是由每次调用 DragDelta 时根据最大移动的方向计算比例拖动方向引起的。代码很容易改变方向并感到困惑,甚至不知道它应该以哪种方式正确运行。
解决此问题的一种方法是在比例拖动开始时确定拖动方向,并继续进行直到结束。这似乎效果更好,并解决了上述问题。代码如下。
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
namespace BagControlResize
{
public class TextBlockAdorner : Adorner
{
private double angle = 0.0;
private Point transformOrigin = new Point(0, 0);
private TextBlock childElement;
private VisualCollection visualChilderns;
public Thumb leftTop, rightTop, leftBottom, rightBottom;
private bool dragStarted = false;
private bool isHorizontalDrag = false;
public TextBlockAdorner(UIElement element) : base(element)
{
visualChilderns = new VisualCollection(this);
childElement = element as TextBlock;
CreateThumbPart(ref leftTop);
leftTop.DragDelta += (sender, e) =>
{
double hor = e.HorizontalChange;
double vert = e.VerticalChange;
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
{
if (dragStarted) isHorizontalDrag = Math.Abs(hor) > Math.Abs(vert);
if (isHorizontalDrag) vert = hor; else hor = vert;
}
ResizeX(hor);
ResizeY(vert);
dragStarted = false;
e.Handled = true;
};
CreateThumbPart(ref rightTop);
rightTop.DragDelta += (sender, e) =>
{
double hor = e.HorizontalChange;
double vert = e.VerticalChange;
System.Diagnostics.Debug.WriteLine(hor + "," + vert + "," + (Math.Abs(hor) > Math.Abs(vert)) + "," + childElement.Height + "," + childElement.Width + "," + dragStarted + "," + isHorizontalDrag);
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
{
if (dragStarted) isHorizontalDrag = Math.Abs(hor) > Math.Abs(vert);
if (isHorizontalDrag) vert = -hor; else hor = -vert;
}
ResizeWidth(hor);
ResizeY(vert);
dragStarted = false;
e.Handled = true;
};
CreateThumbPart(ref leftBottom);
leftBottom.DragDelta += (sender, e) =>
{
double hor = e.HorizontalChange;
double vert = e.VerticalChange;
System.Diagnostics.Debug.WriteLine(hor + "," + vert + "," + (Math.Abs(hor) > Math.Abs(vert)) + "," + childElement.Height + "," + childElement.Width + "," + dragStarted + "," + isHorizontalDrag);
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
{
if (dragStarted) isHorizontalDrag = Math.Abs(hor) > Math.Abs(vert);
if (isHorizontalDrag) vert = -hor; else hor = -vert;
}
ResizeX(hor);
ResizeHeight(vert);
dragStarted = false;
e.Handled = true;
};
CreateThumbPart(ref rightBottom);
rightBottom.DragDelta += (sender, e) =>
{
double hor = e.HorizontalChange;
double vert = e.VerticalChange;
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
{
if (dragStarted) isHorizontalDrag = Math.Abs(hor) > Math.Abs(vert);
if (isHorizontalDrag) vert = hor; else hor = vert;
}
ResizeWidth(hor);
ResizeHeight(vert);
dragStarted = false;
e.Handled = true;
};
}
public void CreateThumbPart(ref Thumb cornerThumb)
{
cornerThumb = new Thumb { Width = 25, Height = 25, Background = Brushes.Black };
cornerThumb.DragStarted += (object sender, DragStartedEventArgs e) => dragStarted = true;
visualChilderns.Add(cornerThumb);
}
private void ResizeWidth(double e)
{
double deltaHorizontal = Math.Min(-e, childElement.ActualWidth - childElement.MinWidth);
Canvas.SetTop(childElement, Canvas.GetTop(childElement) - transformOrigin.X * deltaHorizontal * Math.Sin(angle));
Canvas.SetLeft(childElement, Canvas.GetLeft(childElement) + (deltaHorizontal * transformOrigin.X * (1 - Math.Cos(angle))));
childElement.Width -= deltaHorizontal;
}
private void ResizeX(double e)
{
double deltaHorizontal = Math.Min(e, childElement.ActualWidth - childElement.MinWidth);
Canvas.SetTop(childElement, Canvas.GetTop(childElement) + deltaHorizontal * Math.Sin(angle) - transformOrigin.X * deltaHorizontal * Math.Sin(angle));
Canvas.SetLeft(childElement, Canvas.GetLeft(childElement) + deltaHorizontal * Math.Cos(angle) + (transformOrigin.X * deltaHorizontal * (1 - Math.Cos(angle))));
childElement.Width -= deltaHorizontal;
}
private void ResizeHeight(double e)
{
double deltaVertical = Math.Min(-e, childElement.ActualHeight - childElement.MinHeight);
Canvas.SetTop(childElement, Canvas.GetTop(childElement) + (transformOrigin.Y * deltaVertical * (1 - Math.Cos(-angle))));
Canvas.SetLeft(childElement, Canvas.GetLeft(childElement) - deltaVertical * transformOrigin.Y * Math.Sin(-angle));
childElement.Height -= deltaVertical;
}
private void ResizeY(double e)
{
double deltaVertical = Math.Min(e, childElement.ActualHeight - childElement.MinHeight);
Canvas.SetTop(childElement, Canvas.GetTop(childElement) + deltaVertical * Math.Cos(-angle) + (transformOrigin.Y * deltaVertical * (1 - Math.Cos(-angle))));
Canvas.SetLeft(childElement, Canvas.GetLeft(childElement) + deltaVertical * Math.Sin(-angle) - (transformOrigin.Y * deltaVertical * Math.Sin(-angle)));
childElement.Height -= deltaVertical;
}
//public void EnforceSize(FrameworkElement element)
//{
// if (element.Width.Equals(Double.NaN))
// element.Width = element.DesiredSize.Width;
// if (element.Height.Equals(Double.NaN))
// element.Height = element.DesiredSize.Height;
// FrameworkElement parent = element.Parent as FrameworkElement;
// if (parent != null)
// {
// element.MaxHeight = parent.ActualHeight;
// element.MaxWidth = parent.ActualWidth;
// }
//}
protected override Size ArrangeOverride(Size finalSize)
{
base.ArrangeOverride(finalSize);
double desireWidth = AdornedElement.DesiredSize.Width;
double desireHeight = AdornedElement.DesiredSize.Height;
double adornerWidth = this.DesiredSize.Width;
double adornerHeight = this.DesiredSize.Height;
leftTop.Arrange(new Rect(-adornerWidth / 2 - 15, -adornerHeight / 2 - 15, adornerWidth, adornerHeight));
rightTop.Arrange(new Rect(desireWidth - adornerWidth / 2 + 15, -adornerHeight / 2 - 15, adornerWidth, adornerHeight));
leftBottom.Arrange(new Rect(-adornerWidth / 2 - 15, desireHeight - adornerHeight / 2 + 15, adornerWidth, adornerHeight));
rightBottom.Arrange(new Rect(desireWidth - adornerWidth / 2 + 15, desireHeight - adornerHeight / 2 + 15, adornerWidth, adornerHeight));
return finalSize;
}
protected override int VisualChildrenCount => visualChilderns.Count;
protected override Visual GetVisualChild(int index) => visualChilderns[index];
//protected override void OnRender(DrawingContext drawingContext) => base.OnRender(drawingContext);
}
}
推荐阅读
- r - 分组并查找所有数字变量的方法
- prometheus - Prometheus - 给定时间段内的多个平均 HTTP 请求率
- sql - 尝试将父表中的唯一值插入子表时,为什么会出现外键不匹配?
- javascript - 在两个 React 组件之间共享方法的最佳方式是什么?
- html - CSS - 子元素透视父元素
- powershell - 使用 admin$ share 但在 C:\ 中列出文件
- css - 字体在 Chrome 上正确显示,但在 Safari 或 Firefox 上不正确。我什至添加了格式。怎么了?
- html - Hugo 页面未正确呈现
- java - 为什么negativeButton是“不可见的”?
- java - 在函数“isDigit”中 java.lang.ArrayIndexOutOfBoundsException: 5