Files
MetroGauges-Old/MetroGauges/Controls/DrawTools/RectangleTool.cs
2026-02-23 17:02:55 +08:00

128 lines
3.9 KiB
C#

using MetroGauges.Controls.DrawTools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
namespace MetroGauges.Controls
{
public class RectangleTool : DrawToolBase
{
/// <summary>
/// 是否选择工具
/// </summary>
public bool IsSelecedTool { get; set; }
Rectangle currentRect = new Rectangle();
bool isMouseDown = false;
public RectangleTool(DesignControl canvas) : base(canvas)
{
IsStartDraw = true;
}
public override void MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
isMouseDown = true;
if (IsStartDraw)
{
this.DrawDesign.DisabledMove = true;
Point mousePosition = Mouse.GetPosition(this.DrawDesign.canvasDesign);
currentRect.Stroke = new SolidColorBrush(Colors.White);
currentRect.StrokeThickness = 1;
Canvas.SetLeft(currentRect, mousePosition.X);
Canvas.SetTop(currentRect, mousePosition.Y);
currentRect.DataContext = new Point() { X = mousePosition.X, Y = mousePosition.Y };
this.AddShap(currentRect);
}
}
public override void MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
isMouseDown = false;
IsStartDraw = false;
this.DrawDesign.DisabledMove = false;
if (currentRect.Width == 0 || currentRect.Height == 0)
{
this.DrawDesign.canvasDesign.Children.Remove(currentRect);
}
else
{
if (IsSelecedTool)
{
this.DrawDesign.canvasDesign.Children.Remove(currentRect);
this.DrawDesign.DrawShaps.Remove(currentRect);
}
}
}
public override void MouseMove(object sender, MouseEventArgs e)
{
if (IsStartDraw && isMouseDown)
{
Point mousePosition = Mouse.GetPosition(this.DrawDesign.canvasDesign);
Point sourePoint = (Point)currentRect.DataContext;
currentRect.Width = Math.Abs(mousePosition.X - sourePoint.X);
currentRect.Height = Math.Abs(mousePosition.Y - sourePoint.Y);
if (mousePosition.X - sourePoint.X < 0) // 左边
{
if (mousePosition.Y - sourePoint.Y < 0) //上面
{
Canvas.SetLeft(currentRect, sourePoint.X - currentRect.Width);
Canvas.SetTop(currentRect, sourePoint.Y + (mousePosition.Y - sourePoint.Y));
}
else //下面
{
Canvas.SetLeft(currentRect, sourePoint.X - currentRect.Width);
}
}
else // 右边
{
if (mousePosition.Y - sourePoint.Y < 0) //上面
{
Canvas.SetTop(currentRect, sourePoint.Y+ (mousePosition.Y- sourePoint.Y));
}
else //下面
{
}
}
}
}
public override void MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
}
public override void m_Root_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
}
public override void Highlight(FrameworkElement shape)
{
this.DrawDesign.CrrentDrawTool = this;
base.Highlight(shape);
}
}
}