Visual Layer Overview(6)Using DrawingVisual Objects

翻译 2006年06月14日 21:43:00
Using DrawingVisual Objects
1.        DrawingVisual Object
DrawingVisual是一个用来渲染shapes, images, 或者text的轻量级的类这个类之所以被认为是轻量级的,是因为它没有提供layout 或者event handling,所以增加了它的运行时性能。正是因为这个原因,描画backgrounds clip art是非常理想的。
2.        DrawingVisual Host Container
为了使用DrawingVisual对象,需要创建一个对象的host containerhost container对象必须从提供了layout event handlingFrameworkElement class继承,而DrawingVisual对象没有提供layout event handlinghost 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);
        this.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(MyVisualHost_MouseLeftButtonUp);
3.        Creating DrawingVisual Objects
当创建DrawingVisual对象时,它不包含任何描画内容。通过得到对象的描画上下文,可以添加text, graphic, image 内容。对象的描画上下文调用DrawingVisual对象的RenderOpen方法可以得到。
在描画上下文中描画矩形,使用DrawingContext 对象的DrawRectangle方法,当结束DrawingContext的描画内容时,调用Close方法关闭描画上下文中
// 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);
    return dv;
4.        Creating Overrides for FrameworkElement Members
Host container对象负责管理visual objects的集合。这需要Host container实现(overrides)从继承FrameworkElement class的一些方法
·                       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.
// 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来处理鼠标事件,例如:release鼠标左键,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;
            ((DrawingVisual)result.VisualHit).Opacity = 1.0;
    // Stop the hit test enumeration of objects in the visual tree.
    return HitTestResultBehavior.Stop;


Overview: Accessing Other Game Objects 访问其他游戏物体

Most advanced game code does not only manipulate a single object. The Unity scripting interface has ...

Special Edition Using Visual C++ 6

  • 2008-05-10 00:02
  • 9.96MB
  • 下载

#715 – 在Canvas中使用Thumb控件实现拖动目标的功能(Using the Thumb Control to Drag Objects on a Canvas)

原文地址: ...


  • 2009-10-10 08:56
  • 125KB
  • 下载

Data Structures and Other Objects Using C++ (Chapter 2) 学习笔记五