210 lines
5.7 KiB
C#
210 lines
5.7 KiB
C#
|
|
using MetroGauges.Controls.BlockShaps;
|
|||
|
|
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.DrawTools
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
public enum DrawActionType
|
|||
|
|
{
|
|||
|
|
Pointer,
|
|||
|
|
Line,
|
|||
|
|
Polyline,
|
|||
|
|
Circle,
|
|||
|
|
Rectangle,
|
|||
|
|
Select,
|
|||
|
|
Custom,
|
|||
|
|
Text
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public abstract class DrawToolBase
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
private FrameworkElement selectedShape;
|
|||
|
|
public event EventHandler<HighlighEvent> HighlighEvent;
|
|||
|
|
|
|||
|
|
|
|||
|
|
protected bool IsStartDraw { get; set; }
|
|||
|
|
|
|||
|
|
public DesignControl DrawDesign { get; set; }
|
|||
|
|
|
|||
|
|
|
|||
|
|
public FrameworkElement SelectedShape
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return selectedShape;
|
|||
|
|
}
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
selectedShape = value;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public DrawToolBase(DesignControl canvas)
|
|||
|
|
{
|
|||
|
|
this.DrawDesign = canvas;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public abstract void MouseLeftButtonUp(object sender, MouseButtonEventArgs e);
|
|||
|
|
|
|||
|
|
public abstract void MouseRightButtonUp(object sender, MouseButtonEventArgs e);
|
|||
|
|
|
|||
|
|
public abstract void m_Root_MouseRightButtonDown(object sender, MouseButtonEventArgs e);
|
|||
|
|
|
|||
|
|
public abstract void MouseLeftButtonDown(object sender, MouseButtonEventArgs e);
|
|||
|
|
|
|||
|
|
public abstract void MouseMove(object sender, MouseEventArgs e);
|
|||
|
|
|
|||
|
|
protected void AddShap(FrameworkElement element)
|
|||
|
|
{
|
|||
|
|
element.MouseLeftButtonDown += Element_MouseLeftButtonDown;
|
|||
|
|
element.MouseMove += Element_MouseMove;
|
|||
|
|
element.MouseLeftButtonUp += Element_MouseLeftButtonUp;
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
element.Cursor = Cursors.Hand;
|
|||
|
|
this.DrawDesign.canvasDesign.Children.Add(element);
|
|||
|
|
this.DrawDesign.DrawShaps.Add(element);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#region 图形移动
|
|||
|
|
|
|||
|
|
private System.Windows.Point dragPoint;
|
|||
|
|
System.Windows.Point offset;
|
|||
|
|
bool isInDrag = false;
|
|||
|
|
public void Element_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
|||
|
|
{
|
|||
|
|
if (!IsStartDraw)
|
|||
|
|
{
|
|||
|
|
FrameworkElement element = sender as FrameworkElement;
|
|||
|
|
offset = e.GetPosition(element);
|
|||
|
|
element.CaptureMouse();
|
|||
|
|
isInDrag = true;
|
|||
|
|
|
|||
|
|
dragPoint = e.GetPosition(this.DrawDesign.canvasDesign);
|
|||
|
|
|
|||
|
|
//this.CopyBlock(element.Name);
|
|||
|
|
|
|||
|
|
this.Highlight(element);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//MessageBox.Show($"X:{offset.X}Y:{offset.Y}");
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void Element_MouseMove(object sender, MouseEventArgs e)
|
|||
|
|
{
|
|||
|
|
if (!IsStartDraw)
|
|||
|
|
{
|
|||
|
|
if (isInDrag)
|
|||
|
|
{
|
|||
|
|
FrameworkElement element = sender as FrameworkElement;
|
|||
|
|
System.Windows.Point currentPoint = e.GetPosition(this.DrawDesign.canvasDesign);
|
|||
|
|
|
|||
|
|
double moveX = currentPoint.X - dragPoint.X;
|
|||
|
|
double moveY = currentPoint.Y - dragPoint.Y;
|
|||
|
|
|
|||
|
|
this.MoveElement(moveX, moveY);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
protected virtual void MoveElement( double moveX,double moveY )
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void Element_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
|
|||
|
|
{
|
|||
|
|
if (!IsStartDraw)
|
|||
|
|
{
|
|||
|
|
if (isInDrag)
|
|||
|
|
{
|
|||
|
|
FrameworkElement element = sender as FrameworkElement;
|
|||
|
|
element.ReleaseMouseCapture();
|
|||
|
|
isInDrag = false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
//高亮选择
|
|||
|
|
public virtual void Highlight(FrameworkElement shape)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
foreach (var item in this.DrawDesign.DrawShaps)
|
|||
|
|
{
|
|||
|
|
if (item is Shape)
|
|||
|
|
{
|
|||
|
|
(item as Shape).Stroke = Brushes.White;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
(item as TextBlock).Foreground = new SolidColorBrush(Colors.White);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
if (shape is Shape)
|
|||
|
|
{
|
|||
|
|
(shape as Shape).Stroke = Brushes.Red;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
(shape as TextBlock).Foreground = new SolidColorBrush(Colors.Red);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
this.selectedShape = shape;
|
|||
|
|
|
|||
|
|
if (HighlighEvent != null)
|
|||
|
|
{
|
|||
|
|
HighlighEvent(this, null);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void MoveSelectedShapeByX(double x)
|
|||
|
|
{
|
|||
|
|
if (this.selectedShape != null)
|
|||
|
|
{
|
|||
|
|
BlockOpr opr = new BlockOpr() { ScaleRale = this.DrawDesign.ScaleRale };
|
|||
|
|
opr.currentPoint = new Point() { X = x };
|
|||
|
|
List<FrameworkElement> blocks = new List<FrameworkElement>();
|
|||
|
|
blocks.Add(this.selectedShape);
|
|||
|
|
opr.ExctecOpr(blocks, OprType.MoveByX);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void MoveSelectedShapeByY(double y)
|
|||
|
|
{
|
|||
|
|
if (this.selectedShape != null)
|
|||
|
|
{
|
|||
|
|
BlockOpr opr = new BlockOpr() { ScaleRale = this.DrawDesign.ScaleRale };
|
|||
|
|
opr.currentPoint = new Point() { Y = y };
|
|||
|
|
List<FrameworkElement> blocks = new List<FrameworkElement>();
|
|||
|
|
blocks.Add(this.selectedShape);
|
|||
|
|
opr.ExctecOpr(blocks, OprType.MoveByY);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
public virtual void Deactivated()
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|