WinForm自定義ListBox顯示樣式

WinForm自定義ListBox顯示樣式,多列分不同顏色顯示,效果如下圖:




首先向winForm窗口拖入一個ListBox控件,命名爲lstConsole,同時將DrawMode設置爲:OwnerDrawFixed,這裏一定要注意否則我們接下來的工作都不會起作用。


然後我們來自定義ListBoxItem,代碼如下:


    public class ColoredListBoxItem
    {
        /// <summary>
        /// creates a new ColoredListBoxItem
        /// </summary>
        /// <param name="prefix">the prefix which will be used</param>
        /// <param name="text">the real message</param>
        /// <param name="color">the color of both</param>
        public ColoredListBoxItem(DateTime time, string prefix, string text, Color color)
        {
            Time = time;
            Text = text;
            Prefix = prefix;
            TextColor = color;
        }

        /// <summary>
        /// the real message
        /// </summary>
        public DateTime Time { get; set; }

        /// <summary>
        /// the prefix of the text
        /// </summary>
        public string Prefix { get; set; }

        /// <summary>
        /// the real message
        /// </summary>
        public string Text { get; set; }

        /// <summary>
        /// the color of the message
        /// </summary>
        public Color TextColor { get; set; }
    }

    public enum LogType
    {
        /// <summary>
        /// OpenVPN changed the internal state.l
        /// </summary>
        Created,

        /// <summary>
        /// The management interface wants to say something.
        /// </summary>
        Changed,

        /// <summary>
        /// A "normal" message is logged by OpenVPN via Management Interface.
        /// </summary>
        Deleted,

        /// <summary>
        /// A debug message is sent. This is primary for internal usage.
        /// </summary>
        Renamed
    }



接下來,切換到事件,向lstConsole添加DrawItem事件:lstConsole_DrawItem,代碼如下:


private void lstConsole_DrawItem(object sender, DrawItemEventArgs e)
        {
            // just in case the list is empty...
            if (e.Index == -1)
                return;

            // prefixes are drawed bold
            Font prefixFont = new Font(e.Font, FontStyle.Bold);

            ColoredListBoxItem li = (ColoredListBoxItem)((ListBox)sender).Items[e.Index];

            Brush br = new SolidBrush(li.TextColor);

            // calculate the width of the longest time
            int timeWidth = (int)e.Graphics.MeasureString(" " + new DateTime(2222, 12, 22, 22, 22, 22, 222, CultureInfo.CurrentCulture.Calendar, DateTimeKind.Local).ToString(), prefixFont, e.Bounds.Width, StringFormat.GenericDefault).Width;
            // calculate the width of the longest prefix
            int prefixWidth = (int)e.Graphics.MeasureString(" [Management]", prefixFont, e.Bounds.Width, StringFormat.GenericDefault).Width;


            // prepare the prefix
            string prefix = "";
            switch (li.Prefix)
            {
                case "Created":
                    prefix = "[創建文件] ";
                    break;
                case "Changed":
                    prefix = "[修改文件] ";
                    break;
                case "Deleted":
                    prefix = "[刪除文件] ";
                    break;
                case "Renamed":
                    prefix = "[重命名文件] ";
                    break;
                default:
                    break;
            }

            e.DrawBackground();
            Rectangle newBounds = new Rectangle(e.Bounds.Location, e.Bounds.Size);

            // draw the time
            e.Graphics.DrawString(li.Time.ToString(), prefixFont, br, newBounds, StringFormat.GenericDefault);
            // calculate the new rectangle
            newBounds.X += timeWidth;
            newBounds.Width -= timeWidth;

            // draw the prefix
            e.Graphics.DrawString(prefix, prefixFont, br, newBounds, StringFormat.GenericDefault);
            // calculate the new rectangle
            newBounds.X += prefixWidth;
            newBounds.Width -= prefixWidth;

            // draw the text
            e.Graphics.DrawString(
                li.Text, e.Font, br, newBounds.X, newBounds.Y,
                StringFormat.GenericDefault);

            // draw the focus
            e.DrawFocusRectangle();
        }




最後一步,向ListBox添加Item顯示的數據:


        public void AddLog(LogType prefix, string text)
        {
            if (lstConsole.InvokeRequired)
            {
                try
                {
                    //lstConsole.BeginInvoke(new UtilsHelper.Action<LogType, string>(AddLog), prefix, text);
                }
                catch (ObjectDisposedException)
                {
                }
                return;
            }

            Color rowColor;
            switch (prefix)
            {
                case LogType.Created:
                    rowColor = Color.Green;
                    break;

                case LogType.Changed:
                    rowColor = Color.DarkBlue;
                    break;

                case LogType.Deleted:
                    rowColor = Color.Brown;
                    break;

                default: // e.g. State
                    rowColor = Color.Black;
                    break;
            }

            lstConsole.BeginUpdate();
            if (lstConsole.Items.Count == 2048)
                lstConsole.Items.RemoveAt(0);

            lstConsole.Items.Add(new ColoredListBoxItem(DateTime.Now, prefix.ToString(), text, rowColor));

            int h = lstConsole.ClientSize.Height - lstConsole.Margin.Vertical;
            int i = lstConsole.Items.Count - 1;
            while (h >= 0 && i > 0)
            {
                int nh = lstConsole.GetItemHeight(i);

                if (nh > h)
                    break;
                else
                {
                    h -= nh;
                    i--;
                }
            }

            lstConsole.TopIndex = i;
            lstConsole.EndUpdate();
        }


展示效果軟件下載地址:下載Windows文件監視器



轉自:WinForm自定義ListBox顯示樣式


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章