当前位置:网站首页>[new] control move + drag size + animation drag in WPF window
[new] control move + drag size + animation drag in WPF window
2022-06-09 12:59:00 【Shunnet】
I wrote before WPF Controls in the form move + Drag size + Animation drag , But only in Canvas Used in container layout
Now? , New can be added in GRID You can drag in animation
【GRID Realize animation effect in 】
【Canvas animating 】
Serve 【 Be sure to set the width and height of the control to be dynamically dragged 】
/*
Be careful : As long as controls without focus include user controls You can drag and drop the size 【 In the base class 【 Common parameters 】 You can modify it by yourself 】
Usage method [ This is the background code in a form ]:
// Instantiate objects
public DragControlsHelper dragControlsHelper = new DragControlsHelper();
// You can drag and drop by executing the following methods [this Objects belonging to the form , You can create your own layout container by small-scale dragging ]
dragControlsHelper.Insert( Control's object or control's Name, this);
// It is also easy to remove drag size and move
dragControlsHelper.Remove( Control's object or control's Name);
//WPF The layout containers in have 6 The species are as follows :
[Grid] Grid layout , Where the control or container needs to specify the location ;
[StackPanel] Stack panels , The controls are laid out horizontally 、 Vertical layout ;
[DockPanel] Docking panel , Internal controls or containers can be placed on 、 Next 、 Left 、 Right ;
[WrapPanel] It can be regarded as having the function of automatic line feed StackPanel Containers . The form is too small , The control at the end of it will wrap automatically , image Java Flow distribution in ;
[Canvas] Coordinate layout , Coordinate based layout , utilize Canvas.Left,Canvas.Top,Canvas.Right,Canvas.Bottom These four additional properties locate the control coordinates ;
[UniformGrid] Specify the number of rows and columns , Divide the limited container space equally .
//by:Shunnet.top 2022/5/18
*/
/// <summary>
/// Control drag base class
/// </summary>
public class DragControlsBase : Adorner
{
/// <summary>
/// Constructors
/// </summary>
/// <param name="Controls"> Control to drag </param>
/// <param name="LlayoutContainer"> The layout container of the form </param>
public DragControlsBase(UIElement Controls, FrameworkElement LlayoutContainer) : base(Controls)
{
this.Controls = Controls;
this.LlayoutContainer = LlayoutContainer;
InitDragDelta(); // Initialize drag size
InitMove(); // Initialize the move
}
/// <summary>
/// Container border color
/// </summary>
public SolidColorBrush BorderColor = new SolidColorBrush(Colors.Green);
/// <summary>
/// Container border line diameter
/// </summary>
public Thickness BorderWireDiameter = new Thickness(1);
/// <summary>
/// Container border transparency
/// </summary>
public double BorderOpacity = 0;
/// <summary>
/// Drag the color of the inner circle of the decorator
/// </summary>
public SolidColorBrush ThumbInnerColor = new SolidColorBrush(Colors.Red);
/// <summary>
/// Drag the outer ring color of the decorator
/// </summary>
public SolidColorBrush ThumbOuterColor = new SolidColorBrush(Colors.Red);
/// <summary>
/// Decorator wire diameter
/// </summary>
public double ThumbWireDiameter = 1;
/// <summary>
/// Decorator transparency
/// </summary>
public double ThumbOpacity = 0.6;
/// <summary>
/// Drag the smallest width
/// </summary>
public double MinWidth = 100;
/// <summary>
/// Drag the minimum height
/// </summary>
public double MinHeight = 100;
/// <summary>
/// Drag the maximum width
/// </summary>
public double MaxWidth = 500;
/// <summary>
/// Drag the maximum height
/// </summary>
public double MaxHeight = 500;
#region Private field
/// <summary>
/// 4 side
/// </summary>
Thumb LeftThumb, TopThumb, RightThumb, BottomThumb;
/// <summary>
/// 4 Corner
/// </summary>
Thumb LefTopThumb, RightTopThumb, RightBottomThumb, LeftbottomThumb;
/// <summary>
/// middle It is not used at present
/// </summary>
Thumb CentreThumb;
/// <summary>
/// Layout container , If you don't use layout containers , You need to give the above 8 A control layout , The implementation and Grid The layout and positioning are the same , It will be cumbersome and meaningless .
/// </summary>
Grid Llayout;
/// <summary>
/// Control to drag
/// </summary>
UIElement Controls;
/// <summary>
/// The layout container of the form
/// </summary>
FrameworkElement LlayoutContainer;
/// <summary>
/// Is the mouse pressed
/// </summary>
bool IsMouseDown = false;
/// <summary>
/// Mouse down position
/// </summary>
Point MouseDownPosition;
/// <summary>
/// Press the button of the control with the mouse Margin
/// </summary>
Thickness MouseDownMargin;
#endregion
#region Rewriting methods
protected override Visual GetVisualChild(int index)
{
return Llayout;
}
protected override int VisualChildrenCount
{
get
{
return 1;
}
}
protected override Size ArrangeOverride(Size finalSize)
{
// Lay out the container directly , The decorator inside the container will be automatically laid out .
Llayout.Arrange(new Rect(new Point(-LeftThumb.Width / 2, -LeftThumb.Height / 2), new Size(finalSize.Width + LeftThumb.Width, finalSize.Height + LeftThumb.Height)));
return finalSize;
}
#endregion
#region Method
/// <summary>
/// Initialize drag size
/// </summary>
public void InitDragDelta()
{
// Initialize decorator
LeftThumb = new Thumb();
LeftThumb.HorizontalAlignment = HorizontalAlignment.Left;
LeftThumb.VerticalAlignment = VerticalAlignment.Center;
LeftThumb.Cursor = Cursors.SizeWE;
TopThumb = new Thumb();
TopThumb.HorizontalAlignment = HorizontalAlignment.Center;
TopThumb.VerticalAlignment = VerticalAlignment.Top;
TopThumb.Cursor = Cursors.SizeNS;
RightThumb = new Thumb();
RightThumb.HorizontalAlignment = HorizontalAlignment.Right;
RightThumb.VerticalAlignment = VerticalAlignment.Center;
RightThumb.Cursor = Cursors.SizeWE;
BottomThumb = new Thumb();
BottomThumb.HorizontalAlignment = HorizontalAlignment.Center;
BottomThumb.VerticalAlignment = VerticalAlignment.Bottom;
BottomThumb.Cursor = Cursors.SizeNS;
LefTopThumb = new Thumb();
LefTopThumb.HorizontalAlignment = HorizontalAlignment.Left;
LefTopThumb.VerticalAlignment = VerticalAlignment.Top;
LefTopThumb.Cursor = Cursors.SizeNWSE;
RightTopThumb = new Thumb();
RightTopThumb.HorizontalAlignment = HorizontalAlignment.Right;
RightTopThumb.VerticalAlignment = VerticalAlignment.Top;
RightTopThumb.Cursor = Cursors.SizeNESW;
RightBottomThumb = new Thumb();
RightBottomThumb.HorizontalAlignment = HorizontalAlignment.Right;
RightBottomThumb.VerticalAlignment = VerticalAlignment.Bottom;
RightBottomThumb.Cursor = Cursors.SizeNWSE;
LeftbottomThumb = new Thumb();
LeftbottomThumb.HorizontalAlignment = HorizontalAlignment.Left;
LeftbottomThumb.VerticalAlignment = VerticalAlignment.Bottom;
LeftbottomThumb.Cursor = Cursors.SizeNESW;
CentreThumb = new Thumb();
CentreThumb.HorizontalAlignment = HorizontalAlignment.Center;
CentreThumb.VerticalAlignment = VerticalAlignment.Center;
CentreThumb.Cursor = Cursors.SizeAll;
Llayout = new Grid();
// Add a border to the layout container
Border border = new Border();
border.Margin = new Thickness(2);
border.Opacity = BorderOpacity;
border.BorderThickness = BorderWireDiameter;
border.BorderBrush = BorderColor;
Llayout.Children.Add(border);
// Add drag size decorator to layout container
Llayout.Children.Add(LeftThumb);
Llayout.Children.Add(TopThumb);
Llayout.Children.Add(RightThumb);
Llayout.Children.Add(BottomThumb);
Llayout.Children.Add(LefTopThumb);
Llayout.Children.Add(RightTopThumb);
Llayout.Children.Add(RightBottomThumb);
Llayout.Children.Add(LeftbottomThumb);
//Llayout.Children.Add(CentreThumb); // The decorator in the middle Not used temporarily
AddVisualChild(Llayout);
foreach (var item in Llayout.Children)
{
if (item.GetType().Equals(typeof(Thumb)))
{
Thumb thumb = item as Thumb;
thumb.Width = 5; // Set the width of the circle
thumb.Height = 5; // Set the height of the circle
thumb.Opacity = ThumbOpacity;// transparency
thumb.Template = new ControlTemplate(typeof(Thumb)) // Templates
{
VisualTree = GetFactory(ThumbInnerColor, ThumbOuterColor, ThumbWireDiameter)
};
thumb.DragDelta += Control_DragDelta;
}
}
}
/// <summary>
/// Decorator style
/// </summary>
/// <param name="InnerColor"> Inner ring color </param>
/// <param name="OuterColor"> Outer ring color </param>
/// <param name="WireDiameter"> Wire diameter </param>
/// <param name="Opacity"> transparency </param>
/// <returns></returns>
FrameworkElementFactory GetFactory(Brush InnerColor, Brush OuterColor, double WireDiameter)
{
FrameworkElementFactory Element = new FrameworkElementFactory(typeof(Ellipse)); // Draw oval elements
Element.SetValue(Ellipse.FillProperty, InnerColor); // Inner ring color
Element.SetValue(Ellipse.StrokeProperty, OuterColor); // Outer ring color
Element.SetValue(Ellipse.StrokeThicknessProperty, WireDiameter); // Wire diameter
return Element;
}
/// <summary>
/// Initialize the move
/// </summary>
public void InitMove()
{
// Add mobile event
Controls.MouseLeftButtonDown += Control_MouseLeftButtonDown; // Press the left mouse button
Controls.MouseLeftButtonUp += Control_MouseLeftButtonUp; // Release the left mouse button
Controls.MouseMove += Control_MouseMove; // Mouse movement
}
#endregion
#region event
// Drag and drop size logic
private void Control_DragDelta(object sender, DragDeltaEventArgs e)
{
FrameworkElement Control = Controls as FrameworkElement; // Control to drag
FrameworkElement Thumb = sender as FrameworkElement; // Which decoration is dragged
double Left, Top, Right, Bottom, Width, Height; // Left , On , Right , Next , wide , high
if (Thumb.HorizontalAlignment == HorizontalAlignment.Left)
{
Right = Control.Margin.Right;
Left = Control.Margin.Left + e.HorizontalChange;
Width = (double.IsNaN(Control.Width) ? Control.ActualWidth : Control.Width) - e.HorizontalChange;
}
else
{
Left = Control.Margin.Left;
Right = Control.Margin.Right - e.HorizontalChange;
Width = (double.IsNaN(Control.Width) ? Control.ActualWidth : Control.Width) + e.HorizontalChange;
}
if (Thumb.VerticalAlignment == VerticalAlignment.Top)
{
Bottom = Control.Margin.Bottom;
Top = Control.Margin.Top + e.VerticalChange;
Height = (double.IsNaN(Control.Height) ? Control.ActualHeight : Control.Height) - e.VerticalChange;
}
else
{
Top = Control.Margin.Top;
Bottom = Control.Margin.Bottom - e.VerticalChange;
Height = (double.IsNaN(Control.Height) ? Control.ActualHeight : Control.Height) + e.VerticalChange;
}
if (Thumb.HorizontalAlignment != HorizontalAlignment.Center)
{
if (Width >= 0)
{
if (Width >= MinWidth && Width <= MaxWidth)
{
Control.Margin = new Thickness(Left, Control.Margin.Top, Right, Control.Margin.Bottom);
Control.Width = Width;
}
}
}
if (Thumb.VerticalAlignment != VerticalAlignment.Center)
{
if (Height >= 0)
{
if (Height >= MinHeight && Height <= MaxHeight)
{
Control.Margin = new Thickness(Control.Margin.Left, Top, Control.Margin.Right, Bottom);
Control.Height = Height;
}
}
}
}
// Press the left mouse button
private void Control_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var c = sender as FrameworkElement;
IsMouseDown = true;
MouseDownPosition = e.GetPosition(LlayoutContainer);
MouseDownMargin = c.Margin;
c.CaptureMouse();
}
// Release the left mouse button
private void Control_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
var c = sender as FrameworkElement;
IsMouseDown = false;
c.ReleaseMouseCapture();
}
// Mouse movement
private void Control_MouseMove(object sender, MouseEventArgs e)
{
if (IsMouseDown)
{
var c = sender as FrameworkElement;
var pos = e.GetPosition(LlayoutContainer);
var dp = pos - MouseDownPosition;
double Left, Top, Right, Bottom; // Set control coordinates
Left = MouseDownMargin.Left + dp.X;
Top = MouseDownMargin.Top + dp.Y;
Right = MouseDownMargin.Right - dp.X;
Bottom = MouseDownMargin.Bottom - dp.Y;
c.Margin = new Thickness(Left, Top, Right, Bottom);
//GeneralTransform generalTransform = c.TransformToAncestor(LlayoutContainer);
//Point point = generalTransform.Transform(new Point(0, 0));
//// The control of Top left, bottom right
//double ControlLeft = c.Margin.Left; // Left
//double ControlTop = c.Margin.Top; // On
//double ControlRight = point.X + c.Width; // Right
//double ControlBottom = point.Y + c.Height; // Next
}
}
#endregion
}
/// <summary>
/// Control drag implementation class
/// </summary>
public class DragControlsHelper
{
/// <summary>
/// The data dictionary
/// UIElement: Control to drag
/// AdornerLayer: Decorator
/// DragControlsBase: Decorator implementation class
/// </summary>
Dictionary<UIElement, Tuple<AdornerLayer, DragControlsBase>> DictionaryDataList = new Dictionary<UIElement, Tuple<AdornerLayer, DragControlsBase>>();
/// <summary>
/// Add item
/// </summary>
/// <param name="Controls"> Control </param>
/// <param name="LlayoutContainer"> The layout container of the form : This means that whoever packages this control will transfer it , I usually transfer form objects , Forms contain all controls , Small scale drag , Create your own layout container to wrap the controls you want to drag </param>
public void Insert(UIElement Controls, FrameworkElement LlayoutContainer)
{
if (!DictionaryDataList.ContainsKey(Controls))
{
DragControlsBase dragControlsBase = new DragControlsBase(Controls, LlayoutContainer);
AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(Controls);
adornerLayer.Add(dragControlsBase);
Tuple<AdornerLayer, DragControlsBase> tuple = new Tuple<AdornerLayer, DragControlsBase>(adornerLayer, dragControlsBase);
DictionaryDataList.Add(Controls, tuple);
}
}
/// <summary>
/// Remove drag
/// </summary>
/// <param name="Controls"> Control </param>
public void Remove(UIElement Controls)
{
if (DictionaryDataList.ContainsKey(Controls))
{
DictionaryDataList[Controls].Item1.Remove(DictionaryDataList[Controls].Item2); // Remove this attribute
Delete(Controls); // Remove this item from the collection
}
}
/// <summary>
/// Delete this item
/// </summary>
/// <param name="Controls"> Control </param>
private void Delete(UIElement Controls)
{
DictionaryDataList.Remove(Controls); // Remove directly
}
}
/*
Animation drag , Contains the control's move and zoom size
Be careful : Only one layout container can be defined in a single form , This layout container , Cannot set Margin, Fixed width and height cannot be set
by:Shunnet.top 2022/6/8
----------------------------- Here is how to use ---------------------------
*/
#region Back end code
/*
/// <summary>
/// Three in one
/// Only one layout container can be defined in a single form , This layout container , Cannot set Margin, Fixed width and height cannot be set
/// </summary>
DragControlsAnimate dragControlsAnimate;
public MainWindow()
{
InitializeComponent();
dragControlsAnimate = new DragControlsAnimate(this, Pane); // You have to define a container to a container object or Name
dragControlsAnimate.Insert(ConShow1);
dragControlsAnimate.Insert(ConShow2);
dragControlsAnimate.MessageEvenTrigger += MessageEvenTrigger;
dragControlsAnimate.DragEvenTrigger += DragEvenTrigger;
}
/// <summary>
/// news
/// </summary>
/// <param name="Message"> news </param>
/// <param name="element"> Which control displays the message </param>
public void MessageEvenTrigger(string Message, FrameworkElement element)
{
Console.WriteLine($" Control Name:{element.Name}-> Throw a message :{Message}");
}
/// <summary>
/// Remind that the drag event has started , Please pass the button object to be dragged
/// </summary>
/// <param name="element"> On which control the drag is triggered </param>
/// <returns> Return the control object that has been created - Whether to drag the size </returns>
public (FrameworkElement NewControl, bool IsDragAndDragSize) DragEvenTrigger(FrameworkElement ShowControl)
{
FrameworkElement NewControl = new FrameworkElement();
bool IsDragAndDragSize = false;
switch (ShowControl.Name)
{
case "ConShow1":
NewControl = InitControls(0);
IsDragAndDragSize = false;
break;
case "ConShow2":
NewControl = InitControls(1);
IsDragAndDragSize = true;
break;
}
return (NewControl, IsDragAndDragSize);
}
/// <summary>
/// Create icons
/// </summary>
/// <param name="dashboardDataMode"> Icon type </param>
private Label InitControls(int A)
{
return new Label() { Background = new SolidColorBrush(A == 0 ? Colors.AliceBlue : Colors.AntiqueWhite), Width = 100, Height = 100,Content= " Custom control " };
}
*/
#endregion
#region The front-end code
/*
<Window x:Class="WpfApp5.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"
Title="Canvas And Grid Drag animation in + The zoom + Move Shunnet.top" Height="500" Width="800" >
<!--<Canvas Name="Pane" Background="DarkGray">
<Label Content=" This is the use of Canvas Container layout , Only one layout container can be defined in a single form , This layout container , Cannot set Margin, Fixed width and height cannot be set " Foreground="Red" FontWeight="Bold"/>
<Button Content=" Can't drag " Width="90" Height="50" Name="ConShow1" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,30,0,0"/>
<Button Content=" You can drag " Width="90" Height="50" Name="ConShow2" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="100,30,0,0"/>
</Canvas>-->
<Grid Name="Pane" Background="DarkGray">
<Label Content=" This is the use of GRID Container layout , Only one layout container can be defined in a single form , This layout container , Cannot set Margin, Fixed width and height cannot be set " Foreground="Red" FontWeight="Bold"/>
<Button Content=" Can't drag " Width="90" Height="50" Name="ConShow1" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,30,0,0"/>
<Button Content=" You can drag " Width="90" Height="50" Name="ConShow2" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="100,30,0,0"/>
</Grid>
</Window>
*/
#endregion
/// <summary>
/// Drag and drop control animation
/// </summary>
public class DragControlsAnimate
{
/// <summary>
/// Constructors
/// </summary>
/// <param name="Windows"> forms </param>
/// <param name="LlayoutContainer"> Containers : Let the control drag inside </param>
public DragControlsAnimate(FrameworkElement Windows, object LlayoutContainer)
{
this.Windows = Windows;
this.LlayoutContainer = LlayoutContainer;
Windows.SizeChanged += Windwos_SizeChanged;
}
#region Private field
/// <summary>
/// The generated controls on the interface , That is, the collection from which control to drag
/// </summary>
List<FrameworkElement> ShowControlsList = new List<FrameworkElement>();
/// <summary>
/// forms
/// </summary>
FrameworkElement Windows;
/// <summary>
/// Containers : Let the control drag inside
/// </summary>
object LlayoutContainer;
/// <summary>
/// Is the mouse pressed
/// </summary>
bool IsMouseDown = false;
/// <summary>
/// Controls that need to be dragged in real time
/// </summary>
FrameworkElement ControlsObj;
/// <summary>
/// Drag size and move
/// </summary>
DragControlsHelper dragControlsHelper = new DragControlsHelper();
#endregion
#region Method
/// <summary>
/// Add drag size and move
/// </summary>
public void DragSizeInsert(FrameworkElement Controls, FrameworkElement Window)
{
// Create drag and drop size
dragControlsHelper.Insert(Controls, Window);
}
/// <summary>
/// Remove drag size and move
/// </summary>
public void DragSizeRemove(FrameworkElement Controls)
{
// Create drag and drop size
dragControlsHelper.Remove(Controls);
}
/// <summary>
/// Add components to be dragged
/// </summary>
/// <param name="ControlsShow"> The generated controls on the interface </param>
public void Insert(FrameworkElement ControlsShow)
{
if (!ShowControlsList.Contains(ControlsShow)) // Add if not
{
InsertEven(ControlsShow);
ShowControlsList.Add(ControlsShow);
}
}
/// <summary>
/// Remove drag
/// </summary>
/// <param name="ControlsShow"> The generated controls on the interface </param>
public void Remove(FrameworkElement ControlsShow)
{
if (ShowControlsList.Contains(ControlsShow))
{
RemoveEven(ControlsShow);
ShowControlsList.Remove(ControlsShow); // Remove directly
}
}
/// <summary>
/// Create an event
/// </summary>
/// <param name="ControlsShow"> The generated controls on the interface </param>
public void InsertEven(FrameworkElement ControlsShow)
{
//ControlsShow.PreviewMouseLeftButtonDown += delegate (object sender, MouseButtonEventArgs e) { ControlsShow_PreviewMouseLeftButtonDown(sender, e, ControlsObj); };
//ControlsShow.PreviewMouseLeftButtonUp += delegate (object sender, MouseButtonEventArgs e) { ControlsShow_PreviewMouseLeftButtonUp(sender, e, ControlsObj); };
//ControlsShow.PreviewMouseMove += delegate (object sender, MouseEventArgs e) { ControlsShow_PreviewMouseMove(sender, e, ControlsObj); };
ControlsShow.PreviewMouseLeftButtonDown += ControlsShow_PreviewMouseLeftButtonDown;
ControlsShow.PreviewMouseLeftButtonUp += ControlsShow_PreviewMouseLeftButtonUp;
ControlsShow.PreviewMouseMove += ControlsShow_PreviewMouseMove;
}
/// <summary>
/// Remove events
/// </summary>
/// <param name="ControlsShow"> The generated controls on the interface </param>
public void RemoveEven(FrameworkElement ControlsShow)
{
ControlsShow.PreviewMouseLeftButtonDown -= ControlsShow_PreviewMouseLeftButtonDown;
ControlsShow.PreviewMouseLeftButtonUp -= ControlsShow_PreviewMouseLeftButtonUp;
ControlsShow.PreviewMouseMove -= ControlsShow_PreviewMouseMove;
}
#endregion
#region Delegate callback event
/// <summary>
/// Define the entrusted Remind that the drag event has started , Please pass the button object to be dragged
/// </summary>
/// <param name="ShowControl"> On which control the drag is triggered </param>
/// <returns> Return the control object that has been created - Whether to drag the size </returns>
public delegate (FrameworkElement NewControl, bool IsDragAndDragSize) dragEvenTrigger(FrameworkElement ShowControl);
/// <summary>
/// Implement delegation
/// </summary>
public dragEvenTrigger DragEvenTrigger;
/// <summary>
/// Message delegation
/// </summary>
/// <param name="Message"> news </param>
/// <param name="element"> Which control displays the message </param>
public delegate void messageEvenTrigger(string Message, FrameworkElement element);
/// <summary>
/// Implement delegation
/// </summary>
public messageEvenTrigger MessageEvenTrigger;
#endregion
#region Execution event
// Mobile location
private void ControlsShow_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (ControlsObj == null) return;
if (IsMouseDown)
{
if (LlayoutContainer.GetType().Equals(typeof(Canvas)) )
{
Point pos = e.GetPosition(Windows);
Canvas.SetLeft(ControlsObj, pos.X - ControlsObj.Width / 2);
Canvas.SetTop(ControlsObj, pos.Y - ControlsObj.Height / 2);
}
else if (LlayoutContainer.GetType().Equals(typeof(Grid)))
{
Point pos = e.GetPosition(Windows);
double Left = pos.X - ControlsObj.Width / 2;
double Top = pos.Y - ControlsObj.Height / 2;
double Right = Windows.ActualWidth - Left - ControlsObj.Width;
double Bottom = Windows.ActualHeight - Top - ControlsObj.Height;
ControlsObj.Margin = new Thickness(Left, Top, Right, Bottom);
}
}
}
// When the left key of the displayed control is released
private void ControlsShow_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
IsMouseDown = false;
if (ControlsObj == null) return;
ControlsObj.Opacity = 1;
ControlsObj = null;
}
// When left clicking on the displayed control
private void ControlsShow_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (LlayoutContainer.GetType().Equals(typeof(Canvas)))
{
Canvas layout = LlayoutContainer as Canvas;
(FrameworkElement element, bool IsDragAndDragSize) Data = DragEvenTrigger(sender as FrameworkElement);
ControlsObj = Data.element;
if (!layout.Children.Contains(ControlsObj))
{
IsMouseDown = true;
Point Position = e.GetPosition(Windows);
ControlsObj.Opacity = 0.5;
Canvas.SetLeft(ControlsObj, Position.X - ControlsObj.Width / 2);
Canvas.SetTop(ControlsObj, Position.Y - ControlsObj.Height / 2);
layout.Children.Add(ControlsObj);
if (Data.IsDragAndDragSize)
{
// Add drag size and move
DragSizeInsert(ControlsObj, Windows);
}
}
else
{
MessageEvenTrigger(" This control already exists in the layout ", sender as FrameworkElement);
ControlsObj = null;
}
}
else if (LlayoutContainer.GetType().Equals(typeof(Grid)))
{
Grid layout = LlayoutContainer as Grid;
(FrameworkElement element, bool IsDragAndDragSize) Data = DragEvenTrigger(sender as FrameworkElement);
ControlsObj = Data.element;
if (!layout.Children.Contains(ControlsObj))
{
IsMouseDown = true;
Point Position = e.GetPosition(Windows);
ControlsObj.Opacity = 0.5;
double Left = Position.X - ControlsObj.Width / 2;
double Top = Position.Y - ControlsObj.Height / 2;
double Right = Windows.ActualWidth - Left - ControlsObj.Width;
double Bottom = Windows.ActualHeight - Top - ControlsObj.Height;
ControlsObj.Margin = new Thickness(Left, Top, Right, Bottom);
layout.Children.Add(ControlsObj);
if (Data.IsDragAndDragSize)
{
// Add drag size and move
DragSizeInsert(ControlsObj, Windows);
}
}
else
{
MessageEvenTrigger(" This control already exists in the layout ", sender as FrameworkElement);
ControlsObj = null;
}
}
}
// When the window size changes , The layout container should also be changed in size
private void Windwos_SizeChanged(object sender, SizeChangedEventArgs e)
{
FrameworkElement window = sender as FrameworkElement;
if (LlayoutContainer.GetType().Equals(typeof(Canvas)))
{
Canvas layout = LlayoutContainer as Canvas;
layout.Width = window.ActualWidth;
layout.Height = window.ActualHeight;
}
else if (LlayoutContainer.GetType().Equals(typeof(Grid)))
{
Grid layout = LlayoutContainer as Grid;
layout.Width = window.ActualWidth;
layout.Height = window.ActualHeight;
}
}
#endregion
}Copy
Thank you for watching
“ Focus on [ Downstream network ] WeChat official account , Learn more and more interesting real-time information ”
The author of this article :[ Blogger ] dashun
Link to this article :https://shunnet.top/NFzYJb
Copyright notice : Reprint with reference to , thank you
边栏推荐
猜你喜欢
随机推荐
LP流动性挖矿系统开发生态系统详解
kubernetes配置拉取私有镜像仓库
navicat使用说明
[STM32] Hal library CRC verification
View Thread name and set thread name
Comment on "8 reasons why developers don't like low code and no code"
Introduction to ORM framework and common ORM frameworks
What is a hard real-time database system?
【 NativeScript 】
Emergency department: in May, there were 1730 production safety accidents and 1402 deaths nationwide
使 QComboBox 下拉一个树形结构列表
. Net basic knowledge quick pass 8
. Net basic knowledge quick pass 11
【leetcode周赛记录】第294场周赛记录
Stack based buffer overflow for secure encoding
安全编码之基于栈的缓冲区溢出
Drawing of pictures in gdi+
stc8a8k_rgb___LED 888测试代码,还没测试
ep240--leding
勤于奋做国外LEAD的一些习惯








