如何在TreeView的CheckBoxes属性设置为true时隐藏子节点的CheckBox?

内容纲要

要实现这种功能,必须重新绘制树节点(tree node)。我们可以设置TreeView.DrawMode属性为OwnerDrawAll并处理DrawNode事件。请参考以下代码:

代码:

private void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e)

{

if (IsThirdLevel(e.Node))

{

Color backColor, foreColor;

if ((e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected)

{

backColor = SystemColors.Highlight;

foreColor = SystemColors.HighlightText;

}

else if ((e.State & TreeNodeStates.Hot) == TreeNodeStates.Hot)

{

backColor = SystemColors.HotTrack;

foreColor = SystemColors.HighlightText;

}

else

{

backColor = e.Node.BackColor;

foreColor = e.Node.ForeColor;

}

using (SolidBrush brush = new SolidBrush(backColor))

{

e.Graphics.FillRectangle(brush, e.Node.Bounds);

}

TextRenderer.DrawText(e.Graphics, e.Node.Text, this.treeView.Font, e.Node.Bounds, foreColor, backColor);

if ((e.State & TreeNodeStates.Focused) == TreeNodeStates.Focused)

{

ControlPaint.DrawFocusRectangle(e.Graphics, e.Node.Bounds,

foreColor, backColor);

}

e.DrawDefault = false;

}

else

{

e.DrawDefault = true;

}

}

圈主 管理员

热门评论
:
该帖子评论已关闭
图片审查中...
编辑答案: 我的回答: 最多上传一张图片和一个附件
x
x
个人中心
今日签到
有新私信 私信列表
搜索