|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Reflection;
namespace WinApp1
{
public partial class VisualStyleTree : UserControl
{
public event TreeViewEventHandler AfterSelect;
public VisualStyleTree()
{
InitializeComponent();
PopulateVisualStyleList();
m_treeView.SelectedNode = m_treeView.Nodes[0];
}
public VisualStyleElement SelectedElement
{
get
{
Item item = m_treeView.SelectedNode.Tag as Item;
if (item != null)
return item.Element;
return null;
}
}
void PopulateVisualStyleList()
{
try
{
AddType(typeof(VisualStyleElement), null);
}
catch (InvalidOperationException error)
{
m_treeView.Nodes.Add(error.Message);
}
foreach (TreeNode node in m_treeView.Nodes)
node.ExpandAll();
ContextMenu menu = new ContextMenu();
menu.MenuItems.Add(new MenuItem("Expand", new EventHandler(OnExpandSingle)));
menu.MenuItems.Add(new MenuItem("Expand All", new EventHandler(OnExpandAll)));
menu.MenuItems.Add(new MenuItem("Collapse All", new EventHandler(OnCollapseAll)));
m_treeView.ContextMenu = menu;
}
void OnExpandSingle(object sender, EventArgs e)
{
m_treeView.BeginUpdate();
m_treeView.SelectedNode.ExpandAll();
m_treeView.EndUpdate();
}
void OnExpandAll(object sender, EventArgs e)
{
m_treeView.BeginUpdate();
TreeNode selnode = m_treeView.SelectedNode;
foreach (TreeNode node in m_treeView.Nodes)
node.ExpandAll();
m_treeView.SelectedNode = selnode;
m_treeView.EndUpdate();
}
void OnCollapseAll(object sender, EventArgs e)
{
m_treeView.BeginUpdate();
foreach (TreeNode node in m_treeView.Nodes)
node.Collapse();
m_treeView.EndUpdate();
}
class Item
{
public string Name;
public PropertyInfo PropertyInfo;
public VisualStyleElement Element;
public Item(string name, PropertyInfo proeprtyinfo, VisualStyleElement element)
{
Name = name;
Element = element;
PropertyInfo = proeprtyinfo;
}
public override string ToString()
{
return Name;
}
}
void AddType(Type type, TreeNode parent)
{
Type[] types = type.GetNestedTypes();
foreach (Type subtype in types)
{
TreeNode node;
if (parent == null)
node = m_treeView.Nodes.Add(subtype.Name);
else
node = parent.Nodes.Add(subtype.Name);
AddType(subtype, node);
}
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
if (property.PropertyType != typeof(VisualStyleElement))
continue;
VisualStyleElement element = property.GetValue(null, BindingFlags.Static, null, null, null) as VisualStyleElement;
string name = property.Name;
if (VisualStyleRenderer.IsElementDefined(element) == false)
name += " (Not defined)";
TreeNode node = parent.Nodes.Add(name);
node.Tag = new Item(property.DeclaringType.FullName + "+" + property.Name, property, element);
}
}
private void OnTreeDrawNode(object sender, DrawTreeNodeEventArgs e)
{
Item item = e.Node.Tag as Item;
e.DrawDefault = true;
if (item == null)
return;
if (VisualStyleRenderer.IsElementDefined(item.Element) == false)
return;
Rectangle r = e.Bounds;
r.Width += 20;
e.Graphics.DrawString(e.Node.Text, Font, Brushes.Black, r, null);
VisualStyleRenderer rendere = new VisualStyleRenderer(item.Element);
r.Y += 1;
r.Height -= 1;
r.X = 180;
r.Width = 50;
Size size = rendere.GetPartSize(e.Graphics, r, ThemeSizeType.True);
rendere.DrawBackground(e.Graphics, r);
}
private void OnTreeAfterSelect(object sender, TreeViewEventArgs e)
{
string s = string.Empty;
Item item = m_treeView.SelectedNode.Tag as Item;
if (item != null)
{
s = item.Name.Replace("System.Windows.Forms.VisualStyles.", "");
s = s.Replace('+', '.');
}
m_label.Text = s;
if (AfterSelect != null)
AfterSelect(this, e);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.