TreeView with multi-color TreeNode text

不打扰是莪最后的温柔 提交于 2019-12-08 01:35:00

问题


I need to have the text within a node in TreeView to be colored within words or characters. Is that possible? What is the way to go? I heard of Custom Drawing but have no experience with it!


回答1:


Set the property of the TreeView:

treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;

Then from the DrawNode event:

private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e) {
  Color nodeColor = Color.Red;
  if ((e.State & TreeNodeStates.Selected) != 0)
    nodeColor = SystemColors.HighlightText;

  TextRenderer.DrawText(e.Graphics,
                        e.Node.Text,
                        e.Node.NodeFont,
                        e.Bounds,
                        nodeColor,
                        Color.Empty,
                        TextFormatFlags.VerticalCenter);
}

More from MSDN: TreeView.DrawNode Event



来源:https://stackoverflow.com/questions/8709814/treeview-with-multi-color-treenode-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!