Process custom plotting icons of TreeView

Source: Internet
Author: User

Treeview is a commonly used Winform control. It provides a display of check boxes and icons, and indentation of upper and lower-level nodes, which is very convenient and practical in development. Generally, by specifying an ImageList control, you can place all the node charts to be filled in the control and direct the ImageList attribute of the Treeview to it, then, you can specify the corresponding icon serial number (or name) based on the node data in the program, which facilitates the dynamic switch of icons. However, sometimes the node icons are not fixed and may be dynamically generated in the program, such as common color settings and GIS legends. You need to refresh the node color and pattern in real time. Obviously, I cannot store all node images in ImageList in advance. What should I do? Fortunately, the Treeview control provides us with an interface for custom painting.

The following describes the usage and key points of custom plotting based on a legend control previously written.

  FirstTo implement custom painting, you must set the Treeview. DrawMode attribute to specify the painting mode:

Treeview. DrawMode = TreeViewDrawMode. OwnerDrawText; // customize the text and icon of the draw node.

Or

Treeview. DrawMode = TreeViewDrawMode)

In general, we only need to draw icons and text lines. The dotted lines and check boxes are drawn by the system. The following example uses the TreeViewDrawMode. OwnerDrawText mode.

  SecondAfter the draw mode is set, you need to implement the node draw event Treeview. DrawNode. This event is only triggered when the DrawMode attribute is set to the TreeViewDrawMode value of OwnerDrawAll or OwnerDrawText. In addition, this event is triggered when every node is drawn, not for all nodes.

The event is declared as public delegate void DrawTreeNodeEventHandler (Object sender, DrawTreeNodeEventArgs e). The DrawTreeNodeEventArgs parameter contains the Node Object to be drawn and the range coordinates of the node.

  When this method is implementedIt is mainly responsible for two tasks: Drawing icons and drawing text. The drawing method relies on the Graphics object handle in the DrawTreeNodeEventArgs parameter. The sample code is as follows:

1 /// <summary>
2 // draw node icons and text by yourself
3 /// </summary>
4 /// <param name = "sender"> </param>
5 /// <param name = "e"> </param>
6 void legendTree_DrawNode (object sender, DrawTreeNodeEventArgs e)
7 {
8 Rectangle nodeRect = e. Node. Bounds; // Node region
9
10 Point drawPt = new Point (nodeRect. Location. X-18, nodeRect. Location. Y); // the start position of the draw icon
11 Size imgSize = new Size (12, 12); // image Size
12 Rectangle imgRect = new Rectangle (drawPt, imgSize );
13
14 // -------- draw an image: Determine the node type and draw different images based on the node type --------------------
15 if (e. Node is DirectoryNode)
16 {
17 this. LegendIcon. Draw (e. Graphics, drawPt, 0 );
18}
19 else if (e. Node is LayerNode)
20 {
21 ....
22}
23 else if (e. Node is ThemeNode)
24 {
25 ....
26}
27 else
28 {
29
30}
31
32 // ----------------------- draw text -------------------------------
33 Font nodeFont = e. Node. NodeFont;
34 if (nodeFont = null)
35 nodeFont = (TreeView) sender). Font;
36 Brush textBrush = SystemBrushes. WindowText;
37 if (mapView. MapObject. ReadOnly)
38 textBrush = SystemBrushes. GrayText; // if not editable, set the font color to Gray.
39 // reversed highlighted
40 if (e. State & TreeNodeStates. Focused )! = 0)
41 textBrush = SystemBrushes. Window;
42 // The text area is not limited, so that the text in the large font length cannot be captured ---- edited by: Vivi 2009/11/19
43 e. Graphics. DrawString (e. Node. Text, nodeFont, textBrush, Rectangle. Inflate (nodeRect, 2, 0 ));
44}

During painting, Be sure to control the starting position of the icon and the size of the filled range of the icon. In addition, if some fixed Icons can be placed in ImageList or a local image file, you can also draw images using related classes to achieve greater operation flexibility.

The following figure shows the effect after painting:

Note:During the rendering process, we should pay attention to the performance and efficiency issues. Due to real-time rendering and frequent calls, we should avoid large data volumes as much as possible. If we use e. graphics. getHdc (), you must remember to call e. graphics. releaseHdc ().

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.