練習重繪TreeView控件
前兩天看過別人做的"TreeView实现类似Outlook在收件箱后面显示新邮件数"的例子(原文:http://www.cnblogs.com/anjou/archive/2007/11/10/955370.html),自己也照著做了一下,學習一下控件重繪。練習如下:
代碼:
結果如上圖。
代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Globalization;
namespace WindowsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
TreeStudy();
}
/// <summary>
/// 定義TreeNode
/// </summary>
private void TreeStudy()
{
//定義 TreeView 的節點。 並增加節點
TreeNode Root = new TreeNode("lygckt@163.com");
Root.Name = "root";
Root.Nodes.Add("InBox", "收件箱");
Root.Nodes.Add("OutBox", "發件箱");
Root.Nodes.Add("SentBox", "已發送郵件");
Root.Nodes.Add("Spam", "垃圾箱");
treeViewValue.Nodes.Add(Root);
//定義另外一個TreeView 的節點
TreeNode SaveFolder = new TreeNode("存儲文件夾");
SaveFolder.Name = "folder";
SaveFolder.Nodes.Add("saveReadBox","已閱讀");
SaveFolder.Nodes.Add("saveRoughBox","草稿");
treeViewValue.Nodes.Add(SaveFolder);
//展開節點
treeViewValue.ExpandAll();
//設定繪制模式;OwnerDrawText:表示TreeView 節點的標籤部分是手動描繪的。其他節點項目則是由作業系統描繪,包含圖示、核取方塊、加號和減號,以及連接節點的線條。
treeViewValue.DrawMode = TreeViewDrawMode.OwnerDrawText;
//重繪事件
treeViewValue.DrawNode += new DrawTreeNodeEventHandler(treeViewValue_DrawNode);
//btnNew按鈕事件
this.btnNew.Click += new EventHandler(btnNew_Click);
//btnRead按鈕事件
this.btnRead.Click += new EventHandler(btnRead_Click);
}
/// <summary>
/// ReadMail按鈕事件方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void btnRead_Click(object sender, EventArgs e)
{
//重新定義收件箱,便於重繪。並設置Tag作為重繪的填加值
TreeNode inBoxNode = treeViewValue.Nodes["root"].Nodes["InBox"];
inBoxNode.Tag = null;
//重新定義已閱讀,便於重繪。並設置Tag作為重繪的填加值
TreeNode SaveSentBox = treeViewValue.Nodes["folder"].Nodes["saveReadBox"];
SaveSentBox.Tag = 3;
//進行重繪
treeViewValue.Refresh();
}
/// <summary>
/// New Mail按鈕事件方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void btnNew_Click(object sender, EventArgs e)
{
TreeNode inBoxNode = treeViewValue.Nodes["root"].Nodes["InBox"];
inBoxNode.Tag = 3;
TreeNode SaveSentBox = treeViewValue.Nodes["folder"].Nodes["saveReadBox"];
SaveSentBox.Tag = null;
treeViewValue.Refresh();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void treeViewValue_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
//由系統繪制
e.DrawDefault = true;
//在節點后邊繪制"新郵件數"。類似郵件數(3)
if (e.Node.Tag!=null)
{
string newMail = string.Format("({0})", e.Node.Tag.ToString());
e.Graphics.DrawString(newMail, e.Node.TreeView.Font, Brushes.Blue, e.Bounds.Right, e.Bounds.Top);
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Globalization;
namespace WindowsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
TreeStudy();
}
/// <summary>
/// 定義TreeNode
/// </summary>
private void TreeStudy()
{
//定義 TreeView 的節點。 並增加節點
TreeNode Root = new TreeNode("lygckt@163.com");
Root.Name = "root";
Root.Nodes.Add("InBox", "收件箱");
Root.Nodes.Add("OutBox", "發件箱");
Root.Nodes.Add("SentBox", "已發送郵件");
Root.Nodes.Add("Spam", "垃圾箱");
treeViewValue.Nodes.Add(Root);
//定義另外一個TreeView 的節點
TreeNode SaveFolder = new TreeNode("存儲文件夾");
SaveFolder.Name = "folder";
SaveFolder.Nodes.Add("saveReadBox","已閱讀");
SaveFolder.Nodes.Add("saveRoughBox","草稿");
treeViewValue.Nodes.Add(SaveFolder);
//展開節點
treeViewValue.ExpandAll();
//設定繪制模式;OwnerDrawText:表示TreeView 節點的標籤部分是手動描繪的。其他節點項目則是由作業系統描繪,包含圖示、核取方塊、加號和減號,以及連接節點的線條。
treeViewValue.DrawMode = TreeViewDrawMode.OwnerDrawText;
//重繪事件
treeViewValue.DrawNode += new DrawTreeNodeEventHandler(treeViewValue_DrawNode);
//btnNew按鈕事件
this.btnNew.Click += new EventHandler(btnNew_Click);
//btnRead按鈕事件
this.btnRead.Click += new EventHandler(btnRead_Click);
}
/// <summary>
/// ReadMail按鈕事件方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void btnRead_Click(object sender, EventArgs e)
{
//重新定義收件箱,便於重繪。並設置Tag作為重繪的填加值
TreeNode inBoxNode = treeViewValue.Nodes["root"].Nodes["InBox"];
inBoxNode.Tag = null;
//重新定義已閱讀,便於重繪。並設置Tag作為重繪的填加值
TreeNode SaveSentBox = treeViewValue.Nodes["folder"].Nodes["saveReadBox"];
SaveSentBox.Tag = 3;
//進行重繪
treeViewValue.Refresh();
}
/// <summary>
/// New Mail按鈕事件方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void btnNew_Click(object sender, EventArgs e)
{
TreeNode inBoxNode = treeViewValue.Nodes["root"].Nodes["InBox"];
inBoxNode.Tag = 3;
TreeNode SaveSentBox = treeViewValue.Nodes["folder"].Nodes["saveReadBox"];
SaveSentBox.Tag = null;
treeViewValue.Refresh();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void treeViewValue_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
//由系統繪制
e.DrawDefault = true;
//在節點后邊繪制"新郵件數"。類似郵件數(3)
if (e.Node.Tag!=null)
{
string newMail = string.Format("({0})", e.Node.Tag.ToString());
e.Graphics.DrawString(newMail, e.Node.TreeView.Font, Brushes.Blue, e.Bounds.Right, e.Bounds.Top);
}
}
}
}
結果如上圖。