views:

470

answers:

2

I need to implement own TreeView with blinked TreeNode. My prototype is:

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.
                }
            }
        }

In OnDrawNode i can't redraw icon and text of node. Any idea how to solve this?

+1  A: 

Just a thought, but you could invert (xor) over the item without making the tree into an owner-draw control. I think it works something like the following:

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

You'll need to keep track if the blink is visible or not and handle the Paint event so that you can re-draw the inverted rectangle.

csharptest.net
Yes, It can be solution, but I need indeed blinky icon of Node.
Chernikov
A: 

Have a timer toggle the state of the blinking nodes, i.e.:

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