我正在覆盖TreeView
,以便我可以使用更好的颜色突出显示节点。作为应用程序选项的一部分,我想让用户在选择和取消选择时更改TreeView
的字体和字体颜色。代码如下:
class MyTreeView : TreeView
{
// Create a Font object for the node tags and HotTracking.
private Font hotFont;
private Font tagFont = new Font("Helvetica", Convert.ToSingle(8.0), FontStyle.Bold);
#region Accessors.
public Font hotTrackFont
{
get { return this.hotFont; }
set { this.hotFont = value; }
}
//public string unFocusedColor
//{
// get { return this.strDeselectedColor; }
// set { this.strDeselectedColor = value; }
//}
//public string focusedColor
//{
// get { return this.strSelectedColor; }
// set { this.strSelectedColor = value; }
//}
#endregion
public MyTreeView()
{
this.HotTracking = true;
this.DrawMode = TreeViewDrawMode.OwnerDrawText;
hotFont = new Font(this.Font.FontFamily, this.Font.Size, FontStyle.Underline);
}
// Override the drawMode of TreeView.
protected override void OnDrawNode(DrawTreeNodeEventArgs e)
{
TreeNodeStates treeState = e.State;
Font treeFont = e.Node.NodeFont ?? e.Node.TreeView.Font;
// Colors.
Color foreColor = e.Node.ForeColor;
// Like with the hotFont I want to be able to change these dynamically...
string strDeselectedColor = @"#6B6E77", strSelectedColor = @"#94C7FC";
Color selectedColor = System.Drawing.ColorTranslator.FromHtml(strSelectedColor);
Color deselectedColor = System.Drawing.ColorTranslator.FromHtml(strDeselectedColor);
// New brush.
SolidBrush selectedTreeBrush = new SolidBrush(selectedColor);
SolidBrush deselectedTreeBrush = new SolidBrush(deselectedColor);
// Set default font color.
if (foreColor == Color.Empty)
foreColor = e.Node.TreeView.ForeColor;
// Draw bounding box and fill.
if (e.Node == e.Node.TreeView.SelectedNode)
{
// Use appropriate brush depending on if the tree has focus.
if (this.Focused)
{
foreColor = SystemColors.HighlightText;
e.Graphics.FillRectangle(selectedTreeBrush, e.Bounds);
ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds, foreColor, SystemColors.Highlight);
TextRenderer.DrawText(e.Graphics, e.Node.Text, treeFont, e.Bounds,
foreColor, TextFormatFlags.GlyphOverhangPadding);
}
else
{
foreColor = SystemColors.HighlightText;
e.Graphics.FillRectangle(deselectedTreeBrush, e.Bounds);
ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds, foreColor, SystemColors.Highlight);
TextRenderer.DrawText(e.Graphics, e.Node.Text, treeFont, e.Bounds,
foreColor, TextFormatFlags.GlyphOverhangPadding);
}
}
else
{
if ((e.State & TreeNodeStates.Hot) == TreeNodeStates.Hot)
{
e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
TextRenderer.DrawText(e.Graphics, e.Node.Text, hotFont, e.Bounds,
System.Drawing.Color.Black, TextFormatFlags.GlyphOverhangPadding);
}
else
{
e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
TextRenderer.DrawText(e.Graphics, e.Node.Text, treeFont, e.Bounds,
foreColor, TextFormatFlags.GlyphOverhangPadding);
}
}
}
}
正如您所看到的,我正在使用似乎有效的类的访问器更改Font
hotFont
。但是,当我尝试编辑聚焦/未聚焦节点的颜色时,VS2010会崩溃!究竟是什么导致了这种行为,我怎样才能实现我的目标?
答案 0 :(得分:1)
当我启用这些注释掉的属性并将变量添加到控件的范围时,发布的代码不会重新创建错误。
但有几点注意事项:
我会使用实际颜色而不是字符串属性:
private Color _UnfocusedColor = ColorTranslator.FromHtml(@"#94C7FC");
private Color _FocusedColor = ColorTranslator.FromHtml(@"#6B6E77");
public Color UnfocusedColor
{
get { return _UnfocusedColor; }
set { _UnfocusedColor = value; }
}
public Color FocusedColor
{
get { return _FocusedColor; }
set { _FocusedColor = value; }
}
此外,请确保丢弃绘图对象(例如SolidBrush对象),或将其包装在Using(...){}
块中。