绘制矩形时要移到右侧
当我在左侧绘制矩形时,它会变成这样
这是我的代码:
ObservableCollection<RectItem> RectItems = new ObservableCollection<RectItem>();
public void MouseDownEvent(object sender, MouseButtonEventArgs e)
{
startPoint = Mouse.GetPosition((IInputElement)sender);
}
public void MouseMoveEvent(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Released) return;if (selected) return;if (_isDragging) return;var pos = Mouse.GetPosition((IInputElement)sender);
// Set the position of rectangle
var x = Math.Min(pos.X, startPoint.X);
var y = Math.Min(pos.Y, startPoint.Y);
// Set the dimenssion of the rectangle
var w = Math.Max(pos.X, startPoint.X) - x;
var h = Math.Max(pos.Y, startPoint.Y) - y;
if (RectItems.Count != 0)
{
var exist = RectItems.Where(xx => xx.X == x).LastOrDefault();
if (exist != null)
{
exist.Height = h;
exist.Width = w;
}
else
{
RectItems.Add(new RectItem
{
X = x,
Y = y,
Height = h,
Width = w,
id = RectItems.Count() + 1,
});
}
}
else
{
RectItems.Add(new RectItem
{
X = x,
Y = y,
Height = h,
Width = w,
id = RectItems.Count() + 1,
});
}
} public void PreviewMouseLeftButtonUp(object sender,MouseButtonEventArgs e) { StopDragging(); e.Handled = true; }