【问题标题】:How to redraw Node in TreeView (WinForms)如何在 TreeView (WinForms) 中重绘节点
【发布时间】:2009-09-10 08:10:04
【问题描述】:

我需要用闪烁的 TreeNode 实现自己的 TreeView。我的原型是:

public class BlinkTreeView : TreeView
    {
        private int blinkInterval; 

        private bool blinkState;

        [Category("Behavior"), Browsable(true)]
        public Icon BlinkIcon { get; set; }

        [Category("Behavior"), Browsable(true)]
        public Icon SelectedBlinkIcon { get; set; }

        [Category("Behavior"), Browsable(true), DefaultValue(1000)]
        public int BlinkInterval {
            get
            {
                return blinkInterval;
            }
            set
            {
                blinkInterval = value;
                if (value > 0)
                {
                    blinkTimer.Interval = value;
                    blinkTimer.Start();
                }
                else
                {
                    blinkTimer.Stop();
                    blinkState = false;
                    Invalidate();
                }
            }
        }

        private Timer blinkTimer;

        public BlinkTreeView()
            : base()
        {
            blinkTimer = new Timer();
            blinkTimer.Tick += new EventHandler(blinkTimer_Tick);
            blinkState = false;
            this.DrawMode = TreeViewDrawMode.OwnerDrawAll;
        }

        void blinkTimer_Tick(object sender, EventArgs e)
        {
            if (BlinkInterval > 0)
            {
                blinkState = !blinkState;
            }
            else
            {
                blinkState = false;
            }
            Invalidate();
        }

        protected override void OnDrawNode(DrawTreeNodeEventArgs e)
        {
            e.DrawDefault = true;
            base.OnDrawNode(e);
            if (blinkState)
                {
//here i want to draw blinked item, but i can't redraw item icons and text.
                }
            }
        }

在 OnDrawNode 中,我无法重绘节点的图标和文本。 知道如何解决这个问题吗?

【问题讨论】:

  • 有什么问题?你有什么例外吗?
  • 不,只是在 e.Graphics 上,我可以在 base.OnDrawNode(e) 中绘制的图像下绘制;

标签: c# winforms treeview


【解决方案1】:

只是一个想法,但您可以在项目上反转(xor)而不使树成为所有者绘制控件。我认为它的工作原理如下:

using (Graphics g = Graphics.FromHwnd(Tree.Handle))
{
    TreeNode node = myBlinkyNode;
    if (node != null)
    {
        using(Region myRegion = new Region(node.Bounds))
            myRegion.Xor(xorRect);
    }
}

您需要跟踪闪烁是否可见并处理 Paint 事件,以便重新绘制倒置矩形。

【讨论】:

  • 是的,可以解决,但我确实需要 Node 的闪烁图标。
【解决方案2】:

有一个计时器来切换闪烁节点的状态,即:

Node.ForeColor = Node.ForeColor == Color.White ? Color.Black : Color.White;

【讨论】:

    猜你喜欢
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 2010-10-16
    • 2013-11-16
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多