The translation of programming WPF 7th 5. Visual Layer Programming

Source: Internet
Author: User
Tags drawtext

Original: "Programming WPF" translation 7th 5. Visual Layer Programming

shape elements can provide a convenient way to work with graphics, and in some cases, add elements representing drawings to In the UI tree, it may be more cumbersome than its value. Your data may be structured in a way that is easy to write code-simply to represent a series of data-based drawing operations rather than constructing a tree of objects.

WPF provides a "visual layer"APIas a tradeoff for lower levels of shape elements. (In fact, the shape elements are all implemented at the top of the visualization layer.) This API allows us to write code that is generated on demand.

visualization is a visible object. The appearance of a WPF application is the combination of all of its visualizations on the screen. Because WPF is generated at the top level of the visual layer, each element is visualized. the FrameworkElement base class is indirectly derived from Visual. Programming in the visual layer simply solves creating a visualization and writing code to tell WPF what we want to show in the visualization.

even at this low level, the performance of WPF is very different from Win32. Graphics acceleration is managed, which means that your on-demand generated code is rarely called-less than in classic Windows applications.

7.5.1 on-Demand generation

the key customizations generated on demand are OnRender method. This method is called by WPF when it needs your component to generate its appearance. (This is how inline-shaped classes generate themselves.) )

the OnRender virtual method is defined in the ondemandvisual class. Most elements are indirectly derived from this by FrameworkElement , which increases the core style such as appearance and input processing.

example 7-47 shows an element of a word definition, covering OnRender.

Example 7-47

Public classcustomrender:frameworkelement
{
protected Override voidOnRender (DrawingContext drawingcontext)
{
Debug.WriteLine ("OnRender");

Base. OnRender (DrawingContext);

Drawingcontext.drawrectangle (brushes.red,NULL, NewRect (0, 0,  -,  -));

FormattedText text= NewFormattedText ("Hello, World",
CultureInfo.CurrentUICulture, Flowdirection.lefttorightthentoptobottom,
NewTypeface ("Verdana"),  -, Brushes.black);
Drawingcontext.drawtext (text,NewPoint (3, 3));
}

}

The OnRender method passes a separate parameter for the DrawingContext type. This is a low level drawing API in WPF. It provides a set of basic drawing operations, which are listed in table 7-4 . Example 7-47 uses the drawrectangle and DrawText methods.

Notice that the DrawingContext uses the Brush and Pen classes to indicate how shapes are filled and outlined, as we saw earlier in the high-level Shape object. We can also pass the same Geometry and Drawing objects-as we saw earlier in this chapter.

Table 7-4. DrawingContext Drawing Operations

Operation

Usage

Drawdrawing

Draws a Drawing object.

DrawEllipse

Draws an ellipse.

Drawgeometry

Draws any Geometry object.

DrawGlyphRun

Draws a series of glyphs (i.e., text elements) offering detailed control over typography.

DrawImage

Draws a bitmap image.

DrawLine

Draws a line (a single segment).

DrawRectangle

Draws a rectangle.

Drawroundedrectangle

Draws a rectangle with rounded corners.

DrawText

Draws text.

Drawvideo

Draws a rectangular region that can display video.

Pushtransform

Sets a transform that'll be applied to all subsequent drawing operations until pops is called; If a transform is already on place, the net effect would be the combination of all the transforms currently pushed.

Pushclip

Sets a clip region that'll be applied to all subsequent drawing operations until pops is called; As with Pushtransform, multiple active clip regions'll combine.

Pushopacity

Sets a level of opacity that'll be applied to all subsequent drawing operations until pops is called; As with transforms and clips, multiple opacities is combined.

Pop

Removes the transform, clip region, or opacity added most recently by Pushtransform, pushclip, or Pu Shopacity. If Those methods has been called multiple times, calls to POPs remove their effects in reverse order. (The transforms, clip regions, and opacities behave like a stack.)



Because our custom elements derive from theFrameworkElement , it is naturally integrated into anyWpf the application. Example7-48 as a token of a form that contains a custom element, we can use it just like any custom element we used before. This form7-56 is shown.

Example 7-48

<?Mapping XmlNamespace="Controls"ClrNamespace="Visualrender" ?>
<Window x:class="Visualrender.window1"
xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
Xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"
XMLNS:CC="Controls"
Text="Visual Layer Rendering"
>
<Canvas>
<Cc:customrender Canvas.Top="Ten"Canvas.Left="Ten"x:name="Customrender" />
</Canvas>
</Window>

Figure 7-56


Note, example 7-47 Onrender method called Debug.writeline vs2008 debugger, this will be called every time onrender wpf often requires our custom visualizations to generate itself. If you are accustomed to how in the standard win32 and Windows forms You may want to see this being called regularly-whether the form is resized or partially blurred or hidden-on-demand drawing. In fact, it will only be called once.

The result is an on-demand build that is not similar to The old style of Win32 is generated, as you might think. WPF calls your OnRender method when it needs to know what your visualizations are showing, but how graphics acceleration works means that this happens much less than the equivalent redraw in Win32. WPF caches the build directives. The scope and form of this cache is not textual, but it clearly happens. Further, this is much more subtle than a simple image-based cache. We can add this code to the host form of the example 7-48 (which might be the case in the background code file).

  protected   Override   void Onmouseleftbuttondown (MouseButtonEventArgs e)
{

Visualoperations.settransform (Customrender, new ScaleTransform (6, 6 ));
}

the previous fragment applied a transition to our element, magnified with factor 6 . When you click on the user interface, the custom visualization will extend as you wish, and OnRender is not called. Not only that, the magnified visualization does not show any pixelated or blurred artifacts that you can see, zooming through a simple image. It continues to be sharp, as you can see in Figure 7-57 .

Figure 7-57


This indicates that WPF obtains scaling information about the visual content. It can redraw our visual screen appearance without using our OnRender method, even when the conversion changes. This is partly dependent on the acceleration architecture, and also because conversion support is generated at most of the base level in WPF .

The ability to redraw WPF --without using OnRender, allows the user interface to remain intact on the screen, even if our application is busy. It also allows the animation system to work without a lot of interference from the application, because all the underlying drawing operations are preserved, andWPF can reconstruct any part of the UI-when independent elements change.

if the state of our object has to change to some extent-we need to update the appearance, we can call Invalidatevisual method. This will cause WPF to call our OnRender method, allowing us to reconstruct the appearance.

Notice that when you rewrite when OnRender, you should also typically overwrite the measureoverrid and arrangeoverride methods. Otherwise,the appearance system of WPF will not know how big your elements are. The only reason we get it, instead of doing it here, is that we use that element on Canvas, which will not care how big its child elements are. In order to work on other panels, it is essential to let the appearance system know your size. The second chapter describes the Measureoverrid and arrangeoverride methods.

The translation of programming WPF 7th 5. Visual Layer Programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.