■ TreeView 클래스 : DrawNode 이벤트를 사용해 커스텀 노드 그리기

----------------------------------------------------------------------------------------------------


TestProject.zip

 

MainForm.cs

 

 

using System.Drawing;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region Field

 

        /// <summary>

        /// 알림 건수 폰트

        /// </summary>

        private Font noticeFont = new Font("Times New Roman", 8f, FontStyle.Bold);

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            this.treeView.DrawMode = TreeViewDrawMode.OwnerDrawText;

 

            #region 트리 노드들을 추가한다.

 

            TreeNode treeNode;

 

            for(int i = 1; i < 4; ++i)

            {

                treeNode = this.treeView.Nodes.Add(string.Format("과제 {0}", i));

 

                for(int j = 1; j < 4; ++j)

                {

                    treeNode.Nodes.Add(string.Format("하위 과제 {0}", j));

                }

            }

 

            this.treeView.ExpandAll();

 

            this.treeView.Nodes[1].Nodes[0].Tag = "(12)";

 

            this.treeView.SelectedNode = treeView.Nodes[1].Nodes[0];

 

            this.treeView.Nodes[2].Nodes[1].Tag = "(99)";

 

            #endregion

 

            this.treeView.DrawNode += treeView_DrawNode;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 트리 뷰 노드 그리기 - treeView_DrawNode(sender, e)

 

        /// <summary>

        /// 트리 뷰 노드 그리기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e)

        {

            TreeView treeView = sender as TreeView;

 

            e.DrawDefault = true;

 

            if(e.Node.Tag != null)

            {

                string notice = e.Node.Tag.ToString();

 

                Size noticeSize = e.Graphics.MeasureString(notice, treeView.Font).ToSize();

 

                e.Graphics.DrawString

                (

                    e.Node.Tag.ToString(),

                    this.noticeFont,

                    Brushes.DarkGreen,

                    e.Bounds.Right + 2,

                    e.Bounds.Top + 2

                );

            }

        }

 

        #endregion

    }

}

 

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요