[英]WPF Adorner for Control User resizable
我为 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);
}
}
您在按键按下时的测试不正确,我已修改:当 hor 和 vert 具有相反的符号时,您必须只保留两个测试,其他测试必须丢弃。
rightTop.DragDelta += (sender, e) =>
{
double hor = e.HorizontalChange;
double vert = e.VerticalChange;
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
{
double _max = Math.Abs(hor) > Math.Abs(vert) ? Math.Abs(hor) : Math.Abs(vert);
if( _max >50)return;//if distance of mouse from thumb is > 50pixel, no action
if (hor >= 0 && vert <= 0) // review the tests
{
hor = _max;
vert = -_max;
}
else if(hor <= 0 && vert >=0)
{
hor = -_max;
vert = _max;
}
else
{
return;
}
}
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))
{
double _max = Math.Abs(hor) > Math.Abs(vert) ? Math.Abs(hor) : Math.Abs(vert);
if( _max >50)return;
if (hor <= 0 && vert >= 0) //same things
{
hor = -_max;
vert = _max;
}
else if (hor >= 0 && vert <= 0)
{
hor = _max;
vert = -_max;
}
else
{
return;
}
}
ResizeX(hor);
ResizeHeight(vert);
e.Handled = true;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.