none
Treeview TreeNode & HotTracking RRS feed

  • 質問

  • Hi,

     

    I have a treeview with HotTracking enabled. After changing the fontstyle of a node, HotTracking does not seem to work [properly] anymore for the affected node, i.e. the underline no longer appears (other nodes behave as expected).

     

    The code is:

     

    Code Snippet

    TreeNode tn = tree_SERVERS.Nodes[a.server].Nodes[a.group];

    tn.NodeFont = new Font(tree_SERVERS.Font.FontFamily, tree_SERVERS.Font.Size, FontStyle.Bold);

     

    Any ideas?

     PS: Am using VS2005.

     

    Thanks,

    Michael.

     

    2007年5月19日 7:41

回答

  • Yes, the control doesn't want to mess with the font you've selected.  To fix this, you'll need to implement custom drawing.  This sample code gets the job done:

      public partial class Form1 : Form {
        public Form1() {
          InitializeComponent();
          treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;
          treeView1.DrawNode += treeView1_DrawNode;
        }
        private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e) {
          bool hot = (e.State & TreeNodeStates.Hot) != 0;
          if (hot) {
            using (SolidBrush br = new SolidBrush(this.BackColor)) {
              e.Graphics.FillRectangle(br, e.Bounds);
            }
            Font nodeFont = e.Node.NodeFont != null ? e.Node.NodeFont : treeView1.Font;
            nodeFont = new Font(nodeFont, FontStyle.Underline | nodeFont.Style);
            using (nodeFont) {
              using (SolidBrush br = new SolidBrush(this.ForeColor)) {
                e.Graphics.DrawString(e.Node.Text, nodeFont, br, Rectangle.Inflate(e.Bounds, 2, 0));
              }
            }
          }
          else e.DrawDefault = true;
        }
        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) {
          e.Node.NodeFont = new Font(treeView1.Font, FontStyle.Italic);
        }
      }

    2007年5月19日 15:47
    モデレータ