gpt4 book ai didi

c# - 可调整大小的控制用户的 WPF Adorner

转载 作者:行者123 更新时间:2023-12-04 01:35:14 28 4
gpt4 key购买 nike

我为 TextBlock 元素创建了一个简单的装饰器,允许用户更改其大小。您可以更改 block 的四个角的大小,即一次更改两个大小(左上拇指、右上拇指、左下拇指、右下拇指)。一切正常,您可以在第一个 gif 上看到它:

enter image description here

另外,我想添加按住 Shift 键按比例调整大小的功能。你可以在第二个 gif 中看到这个结果:

enter image description here

如您所见,左上拇指和右下拇指可让您正确更改尺寸。但是,其他两个拇指元素不起作用,我不明白是怎么做到的。

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:

enter image description here

最佳答案

@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);
}
}

关于c# - 可调整大小的控制用户的 WPF Adorner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59803689/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com