简体   繁体   中英

What is WPF equivalent of Windows Forms Region.Xor in Paint event?

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);
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM