Skip to content

Commit

Permalink
added treeNode.ToolTipText;
Browse files Browse the repository at this point in the history
added treeView.NodeMouseHover
  • Loading branch information
Meragon committed Feb 2, 2018
1 parent b27946c commit f6939bd
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 20 deletions.
1 change: 1 addition & 0 deletions System/Windows/Forms/TreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public TreeNode PrevNode
}
public object Tag { get; set; }
public string Text { get; set; }
public string ToolTipText { get; set; }
public TreeView TreeView
{
get
Expand Down
14 changes: 14 additions & 0 deletions System/Windows/Forms/TreeNodeMouseHoverEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace System.Windows.Forms
{
public delegate void TreeNodeMouseHoverEventHandler(object sender, TreeNodeMouseHoverEventArgs e);

public class TreeNodeMouseHoverEventArgs : EventArgs
{
public TreeNodeMouseHoverEventArgs(TreeNode node)
{
Node = node;
}

public TreeNode Node { get; private set; }
}
}
78 changes: 58 additions & 20 deletions System/Windows/Forms/TreeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class TreeView : Control
internal readonly ScrollBar vScrollBar;
internal int arrowSize = 16;
internal TreeNode root;

private readonly DrawTreeNodeEventArgs nodeArgs = new DrawTreeNodeEventArgs(null, null, Rectangle.Empty, TreeNodeStates.Default);
private readonly List<TreeNode> nodeList = new List<TreeNode>();
private readonly DrawTreeNodeEventHandler onDrawNode;
Expand All @@ -20,6 +20,8 @@ public class TreeView : Control
private Point dragPosition;
private string filter;
private TreeNode hoveredNode;
private ToolTip nodeToolTip;
private TreeNode nodeToolTipLast;
private float resetFilterTime;

public TreeView()
Expand All @@ -36,7 +38,7 @@ public TreeView()
ImageList = new ImageList();
ItemHeight = 22;
Padding = new Padding(4);

ScrollBarColor = Color.FromArgb(222, 222, 230);
ScrollBarHoverColor = Color.FromArgb(136, 136, 136);
ScrollSpeed = 2;
Expand All @@ -58,13 +60,15 @@ public TreeView()
public event ItemDragEventHandler ItemDrag;
public event TreeNodeMouseClickEventHandler NodeMouseClick;
public event TreeNodeMouseClickEventHandler NodeMouseDoubleClick;
public event TreeNodeMouseHoverEventHandler NodeMouseHover;

public BorderStyle BorderStyle { get; set; }
public ImageList ImageList { get; set; }
public int ItemHeight { get; set; }
public TreeNodeCollection Nodes { get; private set; }
public TreeNode SelectedNode { get; set; }

public bool ShowNodeToolTips { get; set; }

internal Color ScrollBarColor { get; set; }
internal Color ScrollBarHoverColor { get; set; }
internal float ScrollIndex { get { return vScrollBar.Value; } set { vScrollBar.Value = (int)value; } }
Expand All @@ -87,6 +91,25 @@ public void CollapseAll()
Nodes[i].CollapseInternal();
Refresh();
}
public TreeNode GetNodeAt(int x, int y)
{
var scrollNodeListCount = scrollNodeList.Count;
for (int i = 0; i < scrollNodeListCount; i++)
{
var node = scrollNodeList[i];
var nodeY = node.Bounds.Y - ScrollIndex;
var nodeH = node.Bounds.Height;

if (y >= nodeY && y < nodeY + nodeH)
return node;
}

return null;
}
public TreeNode GetNodeAt(Point pt)
{
return GetNodeAt(pt.X, pt.Y);
}
public void ExpandAll()
{
var nodesCount = Nodes.Count;
Expand Down Expand Up @@ -213,7 +236,31 @@ protected override void OnMouseHover(EventArgs e)
{
var mclient = PointToClient(MousePosition);

hoveredNode = _GetNodeAtPosition(root, mclient);
hoveredNode = GetNodeAt(mclient);
if (hoveredNode != null)
{
if (ShowNodeToolTips)
{
if (string.IsNullOrEmpty(hoveredNode.ToolTipText) == false)
{
if (nodeToolTip == null)
{
nodeToolTip = new ToolTip();
nodeToolTip.InitialDelay = 0;
}

if (hoveredNode != nodeToolTipLast)
nodeToolTip.Show(hoveredNode.ToolTipText, mclient);

nodeToolTipLast = hoveredNode;
}
else
nodeToolTipLast = null;
}

// Tooltip will show previous ToolTipText. Override OnMouseHover if neccesary.
OnNodeMouseHover(new TreeNodeMouseHoverEventArgs(hoveredNode));
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
Expand Down Expand Up @@ -246,6 +293,12 @@ protected virtual void OnNodeMouseDoubleClick(TreeNodeMouseClickEventArgs e)
if (nodeMouseDoubleClick != null)
nodeMouseDoubleClick(this, e);
}
protected virtual void OnNodeMouseHover(TreeNodeMouseHoverEventArgs e)
{
var handler = NodeMouseHover;
if (handler != null)
handler(this, e);
}
protected override void OnPaint(PaintEventArgs e)
{
// Reset filter.
Expand Down Expand Up @@ -332,21 +385,6 @@ private void _Application_UpClick(object sender, MouseEventArgs e)
dragNode = null;
dragPosition = Point.Empty;
}
private TreeNode _GetNodeAtPosition(TreeNode rootNode, Point position)
{
var scrollNodeListCount = scrollNodeList.Count;
for (int i = 0; i < scrollNodeListCount; i++)
{
var node = scrollNodeList[i];
var nodeY = node.Bounds.Y - ScrollIndex;
var nodeH = node.Bounds.Height;

if (position.Y >= nodeY && position.Y < nodeY + nodeH)
return node;
}

return null;
}
private Rectangle _GetNodeBounds(TreeNode node)
{
nodeList.Add(node);
Expand Down Expand Up @@ -439,7 +477,7 @@ private void _OnDrawNode(object sender, DrawTreeNodeEventArgs e)
}
private TreeNode _SelectAtPosition(MouseEventArgs e)
{
var node = _GetNodeAtPosition(root, e.Location);
var node = GetNodeAt(e.Location);
if (node == null || node.Enabled == false) return null;

SelectedNode = node;
Expand Down

0 comments on commit f6939bd

Please sign in to comment.