拒絕單色調,重寫Label控件


在WinForm中,微軟提供的Label中文字只能是一種顏色:要么全是黑色,要么全是紅色或者其它顏色。當我們為了強調某些文字,讓它用紅色顯示時只能新建一個Lable對象,設置它FontColor為紅色。但是如果在一行文字內嵌三個紅色,那豈不是要拼接六個Label控件?坑爹啊,這么麻煩,我們還不如自己重寫一下Label控件,讓它支持多顏色的文字呢。

OK。Let's do it.

要讓不同的文字顯示不同的顯示,我們必須告訴Label控件哪些文字對應哪種顏色。在這里,我們定義一個字典來存儲。

        /// <summary>
        ///KeyValuePair中 string存儲文字,Color為對應的顏色
        /// </summary>
        private Dictionary<string, KeyValuePair<string, Color>> strColorDic = new Dictionary<string, KeyValuePair<string, Color>>();

設置對應的公共屬性:

 public Dictionary<string, KeyValuePair<string, Color>> StrColorDic
        {
            get { return strColorDic; }
            set { strColorDic = value; }
        }

接下來就是最重要的重寫OnPaint事件了。在這里如果我們定義的strColorDic中有值,就用g.DrawString將strColorDic中的文本寫到Label控件上。如果strColorDic中沒有值,就調用base.OnPaint(e)將Text屬性中的文字寫到Label控件上。

注意繪制時起點的X軸要作相應的變動。

 /// <summary>
        /// 重寫OnPaint,如果strColorDic不為空,就將strColorDic中文本
        /// 繪制到Label中
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {    
            Graphics g = e.Graphics;
            float X = 0.0F;
            //如果字典中的鍵>0
            if (strColorDic.Keys.Count > 0)
            {
                foreach (string key in strColorDic.Keys)
                {
                    //根據Color獲取對應的Brush
                    Brush brush = new SolidBrush(strColorDic[key].Value);
                    g.DrawString(strColorDic[key].Key, this.Font, brush, new PointF(X, 0.0F));
                    //繪制的X軸坐標要做相應的移動
                    X += (strColorDic[key].Key.Length) * Font.Height - Font.Size;
                    this.Text += strColorDic[key].Key;
                } 
            }
            else if(this.Text!="")
            {
                base.OnPaint(e);
            } 
        }

大功告成,很簡單吧。我們新建一個WinForm窗體來測試一下。在Form1_Load中添加下面代碼:

  Dictionary<string, KeyValuePair<string, Color>> dic = new Dictionary<string, KeyValuePair<string, Color>>();
            dic.Add("1",new KeyValuePair<string,Color>("我是紅色",Color.Red));
            dic.Add("2", new KeyValuePair<string, Color>("我是藍色", Color.Blue));
            dic.Add("3", new KeyValuePair<string, Color>("我是黃色", Color.Yellow));
            this.myLabel1.StrColorDic = dic;

可以看到運行效果:

 

 不過還有一個Bug,如果this.myLabel1.Text(myLabel1即為我們的自定義控件)的值為空的話,它不會觸發我們重寫的OnPaint事件。我們在代碼中給它賦的值也就顯示不出來。

 其實我們可以在給myLabel1賦Dic時隨便給myLabel1.Text賦個值就行了,如:

 Dictionary<string, KeyValuePair<string, Color>> dic = new Dictionary<string, KeyValuePair<string, Color>>();
            dic.Add("1", new KeyValuePair<string, Color>("我是紅色", Color.Red));
            dic.Add("2", new KeyValuePair<string, Color>("我是藍色", Color.Blue));
            dic.Add("3", new KeyValuePair<string, Color>("我是黃色", Color.Yellow));
            //myLabel1.Text不能為空,否則無法觸發 OnPaint事件
            this.myLabel1.Text = "  ";
            this.myLabel1.StrColorDic = dic;

代碼下載:點我

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM