TreeView问题: 当我们鼠标选中一个Node时,只有一种难看的蓝色,请问能够让选中的Node添加其他颜色或不要颜色吗

machunyanlengxue 2007-10-06 01:56:32
TreeView问题: 当我们鼠标选中一个Node时,只有一种难看的蓝色,请问能够让选中的Node添加其他颜色或不要颜色吗
...全文
328 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
shluoping 2008-10-09
  • 打赏
  • 举报
回复
InitializeComponent 没有,报错不能呢个编译
tao77 2007-10-17
  • 打赏
  • 举报
回复
前段时间正好感上出差,没来,不好意思,直接贴上去吧,我自己写的。

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace UISystemControlBase
{
public delegate void PaintedEventHandler(object sender);



public partial class UITreeView : System.Windows.Forms.TreeView
{
public event PaintedEventHandler Painted;

private TreeNode mLastNode;

protected virtual void OnPainted()
{
if (Painted != null)
{
Painted(this );
}
}

public UITreeView()
{
InitializeComponent();
}

public UITreeView(IContainer container)
{
container.Add(this);

InitializeComponent();
}

#region "属性"
Bitmap mbmpExpandIcon = null;
Bitmap mbmpCollapseIcon = null;
Bitmap mbmpNoneIcon = null;
int mintTextLeft = 19;
Color mcolSelectedNodeBackColor = Color.FromArgb(255, 241, 230);

public Color SelectedNodeBackColor
{
get { return mcolSelectedNodeBackColor; }
set { mcolSelectedNodeBackColor = value; }
}

public int TextLeft
{
get { return mintTextLeft; }
set { mintTextLeft = value; }
}

public Bitmap ExpandIcon
{
get { return mbmpExpandIcon; }
set { mbmpExpandIcon = value; }
}

public Bitmap CollapseIcon
{
get { return mbmpCollapseIcon; }
set { mbmpCollapseIcon = value; }
}

public Bitmap NoneIcon
{
get { return mbmpNoneIcon; }
set { mbmpNoneIcon = value; }
}
#endregion

public int GetAllNodesHeight()
{
int intTop;
int intBottom;

if (base.Nodes.Count == 0) return 0;

intTop = base.Nodes[0].Bounds.Y;

GetLastNode(base.Nodes[base.Nodes.Count - 1]);

intBottom = mLastNode.Bounds.Y;

return intBottom - intTop + this.ItemHeight ;

}

private void GetLastNode(TreeNode n)
{
if (n.Nodes.Count != 0 && n.IsExpanded)
{
GetLastNode(n.Nodes[n.Nodes.Count - 1]);
}
else
{
mLastNode = n;
}
}

protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == 15)
{
OnPainted();
}
base.WndProc(ref m);
}

private void UITreeView_AfterExpand(object sender, TreeViewEventArgs e)
{
this.Invalidate();

}

private void UITreeView_AfterCollapse(object sender, TreeViewEventArgs e)
{
this.Invalidate();

}

private void UITreeView_MouseDown(object sender, MouseEventArgs e)
{
TreeNode node = this.GetNodeAt(e.X, e.Y);
if (node != null)
{
this.SelectedNode = node;
}
}


private void UITreeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
if ((e.State & TreeNodeStates.Selected) != 0)
{
e.Graphics.FillRectangle(new SolidBrush(mcolSelectedNodeBackColor), 0, e.Node.Bounds.Y, this.Width, e.Node.Bounds.Height);

}

//Debug.Print(e.Node.Text + ":" + e.State.ToString());

if (e.Node.Level == 0)
{
if (e.Node.Nodes.Count != 0)
{
if (e.Node.IsExpanded && mbmpExpandIcon != null)
{
e.Graphics.DrawImage(mbmpExpandIcon, 0, e.Bounds.Y + (int)(e.Bounds.Height - mbmpExpandIcon.Height) / 2, mbmpExpandIcon.Width, mbmpExpandIcon.Height);
}
else if (!e.Node.IsExpanded && mbmpCollapseIcon != null)
{
e.Graphics.DrawImage(mbmpCollapseIcon, 0, e.Bounds.Y + (int)(e.Bounds.Height - mbmpCollapseIcon.Height) / 2, mbmpCollapseIcon.Width, mbmpCollapseIcon.Height);
}
}
else if (mbmpNoneIcon != null)
{
e.Graphics.DrawImage(mbmpNoneIcon, 0, e.Bounds.Y + (int)(e.Bounds.Height - mbmpNoneIcon.Height) / 2, mbmpNoneIcon.Width, mbmpNoneIcon.Height);
}

}

Image NodeIcon = null;

Font NodeFont;
Color NodeForeColor;

if (e.Node.NodeFont != null)
NodeFont = e.Node.NodeFont;
else
NodeFont = this.Font;

if (e.Node.ForeColor != Color.Empty)
NodeForeColor = e.Node.ForeColor;
else
NodeForeColor = this.ForeColor;

int FontHeight = new Font(NodeFont.FontFamily, NodeFont.Size, GraphicsUnit.Pixel).Height;

Point ptNodeText = new Point();

if (this.ImageList != null)
{
if (e.Node.ImageIndex >= 0 && e.Node.ImageIndex < this.ImageList.Images.Count)
{
NodeIcon = this.ImageList.Images[e.Node.ImageIndex];
}
else if (e.Node.ImageKey != "")
{
NodeIcon = this.ImageList.Images[e.Node.ImageKey];
}

if (NodeIcon != null)
{
e.Graphics.DrawImage(NodeIcon, mintTextLeft, e.Bounds.Y + (int)(e.Bounds.Height - NodeIcon.Height) / 2, NodeIcon.Width, NodeIcon.Height);
ptNodeText.X = mintTextLeft + NodeIcon.Width + 2;
ptNodeText.Y = e.Bounds.Y + (e.Bounds.Height - FontHeight) / 2;
}
else
{
ptNodeText.X = mintTextLeft;
ptNodeText.Y = e.Bounds.Y + (e.Bounds.Height - FontHeight) / 2;
}

}
else
{
ptNodeText.X = mintTextLeft;
ptNodeText.Y = e.Bounds.Y + (e.Bounds.Height - FontHeight) / 2;
}

e.Graphics.DrawString(e.Node.Text, NodeFont, new SolidBrush(NodeForeColor), ptNodeText);


}


}
}
machunyanlengxue 2007-10-11
  • 打赏
  • 举报
回复
谁能告诉我怎么做啊????????????????
machunyanlengxue 2007-10-10
  • 打赏
  • 举报
回复
你什么时候给我发过来啊


我急用啊


guchunjia0221 2007-10-10
  • 打赏
  • 举报
回复
可以发给我一份么
guchunjia0221@sina.com
谢谢!
machunyanlengxue 2007-10-09
  • 打赏
  • 举报
回复
改了以后,怎么调用啊
machunyanlengxue 2007-10-09
  • 打赏
  • 举报
回复
是不是改_SelectNodeColor = Color.Blue;这一项


怎么不行啊
machunyanlengxue 2007-10-09
  • 打赏
  • 举报
回复
我的邮箱是:machunyanlengxue@126.com
tao77 2007-10-09
  • 打赏
  • 举报
回复
我正好遇到这个问题,重写了一个TreeView,可以改变选中后的背景色,要的话发邮箱,我发给你。
chanviva 2007-10-08
  • 打赏
  • 举报
回复
顶一下
machunyanlengxue 2007-10-08
  • 打赏
  • 举报
回复
命名空间找到了


但提示 “TreeView”是不明确的引用

什么意思啊
machunyanlengxue 2007-10-08
  • 打赏
  • 举报
回复
TreeViewCancelEventArgs 和 TreeViewEventArgs 在什么命名空间啊
JoineChoi 2007-10-08
  • 打赏
  • 举报
回复
你新建一个项目,然后再新建一个类,把上面的代码全部代替这个类原来自动生成的代码(命名空间WindowsApplication1改为和你项目一样的命名空间)。然后编译一下,在工具栏的最上方就会出现这个myTreeView,你拉进来试一下
JoineChoi 2007-10-08
  • 打赏
  • 举报
回复
那你用 System.Windowns.Forms.TreeView
这样它的命名空间就明确了。
machunyanlengxue 2007-10-08
  • 打赏
  • 举报
回复
谁告诉我应该怎么做啊
machunyanlengxue 2007-10-07
  • 打赏
  • 举报
回复
怎么重绘一下啊

能具体点吗
JoineChoi 2007-10-07
  • 打赏
  • 举报
回复
using System;
using System.Windows.Forms;
using System.Drawing;

namespace WindowsApplication1
{
public partial class myTreeView : TreeView
{
public myTreeView()
{
_SelectNodeColor = Color.Blue;
_SelectNodeFont = this.Font;
}

// 所选节点的文本颜色
Color _SelectNodeColor;
public Color SelectNodeColor
{
get { return _SelectNodeColor; }
set { _SelectNodeColor = value; }
}

//所选节点的文本字体
Font _SelectNodeFont;
public Font SelectNodeFont
{
get { return _SelectNodeFont; }
set { _SelectNodeFont = value; }
}

//前一个选择的节点还原显示效果
protected override void OnBeforeSelect(TreeViewCancelEventArgs e)
{
TreeNode node = e.Node.TreeView.SelectedNode;

if (node != null)
{
node.ForeColor = this.ForeColor;
node.NodeFont = this.Font;
}
}

//设置选择的节点的显示效果
protected override void OnAfterSelect(TreeViewEventArgs e)
{
e.Node.ForeColor = _SelectNodeColor;
e.Node.NodeFont=_SelectNodeFont;
}

//去掉焦点框。(还是会闪一下的,要真正不要焦点,需要栏截API,开销太大了)
protected override void OnGotFocus(EventArgs e)
{
this.Parent.SelectNextControl(this, true, false, true, true);
}
}
}
JoineChoi 2007-10-07
  • 打赏
  • 举报
回复
继承TreeView控件,重绘一下。
machunyanlengxue 2007-10-06
  • 打赏
  • 举报
回复
我是在03里做的


哪位好心朋友帮帮小妹我啊


我急着交工呢

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧