Java教程

可视化对象

本文主要是介绍可视化对象,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

比几何和路径消耗的资源更小,适合大量。

 

drawingContext.DrawRectangle();
drawingContext.DrawText();
drawingContext.DrawEllipse();
drawingContext.DrawGeometry();
drawingContext.DrawRoundedRectangle();
drawingContext.DrawImage();

  • 定义可视化对象
    DrawingVisual visual = new DrawingVisual();
    using (DrawingContext drawingContext = visual.RenderOpen())
    {
    Pen pen = new Pen(Brushes.Black, 3);
    drawingContext.DrawLine(pen, new Point(0, 50), new Point(50, 0));
    drawingContext.DrawLine(pen, new Point(50, 0), new Point(100, 50));
    drawingContext.DrawLine(pen, new Point(0, 50), new Point(100, 50));
    }
  • 借助元素,将可视化对象添加到元素树中
    •  AddVisualChild() 和 AddLogicalChild() 注册
    • 重写GetVisualChild 方法


绘制可视化对象

class MyPanel: Panel
    {
        private List<Visual> visuals = new List<Visual>();
        protected override Visual GetVisualChild(int index)
        {
            return visuals[index];
        }

        protected override int VisualChildrenCount => visuals.Count;


        public void AddVisual(Visual visual)
        {
            visuals.Add(visual);
            base.AddLogicalChild(visual);
            base.AddVisualChild(visual);

        }
    }

 

元素中包含可视化对象

 private void MyPanel_Loaded(object sender, RoutedEventArgs e)
        {
            DrawingVisual visual = new DrawingVisual();
            using (DrawingContext drawingContext = visual.RenderOpen())
            {
                Brush brush = Brushes.LightGreen;
                Pen pen = new Pen(Brushes.SteelBlue, 3);

                drawingContext.DrawLine(pen, new Point(0, 50), new Point(50, 0));
                drawingContext.DrawLine(pen, new Point(50, 0), new Point(100, 50));
                drawingContext.DrawLine(pen, new Point(0, 50), new Point(100, 50));

                drawingContext.DrawRectangle(brush, pen, new Rect(new Point(100, 100), new Size(50, 100)));

                drawingContext.DrawEllipse(brush,pen,new Point(300,200),100,60);
            }
            MyPanel.AddVisual(visual);
        }

 

这篇关于可视化对象的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!