Drawing with the VisualStyleRenderer

Assuming visual styles are enabled, you begin by choosing the type of element you want to draw from the set of VisualStyleElement nested classes. Each nested VisualStyleElement class contains a group of static properties that allows you to retrieve the VisualStyleElement object.

For example, the VisualStyleElement.CheckBox.Button class provides static properties like CheckedDisabled, CheckedNormal, CheckedPressed, UncheckedDisabled, and so on. Each property returns a VisualStyle object that represents the element in the corresponding state.

VisualStyleElement element = VisualStyleElement.Button.CheckBox.CheckedNormal;

There are several dozen visual element classes. You can consult the MSDN Help to browse the full list.

Once you have the VisualStyle object you want, you can create a VisualStyleRenderer that wraps it. Before you do this, it's considered good practice to call the VisualStyleRenderer. IsElementDefined() method to make sure the renderer supports the element you've chosen (meaning it's supported by the current theme). For example, though there's a set of VisualStyleElement.Menu classes, none of the themes provided with current operating systems supports it.

if (VisualStyleRenderer.IsElementDefined(element)) {

VisualStyleRenderer renderer = new VisualStyleRenderer(element);

■Note In theory, you could write your code generically to use visual styles for all elements when available. However, there is only one implementation of visual styles currently available (both Windows XP and Windows 2003 Server are the same), and future versions of Windows are likely to adopt a new drawing framework. That means in practice it's reasonable to code against the known visual style implementation and streamline your code.

The last step is to use the methods of the VisualStyleRenderer to create the output. The core VisualStyleRenderer methods are described in Table 7-7.

Table 7-7. Essential VisualStyleRenderer Methods

Method

Description

DrawBackground()

DrawEdge() DrawText()

DrawImage()

Draws the background for the current visual style element. In many cases, the background is the element—for example, the background of a push button creates the familiar white shaded button, and the background of a check box paints the check box. All you need to do after calling this method is (optionally) add text and a border.

Draws one or more edges of the specified bounding rectangle.

Draws text in the specified bounds using the appropriate font. The image is automatically adjusted based on the state of the item (for example, disabled).

Draws the specified image within the specified bounding rectangle. The image is automatically adjusted based on the state of the item (for example, disabled).

Table 7-7. Essential VisualStyleRendererMethods (Continued) Method Description

DrawParentBackground() Draws the background of the control's parent in the specified area. Has no effect when painting directly to a form.

Returns true if a specified point is contained in the background of the current visual style element. This is useful because, although you choose the bounding rectangle for the element, you don't necessarily know where the content is drawn.

Returns true if the specified visual style element is defined by the current visual style. If it isn't, don't attempt to use any of the drawing methods—they won't produce any output.

Sets the VisualStyleRenderer to use a different VisualStyleElement object.

HitTestBackground()

IsElementDefined()

SetParameters()

Here's the remainder of the painting code. It displays a check box in a bordered and a text caption. The key methods are DrawBackground(), which creates the check box, DrawEdge(), and DrawTest().

Rectangle rectCheck = new Rectangle(10, 10, 50, 50); Rectangle rectBox = new Rectangle(10, 10, 200, 50); Rectangle rectText = new Rectangle(50, 25, 150, 25); renderer.DrawBackground(e.Graphics, rectCheck); renderer.DrawEdge(e.Graphics, rectBox,

Edges.Bottom | Edges.Top | Edges.Left | Edges.Right, EdgeStyle.Etched, EdgeEffects.Flat); renderer.DrawText(e.Graphics, rectText, "Styled checkbox", false, TextFormatFlags.Top);

Figure 7-28 shows the result.

Figure 7-28. Drawing pictures with VisualStyleRenderer

■Note Sadly, due to a bug in the .NET API for visual styles, the font is not always set correctly. Although this issue will be fixed in future releases, you can use the (somewhat awkward) workaround described at http://blogs.msdn.com/jfoscoding/articles/475517.aspx for now.

To get a better feeling for DrawTest() and the visual style elements that are available, be sure to browse the System.Windows.Form.VisualStyles namespace. You'll find classes that represent core controls (buttons, check boxes, text boxes, drop-downs, scroll bars, etc.) along with more modern controls (trees, panels, toolbars, and more). Regardless of the element, you use the same set of VisualStyleRenderer methods from Table 7-7 to render the output.

■Tip You can switch an existing VisualStyleRenderer object to use another element by calling the SetParameters() method and supplying the new VisualStyleElement object.

0 0

Post a comment

  • Receive news updates via email from this site