init
This commit is contained in:
@@ -0,0 +1,186 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace DatabaseExport
|
||||
{
|
||||
public class RegionAdorner : Adorner
|
||||
{
|
||||
public Point GetUpperLeft()
|
||||
{
|
||||
var p = SelectRect.TopLeft;
|
||||
|
||||
var i = AdornedElement as Image;
|
||||
|
||||
var s = new Size(i.Source.Width, i.Source.Height);
|
||||
var difX = (s.Width - _MeasuredSize.Width);
|
||||
var difY = s.Height - _MeasuredSize.Height;
|
||||
var scaleX = difX / _MeasuredSize.Width;
|
||||
var scaleY = difY / _MeasuredSize.Height;
|
||||
p = new Point(p.X + p.X * scaleX, p.Y + p.Y * scaleY);
|
||||
|
||||
|
||||
return p;
|
||||
}
|
||||
public Point GetLowerRight()
|
||||
{
|
||||
var p = SelectRect.BottomRight;
|
||||
|
||||
var i = AdornedElement as Image;
|
||||
if (null == i.Source) { return new Point(0, 0); }
|
||||
var s = new Size(i.Source.Width, i.Source.Height);
|
||||
var difX = (s.Width - _MeasuredSize.Width);
|
||||
var difY = s.Height - _MeasuredSize.Height;
|
||||
var scaleX = difX / _MeasuredSize.Width;
|
||||
var scaleY = difY / _MeasuredSize.Height;
|
||||
|
||||
p = new Point(p.X + p.X * scaleX, p.Y + p.Y * scaleY);
|
||||
return p;
|
||||
}
|
||||
public delegate void RegionSelectedHandler(RegionAdorner r, MouseButtonEventArgs e);
|
||||
public event RegionSelectedHandler OnRegionSelected;
|
||||
|
||||
private bool _bNew = true;
|
||||
public bool IsNew
|
||||
{
|
||||
get => _bNew;
|
||||
set
|
||||
{
|
||||
_bNew = value;
|
||||
if (_bNew)
|
||||
{
|
||||
MyRegion.RegionAddVisibility = Visibility.Visible;
|
||||
MyRegion.RegionDeleteVisibility = Visibility.Hidden;
|
||||
}
|
||||
else
|
||||
{
|
||||
MyRegion.RegionDeleteVisibility = Visibility.Visible;
|
||||
MyRegion.RegionAddVisibility = Visibility.Hidden;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Point __anchorPoint;
|
||||
public Point AnchorPoint
|
||||
{
|
||||
get => __anchorPoint;
|
||||
set => __anchorPoint = value;
|
||||
}
|
||||
private UIElement _adornedElement;
|
||||
private Path _path;
|
||||
private Rect __selectRect;
|
||||
public Rect SelectRect
|
||||
{
|
||||
get => __selectRect;
|
||||
set
|
||||
{
|
||||
__selectRect = value;
|
||||
MyRegion.RegionUpperLeft = GetUpperLeft();
|
||||
MyRegion.RegionBottomRight = GetLowerRight();
|
||||
}
|
||||
}
|
||||
private RectangleGeometry _geometry;
|
||||
|
||||
private Region _region;
|
||||
public Region MyRegion
|
||||
{
|
||||
get => _region;
|
||||
set => _region = value;
|
||||
}
|
||||
public RegionAdorner(UIElement adornedElement, TestObjectTemplate template, Contexts context)
|
||||
: base(adornedElement)
|
||||
{
|
||||
_region = new Region(this, template);
|
||||
_adornedElement = adornedElement;
|
||||
SelectRect = new Rect();
|
||||
_geometry = new RectangleGeometry();
|
||||
_path = new Path();
|
||||
_path.Data = _geometry;
|
||||
_path.StrokeThickness = 5;
|
||||
_path.Stroke = Brushes.AliceBlue;
|
||||
_path.Opacity = .6;
|
||||
_path.Visibility = Visibility.Hidden;
|
||||
MouseLeftButtonUp += new MouseButtonEventHandler(Region_MouseLeftButtonUp);
|
||||
MouseMove += new MouseEventHandler(Region_MouseMove);
|
||||
MouseLeftButtonDown += new MouseButtonEventHandler(Region_MouseLeftButtonDown);
|
||||
_MeasuredSize = adornedElement.RenderSize;
|
||||
//_region.PropertyChanged += new PropertyChangedEventHandler(_region_PropertyChanged);
|
||||
Context = context;
|
||||
}
|
||||
void Region_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
EndSelection(sender, e);
|
||||
}
|
||||
public enum Contexts { EditTestObject, EditTestObjectTemplate };
|
||||
private Contexts _context;
|
||||
public Contexts Context
|
||||
{
|
||||
get => _context;
|
||||
set => _context = value;
|
||||
}
|
||||
void Region_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
if (null != OnRegionSelected) { OnRegionSelected(this, e); }
|
||||
}
|
||||
private Size __MeasuredSize;
|
||||
private Size _MeasuredSize
|
||||
{
|
||||
get => __MeasuredSize;
|
||||
set => __MeasuredSize = value;
|
||||
}
|
||||
private Point p1;
|
||||
public void Region_MouseMove(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (e.LeftButton == MouseButtonState.Pressed)
|
||||
{
|
||||
if (IsNew) { DrawSelection(sender, e, _adornedElement); }
|
||||
else { MoveSelection(sender, e, _adornedElement); }
|
||||
|
||||
var layer = AdornerLayer.GetAdornerLayer(_adornedElement);
|
||||
InvalidateVisual();
|
||||
layer.InvalidateVisual();
|
||||
}
|
||||
}
|
||||
public void DrawSelection(object sender, MouseEventArgs e, UIElement adornedElement)
|
||||
{
|
||||
if (e.LeftButton == MouseButtonState.Pressed)
|
||||
{
|
||||
var mousePosition = e.GetPosition(adornedElement);
|
||||
var r = SelectRect;
|
||||
if (mousePosition.X < AnchorPoint.X) { r.X = mousePosition.X; }
|
||||
else { r.X = AnchorPoint.X; }
|
||||
if (mousePosition.Y < AnchorPoint.Y) { r.Y = mousePosition.Y; }
|
||||
else { r.Y = AnchorPoint.Y; }
|
||||
r.Width = Math.Abs(mousePosition.X - AnchorPoint.X);
|
||||
r.Height = Math.Abs(mousePosition.Y - AnchorPoint.Y);
|
||||
SelectRect = r;
|
||||
_geometry.Rect = SelectRect;
|
||||
}
|
||||
}
|
||||
public void MoveSelection(object sender, MouseEventArgs e, UIElement adornedElement)
|
||||
{
|
||||
if (e.LeftButton == MouseButtonState.Pressed)
|
||||
{
|
||||
var mousePosition = e.GetPosition(adornedElement);
|
||||
var r = SelectRect;
|
||||
r.X -= /*p1.X -*/ (p1.X - mousePosition.X);
|
||||
r.Y -= /*p1.Y -*/(p1.Y - mousePosition.Y);
|
||||
SelectRect = r;
|
||||
p1 = mousePosition;
|
||||
_geometry.Rect = SelectRect;
|
||||
}
|
||||
}
|
||||
public delegate void EndSelectionHandler(RegionAdorner r);
|
||||
public event EndSelectionHandler OnEndSelection;
|
||||
|
||||
public void EndSelection(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
ReleaseMouseCapture();
|
||||
if (null != OnEndSelection) { OnEndSelection(this); }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user