Files
Future/Future.Contract/Adorners/DragDropAdorner.cs
2025-08-30 17:19:57 +08:00

107 lines
4.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
namespace Future.Contract
{
/// <summary>
/// 拖动辅助装饰器
/// </summary>
public class DragDropAdorner : Adorner
{
#region
private static DragDropAdorner sAdorner; //装饰器:拖放过程中所有效果绘制使用的装饰器对象,结束后会释放
private static AdornerLayer sLayer; //装饰层:用于放置拖放效果装饰器,拖放过程结束后会清除对象引用
private static UIElement sOrigin; //拖起目标:被拖动的源对象
private static UIElement sTarget; //放置目标:在拖动过程中用于放置拖动源的目标,随拖动过程而变化
private static Canvas sCanvas; //拖放画布:整个拖放过程的完整区域,是拖起目标与放置目标的祖辈
private static Border sShadow; //鼠标拖起阴影:用于将托起目标尺寸按照相同缩放比缩放后变成阴影
private static Canvas sEffect; //鼠标拖动效果:用于提示放置的参考点位置,左、上、右、下,中。
private static Dock sPosition; //参考放置位置:为放置目标的位置提供参考,左、上、右、下,中。
/// <summary>
/// 放置的参考点
/// </summary>
public static Dock DropPosition { get => sPosition; }
#endregion
#region
/// <summary>
/// 设定完整拖放效果区域以及被拖动的对象,或是等效对象来起动拖放事件
/// </summary>
/// <param name="area">完整的拖放区域,必须是拖起目标与放置目标的共同祖先节点</param>
/// <param name="origin">被拖动的可视化目标,或是其等效目标</param>
/// <returns></returns>
public static DragDropEffects DoDragDrop(UIElement area, UIElement dragSource, object data, DragDropEffects allowedEffects)
{
DragDropEffects dragDropEffects = allowedEffects;
//TODO准备拖放生成阴影监听拖动执行区域中的拖动事件
dragDropEffects = DragDrop.DoDragDrop(dragSource, data, dragDropEffects);
//TODO拖拽操作完成释放装饰器以及过程中使用的所有资源
sLayer.Remove(sAdorner);
return dragDropEffects;
}
private static void OnDragEnter(UIElement target, DragEventArgs args)
{
sTarget= target;
target.MouseMove += Target_MouseMove;
//TODO更换目标对象监听目标对象的鼠标事件规制“阴影”与“效果”
}
private static void Target_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
//TODO计算鼠标位置移动效果与阴影
}
//public static void OnDragOver(UIElement target, DragEventArgs args)
//{
// //TODO计算鼠标位置移动效果与阴影
//}
public static void OnDragLeave(UIElement target, DragEventArgs args)
{
//TODO清除目标对象清除对鼠标事件的监听清除“阴影”与“效果”
target.MouseMove -= Target_MouseMove;
}
public static void DropCompleted(UIElement target, DragEventArgs args)
{
//TODO清除静态存储区中所使用过的数据清除装饰器
}
#endregion
private readonly VisualCollection mVisuals;
private DragDropAdorner(UIElement adornedElement) : base(adornedElement)
{
mVisuals = new VisualCollection(this);
sCanvas = new Canvas
{
Background=Brushes.Transparent
};
mVisuals.Add(sCanvas);
sLayer = AdornerLayer.GetAdornerLayer(adornedElement);
sLayer?.Add(this);
}
protected override int VisualChildrenCount => mVisuals.Count;
protected override Visual GetVisualChild(int index) => mVisuals[index];
protected override Size ArrangeOverride(Size finalSize)
{
//var bubbleSize = _bubble.DesiredSize;
//var location = new Point(finalSize.Width + 10, (finalSize.Height - bubbleSize.Height) / 2);
//_bubble.Arrange(new Rect(location, DesiredSize));
sCanvas.Arrange(new Rect(new Point(), finalSize));
return finalSize;
}
}
}