TreeView DrawNode事件问题 [英] TreeView DrawNode Event Problem

查看:484
本文介绍了TreeView DrawNode事件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一种形式的TreeView.我已使用DrawNode事件在TreeView中创建两列.源代码来自Microsoft msdn网站,用法如下:

Hi,

I have a TreeView in one form. I have used the DrawNode event to create two columns in the TreeView. Source code, which is taken from microsoft msdn site, used is given below:

private void tvwCOA_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
  int intFlag = 0;

  // Draw the background and node text for a selected node.
  if ((e.State & TreeNodeStates.Selected) != 0)
  {
    // Draw the background of the selected node. The NodeBounds
    // method makes the highlight rectangle large enough to
    // include the text of a node tag, if one is present.
    e.Graphics.FillRectangle(Brushes.Green, NodeBounds(e.Node));
    // Retrieve the node font. If the node font has not been set,
    // use the TreeView font.
    Font nodeFont = e.Node.NodeFont;
    if (nodeFont == null) nodeFont = ((TreeView)sender).Font;

    // Draw the node text.
    e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White, e.Bounds.X, e.Bounds.Y);
    e.Graphics.DrawString(clsCommon.Right(e.Node.Tag.ToString(), 8), nodeFont, Brushes.White, 435, e.Bounds.Y);
                intFlag = 1;
  }

  // Use the default background and node text.
  else
  {
    e.DrawDefault = true;
  }

  // If a node tag is present, draw its string representation
  // to the right of the label text.

  if (intFlag != 1)
  {
    e.Graphics.DrawString(clsCommon.Right(e.Node.Tag.ToString(), 8), tagFont, Brushes.Black, 435, e.Bounds.Top);
  }

  // If the node has focus, draw the focus rectangle large, making
  // it large enough to include the text of the node tag, if present.
  if ((e.State & TreeNodeStates.Focused) != 0)
  {
    using (Pen focusPen = new Pen(Color.Black))
    {
      focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
      Rectangle focusBounds = NodeBounds(e.Node);
      focusBounds.Size = new Size(focusBounds.Width - 1,
      focusBounds.Height - 1);
      e.Graphics.DrawRectangle(focusPen, focusBounds);
    }
  }
} 

private Rectangle NodeBounds(TreeNode node)
{
  // Set the return value to the normal node bounds.
  Rectangle bounds = node.Bounds;
  if (node.Tag != null)
  {
    // Retrieve a Graphics object from the TreeView handle
    // and use it to calculate the display width of the tag.
    Graphics g = tvwCOA.CreateGraphics();
    //int tagWidth = (int)g.MeasureString (node.Tag.ToString(), tagFont).Width + 6;
    int tagWidth = 485; // (int)g.MeasureString(node.Text.ToString(), tagFont).Width + 6;
    // Adjust the node bounds using the calculated value.
    bounds.Offset(tagWidth / 2, 0);
    bounds = Rectangle.Inflate(bounds, tagWidth / 2, 0);
    g.Dispose();
  }

  return bounds;
}



每个节点在标签中都有一个值.我面临的问题是,每当我单击一个节点以将其展开时,TreeView中第一个节点的标记值(在X = 435处打印在第一个节点上)将被第一个子节点的标记值覆盖.扩展节点.如果我们向下滚动并到达第一个节点,则该值正确.我做了很多尝试,但是找不到我犯错的地方.希望我能解释清楚.请帮忙.

Cris Evan



Each node has a value in the tag. The problem I am facing is whenever I click a node to expand it, the tag value of the first node in the TreeView ( which is printed against the first node at X= 435 ) is overwritten by the tag value of the first child of the expanded node. If we scroll down and come to the first node the value is correct. I tried a lot but could not locate where I am making a mistake. Hope I explained well. Please help.

Cris Evan

推荐答案

我认为问题可能是您正在做一些绘图,即使执行了else-> e.DrawDefault = true;.这意味着完成处理程序后,DrawDefault 的值用于确定是否必须完成任何绘制.

此代码不在该逻辑范围内,可以由默认处理程序重新绘制:
I think the problem might be you are doing some drawing even if the else was executed -> e.DrawDefault = true;. This means that when you''re handler is done, the value of DrawDefault is used to determine if any drawing must be done.

This code is outside of that logic and could be repainted by the default handler:
// If a node tag is present, draw its string representation
  // to the right of the label text.
  if (intFlag != 1)
  {
    e.Graphics.DrawString(clsCommon.Right(e.Node.Tag.ToString(), 8), tagFont, Brushes.Black, 435, e.Bounds.Top);
  }
  // If the node has focus, draw the focus rectangle large, making
  // it large enough to include the text of the node tag, if present.
  if ((e.State & TreeNodeStates.Focused) != 0)
  {
    using (Pen focusPen = new Pen(Color.Black))
    {
      focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
      Rectangle focusBounds = NodeBounds(e.Node);
      focusBounds.Size = new Size(focusBounds.Width - 1,
      focusBounds.Height - 1);
      e.Graphics.DrawRectangle(focusPen, focusBounds);
    }
  }
}



祝你好运!



Good luck!


这篇关于TreeView DrawNode事件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆