问题
I'm trying to move this WinForms code to WPF, but there is no Paint
event.
private void OnPaint(object sender, PaintEventArgs e)
{
var region = new Region(new Rectangle(0, 0, this.Width, this.Height));
var rectangle = new Rectangle(0, 0, 50, 50);
region.Xor(rectangle);
e.Graphics.FillRegion(Brushes.Black, region);
}
回答1:
WPF doesn't work like WinForms in terms of graphics. You can't actually draw shapes, you have to place them into your content.
Geometry should serve as a good replacement for Region
. You can use Geometry.Combine and specify GeometryCombineMode.Xor to replicate your drawing code.
RectangleGeometry is how you make rectangles. There are similar classes for other shapes.
To actually display the Geometry
, put it in a Path, which can be used as a control's content.
来源:https://stackoverflow.com/questions/18667903/what-is-wpf-equivalent-of-windows-forms-region-xor-in-paint-event