简体   繁体   English

在Paint事件中,WPF与Windows Forms Region.Xor等效吗?

[英]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. 我正在尝试将此WinForms代码移动到WPF,但是没有Paint事件。

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. 就图形而言,WPF不能像WinForms那样工作。 You can't actually draw shapes, you have to place them into your content. 您实际上无法绘制形状,必须将其放入内容中。

Geometry should serve as a good replacement for Region . Geometry应该可以很好地替代Region You can use Geometry.Combine and specify GeometryCombineMode.Xor to replicate your drawing code. 您可以使用Geometry.Combine并指定GeometryCombineMode.Xor复制您的绘图代码。

RectangleGeometry is how you make rectangles. RectangleGeometry是制作矩形的方式。 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. 要实际显示Geometry ,请将其放在Path ,可以将其用作控件的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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