WPF InkCanvas甚至在Clip边界之外的空间上绘制。覆盖命中测试

时间:2018-08-06 14:36:01

标签: wpf drawing hittest inkcanvas

我正在使用InkCanvas.Clip属性将我的InkCanvas裁剪为所需的形状(为此我使用了PathGeometry)。我只能在此区域开始绘图,但是我希望能够在该区域之外开始绘图。当然,超出此区域的笔触零件将不会显示。 我怎样才能做到这一点?我考虑过覆盖InkCanvas的HitTestCore,也许默认情况下,HitTest在我允许的绘图区域之外失败

1 个答案:

答案 0 :(得分:1)

也许OpacityMask可以实现您想要的目标:

  1. 将笔划剪到外面
  2. 开始在外部绘画并进入内部。

<Grid Background="LightSalmon">
    <InkCanvas Background="LightBlue">
        <InkCanvas.OpacityMask>
            <DrawingBrush>
                <DrawingBrush.Drawing>
                    <GeometryDrawing>
                        <GeometryDrawing.Brush>
                            <SolidColorBrush Color="Black"/>
                        </GeometryDrawing.Brush>
                        <GeometryDrawing.Geometry>
                            <PathGeometry>
                                <PathGeometry.Figures>
                                    <PathFigure StartPoint="0,0">
                                        <PathFigure.Segments>
                                            <PolyLineSegment Points="100,0 100,100 0,100 0,0"/>
                                            <PolyLineSegment Points="100,0 100,100 0,100 0,0"/>
                                            <PolyLineSegment Points="30,30 70,30 70,70 30,70 30,30"/>
                                        </PathFigure.Segments>
                                    </PathFigure>
                                </PathGeometry.Figures>
                            </PathGeometry>
                        </GeometryDrawing.Geometry>
                    </GeometryDrawing>
                </DrawingBrush.Drawing>
            </DrawingBrush>
        </InkCanvas.OpacityMask>
    </InkCanvas>
</Grid>

已添加:

为避免如@SaintMSent所述发生变化,我们应启用InkCanvas的AdornerDecorator的ClipToBounds属性:

<InkCanvas x:Name="MyInkCanvas" Background="LightBlue" Loaded="MyInkCanvas_OnLoaded">
    ......
</InkCanvas>

隐藏代码:

private void MyInkCanvas_OnLoaded(object sender, RoutedEventArgs e)
{
    var adornerDecorator = VisualTreeHelper.GetChild(MyInkCanvas, 0) as AdornerDecorator;
    if (adornerDecorator != null)
    {
        adornerDecorator.ClipToBounds = true;
    }
}