WPF DrawingContext在我的对象后面绘制

时间:2012-12-20 18:47:09

标签: c# wpf drawingcontext

我有一个带有一些图像的画布,我也在使用DrawGeometry来绘制一个在时间过去时填充的圆圈。

这就是我在DrawingContext中绘制圆圈的方法:

protected override void OnRender(DrawingContext drawingContext)
{
    base.OnRender(drawingContext);
    MyUtils.RenderProgressClock(drawingContext, clockPosition, 50, gameTime / totalTime);
}

并调用InvalidateVisual();叫它。

但是这样做我的圈子就在我的画面背后,我无法看到它,我怎么能在它们面前画出来?

我是WPF的新手,它给了我一个艰难的时间......

这是所要求的其他方法代码:

 private static PathGeometry GetClockGeometry(Point position, double percentage, double radius)
        {
            const double innerFactor = 0.90;
            double innerRadius = radius * innerFactor;

            PathGeometry pie = new PathGeometry();

            PathFigure pathFigure = new PathFigure();
            pathFigure.StartPoint = new Point(0, -innerRadius);
            pathFigure.IsClosed = true;

            if (percentage > kMaxClockPercentage)
            {
                percentage = kMaxClockPercentage;
            }
            double angle = 360.0 * percentage;

            // Starting Point
            LineSegment inOutLine = new LineSegment(new Point(0, -radius), true);

            // Arc
            ArcSegment outerArc = new ArcSegment();

            outerArc.IsLargeArc = angle >= 180.0;
            outerArc.Point = new Point(Math.Cos((angle - 90) * Math.PI / 180.0) * radius, Math.Sin((angle - 90) * Math.PI / 180.0) * radius);
            outerArc.Size = new Size(radius, radius);
            outerArc.SweepDirection = SweepDirection.Clockwise;

            LineSegment outInLine = new LineSegment(new Point(outerArc.Point.X * innerFactor, outerArc.Point.Y * innerFactor), true);

            ArcSegment innerArc = new ArcSegment();
            innerArc.IsLargeArc = angle >= 180.0;
            innerArc.Point = pathFigure.StartPoint;
            innerArc.Size = new Size(innerRadius, innerRadius);
            innerArc.SweepDirection = SweepDirection.Counterclockwise;

            pathFigure.Segments.Add(inOutLine);
            pathFigure.Segments.Add(outerArc);
            pathFigure.Segments.Add(outInLine);
            pathFigure.Segments.Add(innerArc);

            pie.Transform = new TranslateTransform(position.X, position.Y);
            pie.Figures.Add(pathFigure);

            return pie;
        }

2 个答案:

答案 0 :(得分:1)

好的,既然我现在了解的情况好一点,我看到我的初步答案不会直接对你有用。但是,我也发现你有一点问题。

OnRender工作方式的一般性质意味着您绘制的内容总是会在图像后面以及添加到窗口中的内容结束。

除此之外,您将所有这些特定功能(进度时钟)的绘图代码放入窗口本身,这个解决方案感觉有些偏差。

您可能想要探索一些替代方案。

一个简单的方法是创建一个UserControl来绘制Clock。 UserControl可能具有应该填充的%的DependencyProperty。您可以在UserControl中使用(大致)相同的OnRender代码,或者您可以使用其他一些奇特的方式(我确信在所有XAML中都有一些方法可以做到这一点,尽管我不知道它在我的头脑中)。然后你就像把所有其他图像/控件一样放进窗口。

你也可以创建一个CustomControl,虽然这需要更多关于WPF和资源的知识,以及如何理解它是如何工作的。由于你是WPF的新手,现在可能有点儿了。

答案 1 :(得分:1)

您需要向我们展示如何将圆圈添加到“画布”中。

WPF是一个保留的绘图系统,因此控件在其可视树中显示的顺序决定了它们的堆叠顺序。OnRender()实际上意味着AccumulateDrawingObjects(),因为它不直接绘制,它只是创建一组要绘制的对象。

此外,如果对象保持相同的大小,则不需要InvalidateVisual(),因为它会导致非常昂贵的重新布局。

更有效的重新呈现方法是使用带有AffectsRender的DependencyProperty ...或创建DrawingGroup,在DrawingContext期间将其添加到OnRender(),然后,您可以随时DrawingGroup.Open()更改DrawingGroup中的绘图命令。

相关问题