winfrom 树形控件如何实现鼠标经过节点时光标颜色改变效果
一、winform TreeView控件的实现方式。
/// <summary>
/// Handles the MouseMove event of the treeView1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
/// <remarks></remarks>
private void treeView1_MouseMove(object sender, MouseEventArgs e)
{
TreeView _TreeView = (TreeView)sender;
//通过鼠标的e.X和e.Y坐标来获取TreeNode。
TreeNode _Node = treeView1.GetNodeAt(e.X, e.Y);
if (_Node != null)
{
//防止出现闪烁。
if (_TreeView.Tag == null || !_TreeView.Tag.Equals(_Node))
{
_TreeView.Refresh();
Graphics _Graphics = Graphics.FromHwnd(_TreeView.Handle);
//_Graphics.FillRectangle(new SolidBrush(Color.FromArgb(100, 0, 0, 255)), _Node.Bounds);
//重绘整行节点。
_Graphics.FillRectangle(new SolidBrush(Color.FromArgb(100, 0, 0, 255)), new Rectangle(new Point(0, _Node.Bounds.Y), new Size(this.treeView1.Width, _Node.Bounds.Height)));
_Graphics.Dispose();
_TreeView.Tag = _Node;
}
}
}
参考文章:http://topic.csdn.net/u/20091216/14/8df3c649-fb8b-4ee5-89f2-6ab8ad03f9cf.html
二、winform Devexpress TreeList控件的实现方式。
/// <summary>
/// Handles the MouseMove event of the treeList1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
/// <remarks></remarks>
private void treeList1_MouseMove(object sender, MouseEventArgs e)
{
TreeList _TreeView = (TreeList)sender;
Point mousePosition = Control.MousePosition;
if (_TreeView.State == TreeListState.Regular)
{
TreeListHitInfo hitInfo = _TreeView.CalcHitInfo(new Point(e.X, e.Y));
if (hitInfo.HitInfoType == HitInfoType.Cell)
{
//_TreeView.SetFocusedNode(hitInfo.Node);
//根据鼠标的e.X和e.Y来获取TreeListNode;而不能直接通过 _TreeView.FocusedNode来获取。
TreeListNode _Node = hitInfo.Node;
//TreeListNode _Node = _TreeView.FocusedNode;// Winfrom 程序中这样获取的this.treeList1.GetNodeAt(e.X, e.Y);
Rectangle r = (_TreeView.ViewInfo.RowsInfo[_Node].Cells[0] as DevExpress.XtraTreeList.ViewInfo.CellInfo).Bounds;
if (_Node != null)
{
//if (!_Node.Checked)
//{
//防止出现闪烁。
if (_TreeView.Tag == null || !_TreeView.Tag.Equals(_Node))
{
_TreeView.Refresh();
Graphics _Graphics = Graphics.FromHwnd(_TreeView.Handle);
// _Graphics.FillRectangle(new SolidBrush(Color.FromArgb(100, 0, 0, 255)), r);
//重绘整行节点。
_Graphics.FillRectangle(new SolidBrush(Color.FromArgb(100, 0, 0, 255)), new Rectangle(new Point(0, r.Y), new Size(_TreeView.Width, r.Height)));
_Graphics.Dispose();
_TreeView.Tag = _Node;
}
//}
}
}
}
}
一、关于Devexpress控件的TreeListNode如何得到Point的问题。
可以参考http://www.devexpress.com/Support/Center/p/Q203877.aspx
二、关于Devexpress控件在MouseOver事件中如何得到当前鼠标的TreeListNode
可以参考http://community.devexpress.com/forums/t/82086.aspx
演示效果:演示Demo下载
/// <summary>
/// Handles the MouseMove event of the treeView1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
/// <remarks></remarks>
private void treeView1_MouseMove(object sender, MouseEventArgs e)
{
TreeView _TreeView = (TreeView)sender;
//通过鼠标的e.X和e.Y坐标来获取TreeNode。
TreeNode _Node = treeView1.GetNodeAt(e.X, e.Y);
if (_Node != null)
{
//防止出现闪烁。
if (_TreeView.Tag == null || !_TreeView.Tag.Equals(_Node))
{
_TreeView.Refresh();
Graphics _Graphics = Graphics.FromHwnd(_TreeView.Handle);
//_Graphics.FillRectangle(new SolidBrush(Color.FromArgb(100, 0, 0, 255)), _Node.Bounds);
//重绘整行节点。
_Graphics.FillRectangle(new SolidBrush(Color.FromArgb(100, 0, 0, 255)), new Rectangle(new Point(0, _Node.Bounds.Y), new Size(this.treeView1.Width, _Node.Bounds.Height)));
_Graphics.Dispose();
_TreeView.Tag = _Node;
}
}
}
参考文章:http://topic.csdn.net/u/20091216/14/8df3c649-fb8b-4ee5-89f2-6ab8ad03f9cf.html
二、winform Devexpress TreeList控件的实现方式。
/// <summary>
/// Handles the MouseMove event of the treeList1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
/// <remarks></remarks>
private void treeList1_MouseMove(object sender, MouseEventArgs e)
{
TreeList _TreeView = (TreeList)sender;
Point mousePosition = Control.MousePosition;
if (_TreeView.State == TreeListState.Regular)
{
TreeListHitInfo hitInfo = _TreeView.CalcHitInfo(new Point(e.X, e.Y));
if (hitInfo.HitInfoType == HitInfoType.Cell)
{
//_TreeView.SetFocusedNode(hitInfo.Node);
//根据鼠标的e.X和e.Y来获取TreeListNode;而不能直接通过 _TreeView.FocusedNode来获取。
TreeListNode _Node = hitInfo.Node;
//TreeListNode _Node = _TreeView.FocusedNode;// Winfrom 程序中这样获取的this.treeList1.GetNodeAt(e.X, e.Y);
Rectangle r = (_TreeView.ViewInfo.RowsInfo[_Node].Cells[0] as DevExpress.XtraTreeList.ViewInfo.CellInfo).Bounds;
if (_Node != null)
{
//if (!_Node.Checked)
//{
//防止出现闪烁。
if (_TreeView.Tag == null || !_TreeView.Tag.Equals(_Node))
{
_TreeView.Refresh();
Graphics _Graphics = Graphics.FromHwnd(_TreeView.Handle);
// _Graphics.FillRectangle(new SolidBrush(Color.FromArgb(100, 0, 0, 255)), r);
//重绘整行节点。
_Graphics.FillRectangle(new SolidBrush(Color.FromArgb(100, 0, 0, 255)), new Rectangle(new Point(0, r.Y), new Size(_TreeView.Width, r.Height)));
_Graphics.Dispose();
_TreeView.Tag = _Node;
}
//}
}
}
}
}
一、关于Devexpress控件的TreeListNode如何得到Point的问题。
可以参考http://www.devexpress.com/Support/Center/p/Q203877.aspx
二、关于Devexpress控件在MouseOver事件中如何得到当前鼠标的TreeListNode
可以参考http://community.devexpress.com/forums/t/82086.aspx
演示效果:演示Demo下载
作者:阿笨
【官方QQ一群:跟着阿笨一起玩NET(已满)】:422315558
【官方QQ二群:跟着阿笨一起玩C#(已满)】:574187616
【官方QQ三群:跟着阿笨一起玩ASP.NET(已满)】:967920586
【官方QQ四群:Asp.Net Core跨平台技术开发(可加入)】:829227829
【官方QQ五群:.NET Core跨平台开发技术(可加入)】:647639415
【网易云课堂】:https://study.163.com/provider/2544628/index.htm?share=2&shareId=2544628
【腾讯课堂】:https://abennet.ke.qq.com
【51CTO学院】:https://edu.51cto.com/sd/66c64
【微信公众号】:微信搜索:跟着阿笨一起玩NET