【问题标题】:shadow on geometry drawn by drawcontext in wpfwpf中drawcontext绘制的几何图形上的阴影
【发布时间】:2013-08-19 15:21:15
【问题描述】:

我正在使用以下代码使用绘图上下文在文本框上渲染一个矩形。

            drawingContext.DrawRoundedRectangle(
                new SolidColorBrush(Color.FromRgb(255, 246, 178)), null,
                new Rect(new Point(rect.TopRight.X + 20, rect.TopRight.Y),
                         new Size(130, rect.Height)),
                         3,
                         3);

我想在这个矩形上渲染一个阴影,我在 WPF 中以编程方式绘制。我该怎么做?

【问题讨论】:

    标签: wpf drawingcontext


    【解决方案1】:

    为视觉添加效果

    试试这样的

    public class MyControl: Control
    {
        private Rect rect = new Rect(100, 100, 200, 200);
    
        protected override void OnRender(DrawingContext drawingContext)
        {
            var r = new Rect(new Point(rect.TopRight.X + 20, rect.TopRight.Y),
                             new Size(130, rect.Height));
            var brush = new SolidColorBrush(Color.FromRgb(255, 246, 178));
    
            DropShadowEffect effect = new DropShadowEffect();
            effect = new DropShadowEffect {Color = Colors.Gainsboro, Direction = 30};
            this.Effect = effect;
    
            drawingContext.DrawRoundedRectangle(brush, null, r, 3, 3);
            base.OnRender(drawingContext);
        }
    

    }

    这给了我:

    编辑

    如果你没有 UI 元素来附加 Effect,那么你需要自己做阴影。 只需在主图下方添加另一个矩形,并使用一些渐变画笔变得透明。

     protected override void OnRender(DrawingContext drawingContext)
        {
            var r = new Rect(new Point(rect.TopRight.X + 20, rect.TopRight.Y),
                             new Size(130, rect.Height));
            var r2 = new Rect(new Point(rect.TopRight.X + 25, rect.TopRight.Y+5),
                             new Size(130, rect.Height));
    
            var brush = new SolidColorBrush(Color.FromRgb(255, 246, 178));
    
            var gradientBrush = new LinearGradientBrush(Colors.Black, Colors.Gray, 30);
    
            drawingContext.DrawRoundedRectangle(gradientBrush, null, r2, 3, 3);
            drawingContext.DrawRoundedRectangle(brush, null, r, 3, 3);
    
            base.OnRender(drawingContext);
        }
    

    这会给你这样的东西

    【讨论】:

    • 感谢塞巴斯蒂安的回答。我认为我的问题是不完整的。您的答案有效,因为渲染发生在具有 Effect 字段的 UIElement 上。就我而言,我在文本框上渲染这个矩形。 (基本上是 AvalonEdit 上的背景渲染器)。所以它没有 Effect 字段。那我该如何显示这个控件呢?有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-22
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多