2
더 나은 색상을 사용하여 노드를 강조 표시 할 수 있도록 TreeView
을 재정의했습니다. 응용 프로그램 옵션의 일부로 선택 및 선택 취소시 사용자가 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
을 변경하고 볼 수 있듯이. 그러나 초점을 맞춘/unfocused 노드의 색상을 편집하려고하면 VS2010이 충돌합니다! 이 행동을 정확히 일으키는 것은 무엇이며 어떻게하면 좋을까요?
focusedColor 및 unFocusedColor가 문자열로 간주됩니까? VS가 충돌하는 곳은 주석 처리 된 속성입니까? – LarsTech
네, 맞습니다. 지연된 응답을 드려 죄송합니다 ... – MoonKnight
오류를 복제 할 수 없습니다. tagItem에서 strDeselectedColor와 strSelectedColor 변수를 DrawItem에서 사용했던 기본값으로 추가하고 속성의 주석을 제거하고 DrawItem 이벤트의 변수 선언을 주석 처리했습니다. 실제 오류 메시지는 무엇입니까? – LarsTech