Visual Layer Overview(6)Using DrawingVisual Objects

本文介绍如何使用DrawingVisual类渲染图形,包括创建DrawingVisual对象、设置其描画内容以及如何通过FrameworkElement类实现布局与事件处理。
部署运行你感兴趣的模型镜像
这部分提供了怎样使用DrawingVisual对象的概要说明
1.        DrawingVisual Object
DrawingVisual 是一个用来渲染 shapes, images, 或者 text 的轻量级的类这个类之所以被认为是轻量级的,是因为它没有提供 layout 或者 event handling ,所以增加了它的运行时性能。正是因为这个原因,描画 backgrounds clip art 是非常理想的。
为了使用 DrawingVisual 对象,需要创建一个对象的 host container host container 对象必须从提供了 layout event handling FrameworkElement class 继承,而 DrawingVisual 对象没有提供 layout event handling host container 对象不提供任何的显示属性,因为它的主要目的是包含其他的子对象。
当为 visual objects 创建 host container 对象时,需要保存 visual objects 对象的引用到一个 VisualCollection 中,使用 Add 方法添加,参见下面的例子:
// Create a host visual derived from the FrameworkElement class.
// This class provides layout, event handling, and container support for
// the child visual objects.
public class MyVisualHost : FrameworkElement
{
    // Create a collection of child visual objects.
    private VisualCollection _children;
    public MyVisualHost()
    {
        _children = new VisualCollection(this);
        _children.Add(CreateDrawingVisualRectangle());
        _children.Add(CreateDrawingVisualText());
        _children.Add(CreateDrawingVisualEllipses());
        this.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(MyVisualHost_MouseLeftButtonUp);
    }
3.        Creating DrawingVisual Objects
当创建 DrawingVisual 对象时,它不包含任何描画内容。通过得到对象的描画上下文,可以添加 text, graphic, image 内容。对象的描画上下文调用 DrawingVisual 对象的 RenderOpen 方法可以得到。
在描画上下文中描画矩形,使用 DrawingContext 对象的 DrawRectangle 方法,当结束 DrawingContext 的描画内容时,调用 Close 方法关闭描画上下文中
下面的例子中, DrawingVisual 对象被创建,在它的描画上下文中绘制了一个矩形
// Create a DrawingVisual that contains a rectangle.
private DrawingVisual CreateDrawingVisualRectangle()
{
    DrawingVisual dv = new DrawingVisual();
    DrawingContext dc = dv.RenderOpen();
    Rect r = new Rect(new Point(160, 100), new Size(320, 80));
    dc.DrawRectangle(Brushes.LightBlue, (Pen)null, r);
    dc.Close();
    return dv;
}
4.        Creating Overrides for FrameworkElement Members
Host container 对象负责管理 visual objects 的集合。这需要 Host container 实现( overrides )从继承 FrameworkElement class 的一些方法
下面列出了必须override的四个方法
·                       ArrangeOverride: Positions the child element and determines size.
·                       GetVisualChild: Returns a child at the specified index from the collection of child elements.
·                       MeasureOverride: Measures and determines the size in layout required for the child element.
·                       VisualChildrenCount: Gets the number of visual child elements within this element.
例子代码:
//<Snippet102a>
// Provide a required override for the VisualChildCount property.
protected override int VisualChildrenCount
{
    get { return _children.Count; }
}
 
// Provide a required override for the GetVisualChild method.
protected override Visual GetVisualChild(int index)
{
    if (index < 0 || index > _children.Count)
    {
        throw new ArgumentOutOfRangeException();
    }
 
    return (Visual)_children[index];
}
 
// Provide a required override for the MeasureOverride method.
protected override Size MeasureOverride(Size availableSize)
{
    // Return the value of the parameter.
    return base.MeasureOverride(availableSize);
}
 
// Provide a required override for the ArrangeOverride method.
protected override Size ArrangeOverride(Size finalSize)
{
    // Return the value of the parameter.
    return base.ArrangeOverride(finalSize);
}
5.        Providing Hit Testing Support
Host container 对象虽然没有提供显示的属性,但是提供了 event handling 。这使得可以创建 event handling routine 来处理鼠标事件,例如: r elease 鼠标左键, event handling routine 还可以通过调用 HitTest 方法实现碰撞检测
示例代码:
// Capture the mouse event and hit test the coordinate point value against
// the child visual objects.
void MyVisualHost_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    // Retreive the coordinates of the mouse button event.
    Point pt = e.GetPosition((UIElement)sender);
 
    // Initiate the hit test by setting up a hit test result callback method.
    VisualTreeHelper.HitTest(this, null, new HitTestResultCallback(myCallback), new PointHitTestParameters(pt));
}
 
// If a child visual object is hit, toggle its opacity to visually indicate a hit.
public HitTestResultBehavior myCallback(HitTestResult result)
{
    if (result.VisualHit.GetType() == typeof(DrawingVisual))
    {
        if (((DrawingVisual)result.VisualHit).Opacity == 1.0)
        {
            ((DrawingVisual)result.VisualHit).Opacity = 0.4;
        }
        else
        {
            ((DrawingVisual)result.VisualHit).Opacity = 1.0;
        }
    }
 
    // Stop the hit test enumeration of objects in the visual tree.
    return HitTestResultBehavior.Stop;
}
 

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究(Matlab代码实现)内容概要:本文围绕“基于数据驱动的Koopman算子的递归神经网络模型线性化”展开,旨在研究纳米定位系统的预测控制问题,并提供完整的Matlab代码实现。文章结合数据驱动方法与Koopman算子理论,利用递归神经网络(RNN)对非线性系统进行建模与线性化处理,从而提升纳米级定位系统的精度与动态响应性能。该方法通过提取系统隐含动态特征,构建近似线性模型,便于后续模型预测控制(MPC)的设计与优化,适用于高精度自动化控制场景。文中还展示了相关实验验证与仿真结果,证明了该方法的有效性和先进性。; 适合人群:具备一定控制理论基础和Matlab编程能力,从事精密控制、智能制造、自动化或相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于纳米级精密定位系统(如原子力显微镜、半导体制造设备)中的高性能控制设计;②为非线性系统建模与线性化提供一种结合深度学习与现代控制理论的新思路;③帮助读者掌握Koopman算子、RNN建模与模型预测控制的综合应用。; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法实现流程,重点关注数据预处理、RNN结构设计、Koopman观测矩阵构建及MPC控制器集成等关键环节,并可通过更换实际系统数据进行迁移验证,深化对方法泛化能力的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值