我希望在从矩形或其中间的任何角落第一次绘制后调整矩形的大小。我正在使用 Geometry 从下面的代码中绘制矩形。
private void UpdateMask(Point p)
{
double left = Math.Min(origin.X, p.X);
double right = Math.Max(origin.X, p.X);
double top = Math.Min(origin.Y, p.Y);
double bottom = Math.Max(origin.Y, p.Y);
rUpar = new Rect(0, 0, panel.ActualWidth, panel.ActualHeight);
rNichy = new Rect(left, top, right - left, bottom - top);
mask.Data = new CombinedGeometry(GeometryCombineMode.Exclude,
new RectangleGeometry(rUpar),
new RectangleGeometry(rNichy));
}
private void PanelMouseMove(object sender, MouseEventArgs e)
{
UpdateMask(e.GetPosition(panel));
var pos = e.GetPosition(panel); // Panel is Grid
x = Math.Min(pos.X, origin.X);
y = Math.Min(pos.Y, origin.Y);
var w = Math.Max(pos.X, origin.X) - x;
var h = Math.Max(pos.Y, origin.Y) - y;
}
private void PanelMouseDown(object sender, MouseButtonEventArgs e)
{ panel.CaptureMouse();
origin = e.GetPosition(panel);
UpdateMask(origin);
}
private void PanelMouseUp(object sender, MouseButtonEventArgs e)
{
panel.ReleaseMouseCapture();
}
WPF: -
<Grid>
<Canvas x:Name="panel" MouseDown="PanelMouseDown" MouseUp="PanelMouseUp" MouseMove="PanelMouseMove">
<Image Name="img" Source="..." />
<Path x:Name="mask" Fill="#99271F1F" />
</Canvas>
</Grid>
我从中得到的是: - 任何用于更新Geometry以调整大小或任何其他方法的方法。