public TreeViewOwnerDraw() { // Create and initialize the TreeView control. myTreeView = new TreeView(); myTreeView.Dock = DockStyle.Fill; myTreeView.BackColor = Color.Tan; myTreeView.CheckBoxes = true; // Add nodes to the TreeView control. TreeNode node; for (int x = 1; x < 4; ++x) { // Add a root node to the TreeView control. node = myTreeView.Nodes.Add(String.Format("Task {0}", x)); for (int y = 1; y < 4; ++y) { // Add a child node to the root node. node.Nodes.Add(String.Format("Subtask {0}", y)); } } myTreeView.ExpandAll(); // Add tags containing alert messages to a few nodes // and set the node background color to highlight them. myTreeView.Nodes[1].Nodes[0].Tag = "urgent!"; myTreeView.Nodes[1].Nodes[0].BackColor = Color.Yellow; myTreeView.SelectedNode = myTreeView.Nodes[1].Nodes[0]; myTreeView.Nodes[2].Nodes[1].Tag = "urgent!"; myTreeView.Nodes[2].Nodes[1].BackColor = Color.Yellow; // Configure the TreeView control for owner-draw and add // a handler for the DrawNode event. myTreeView.DrawMode = TreeViewDrawMode.OwnerDrawText; myTreeView.DrawNode += new DrawTreeNodeEventHandler(myTreeView_DrawNode); // Add a handler for the MouseDown event so that a node can be // selected by clicking the tag text as well as the node text. myTreeView.MouseDown += new MouseEventHandler(myTreeView_MouseDown); // Initialize the form and add the TreeView control to it. this.ClientSize = new Size(292, 273); this.Controls.Add(myTreeView); }
自定义吧!
你需要自己手动绘制树了。
{
// Create and initialize the TreeView control.
myTreeView = new TreeView();
myTreeView.Dock = DockStyle.Fill;
myTreeView.BackColor = Color.Tan;
myTreeView.CheckBoxes = true; // Add nodes to the TreeView control.
TreeNode node;
for (int x = 1; x < 4; ++x)
{
// Add a root node to the TreeView control.
node = myTreeView.Nodes.Add(String.Format("Task {0}", x));
for (int y = 1; y < 4; ++y)
{
// Add a child node to the root node.
node.Nodes.Add(String.Format("Subtask {0}", y));
}
}
myTreeView.ExpandAll(); // Add tags containing alert messages to a few nodes
// and set the node background color to highlight them.
myTreeView.Nodes[1].Nodes[0].Tag = "urgent!";
myTreeView.Nodes[1].Nodes[0].BackColor = Color.Yellow;
myTreeView.SelectedNode = myTreeView.Nodes[1].Nodes[0];
myTreeView.Nodes[2].Nodes[1].Tag = "urgent!";
myTreeView.Nodes[2].Nodes[1].BackColor = Color.Yellow; // Configure the TreeView control for owner-draw and add
// a handler for the DrawNode event.
myTreeView.DrawMode = TreeViewDrawMode.OwnerDrawText;
myTreeView.DrawNode +=
new DrawTreeNodeEventHandler(myTreeView_DrawNode); // Add a handler for the MouseDown event so that a node can be
// selected by clicking the tag text as well as the node text.
myTreeView.MouseDown += new MouseEventHandler(myTreeView_MouseDown); // Initialize the form and add the TreeView control to it.
this.ClientSize = new Size(292, 273);
this.Controls.Add(myTreeView);
}
你的代码好像也不行,很感谢你的回答!也许是你的DrawMode代码里做了些处理,如果允许的话,请分享下!谢谢
2 设置tree ImageList属性为imagelist1 此时可以看到效果
如果想父节点和子节点图片不相同
3 后台递归节点代码:
treenode t=new treenode();
t.ImageIndex=0;...