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

点点圈 提交于 2019-12-13 04:35:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!