自定義樹形視圖
回答
使用TreeViewDrawMode.OwnerDrawText
所以縮進將由TreeView
進行調整。除此之外,你應該實施完整的繪畫。
public sealed class AdvancedTreeView : TreeView
{
public AdvancedTreeView()
{
DrawMode = TreeViewDrawMode.OwnerDrawText;
ShowLines = false;
AlternateBackColor = BackColor;
}
public Color AlternateBackColor { get; set; }
protected override void OnDrawNode(DrawTreeNodeEventArgs e)
{
// background
Color backColor = (GetTopNodeIndex(e.Node) & 1) == 0 ? BackColor : AlternateBackColor;
using (Brush b = new SolidBrush(backColor))
{
e.Graphics.FillRectangle(b, new Rectangle(0, e.Bounds.Top, ClientSize.Width, e.Bounds.Height));
}
// icon
if (e.Node.Nodes.Count > 0)
{
Image icon = GetIcon(e.Node.IsExpanded); // TODO: true=down;false:right
e.Graphics.DrawImage(icon, e.Bounds.Left - icon.Width - 3, e.Bounds.Top);
}
// text (due to OwnerDrawText mode, indenting of e.Bounds will be correct)
TextRenderer.DrawText(e.Graphics, e.Node.Text, Font, e.Bounds, ForeColor);
// indicate selection (if not by backColor):
if ((e.State & TreeNodeStates.Selected) != 0)
ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds);
}
private int GetTopNodeIndex(TreeNode node)
{
while (node.Parent != null)
node = node.Parent;
return Nodes.IndexOf(node);
}
}
爲了得到與截圖類似的結果,只需設置顏色即可。
advancedTreeView1.BackColor = Color.DeepSkyBlue;
advancedTreeView1.AlternateBackColor = Color.LightBlue;
我試過你的解決方案,但我可能錯過了一些東西。我創建了一個'AdvancedTreeView',我用child添加了10個節點(之前用一個標準的'TreeView'測試過),但它只顯示一個背景DeepSkyBlue着色。我在'OnDrawNode'中添加了一個斷點,這個函數永遠不會被調用。 –
如果'DrawMode'只是'Normal',則調用它。 – taffer
我修改了我的樹,現在它正在工作(可能沒有添加節點)。謝謝 –
只需在TreeView的屬性中將DrawMode設置爲「OwnerDrawAll」即可。請記住,你必須自己繪製它,並且必須處理TreeView_DrawNode事件。下面是事件處理的例子:
private void TreeListView_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
if (e.Bounds.Height == 0)
return;
e.Graphics.FillRectangle(new SolidBrush((e.Node.Parent?.Index ?? e.Node.Index) % 2 == 0 ? Color.Blue : Color.Aqua), e.Bounds);
if (e.Node.Nodes.Count > 0)
{
if (!e.Node.IsExpanded)
e.Graphics.FillPolygon(Brushes.Red,
new[]
{
new PointF(e.Bounds.X + e.Bounds.Height/10, e.Bounds.Y + e.Bounds.Height/10),
new PointF(e.Bounds.X + e.Bounds.Height/10, e.Bounds.Y + e.Bounds.Height * 0.9f),
new PointF(e.Bounds.X + e.Bounds.Height, e.Bounds.Y + e.Bounds.Height/2)
});
else
e.Graphics.FillPolygon(Brushes.Red,
new[]
{
new PointF(e.Bounds.X + e.Bounds.Height/10, e.Bounds.Y + e.Bounds.Height/10),
new PointF(e.Bounds.X + e.Bounds.Height, e.Bounds.Y + e.Bounds.Height/10),
new PointF(e.Bounds.X + e.Bounds.Height/2, e.Bounds.Y + e.Bounds.Height)
});
}
e.Graphics.DrawString(e.Node.Text, new Font(FontFamily.GenericMonospace, e.Bounds.Height * 0.7f),
new SolidBrush(Color.Black),
new Rectangle(e.Bounds.X + e.Bounds.Height, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
}
是否有'TextRenderer.DrawText'和'e.Graphics.DrawString'之間的差? –
有 - 看看這個:http://stackoverflow.com/questions/8283631/graphics-drawstring-vs-textrenderer-drawtextwhich-can-deliver-better-quality – MetaColon
- 1. QT自定義樹視圖
- 2. 自定義樹形視圖的展開/摺疊圖標
- 3. Google可視化樹形圖 - 如何創建自定義文本?
- 4. MOODLE ::自定義樹形菜單塊
- 5. 自定義圓形視圖。放置視圖
- 6. DirectShow .NET自定義圖形
- 7. 自定義UISwitch圖形?
- 8. 樹形視圖中的項
- 9. VC++樹形視圖控件
- 10. 樹形視圖組合框
- 11. DataBind Telerik樹形視圖
- 12. MySql的PHP樹形視圖
- 13. 將IHierarchicalEnumerable綁定到樹視圖,並指定自定義圖像和導航Url
- 14. 自定義視圖到現有視圖
- 15. 自定義視圖覆蓋視圖Android
- 16. 添加視圖自定義視圖組
- 17. 樹視圖控件中的自定義項目
- 18. 德爾福:樹視圖的自定義提示
- 19. 使用模板自定義Kendo樹視圖時的問題
- 20. 在C#中創建自定義樹視圖WinForms
- 21. 自定義CSS的odoo樹視圖按鈕
- 22. 自定義視圖顯示爲組件樹中的嵌套視圖
- 23. android如何更新視圖以包含自定義圖形?
- 24. 如何將圖像視圖轉換爲自定義形狀(swift3)
- 25. Swift - Playground - 核心圖形/核心文本/自定義視圖
- 26. 自定義列表視圖
- 27. Drupal自定義視圖
- 28. 自定義視圖和Drop
- 29. 獲取自定義視圖
- 30. Symfony EasyAdminBundle自定義視圖
'treeView1.DrawMode = TreeViewDrawMode.OwnerDrawAll;' – LarsTech