当前位置:   article > 正文

C# 自定义Label实现 指定字符串(关键词)高亮显示(字体、颜色)_c#自定义lable

c#自定义lable

C# 自定义Label实现 指定字符串(关键词)高亮显示(字体、颜色)

原来是搞android的,本来自己就菜,现在由于项目需要开始着手弄C#、WPF,虽然了解一些,毕竟只是皮毛,唉,苦不堪言啊,还是得倚靠万能的互联网啊!
需求:提示用户的文字,但是该文字中需要高亮显示指定字符串,颜色字体等要不一样,比如提示"请使用 支付宝 扫码支付" ,其中要对“支付宝”三个字高亮显示。就像这样

这里写图片描述

第一反应就是使用 自带的Label控件,但是普通的Label是无法实现的,然后想了个笨办法,使用多个Label拼接,这个。。。如果需要高亮显示的字符串多了,那得要多少个Label啊,行不通。然后网上有说使用RichTextBox,试了一下,效果确实可以达到,但是有些差强人意,比如RichTextBox用户可以选中提示文字,试了设置enable=false后,确实是不可以选中文字了,但是系统默认的控件不可用的背景色是灰色,属性无法更改,也可能是自己对这些控件不够了解,或许可以通过重写Paint方法来更改,既然要重写Paint方法,何必使用RichTextBox这个控件呢?所以自定义Label就登场了:
###效果图:
这里写图片描述
这里写图片描述
#####两个重点

  • 需要显示的字符串如何知道哪个是关键词
  • 设置不同的字体后如何让文本居中显示

####1、拆分字符串

        List<string> data = new List<string>();

        //将所给字符串根据关键词 拆分成数组
        private void splitText(string txt)
        {
            if (txt == null || txt.Length < 1)
                return;
            if (txt.Equals(keyword))//当前字符串就是关键词
            {
                data.Add(txt);
                return;
            }
            //拆分文字 拿到关键词
            int startIndex = txt.IndexOf(keyword);//关键词索引

            if (startIndex == -1)//该字符串不包含关键词 直接添加到集合
            {
                data.Add(txt);
                return;
            }
            //包含关键词 还有其他字符窜
            int endIndex = startIndex + keyword.Length;
            if (startIndex == 0)//关键词在首(第一次出现 可能后面还包含有关键词)
            {
                string k = txt.Substring(startIndex, endIndex);//拿到关键词
                data.Add(k);
                splitText(txt.Substring(endIndex));
            }
            else if (endIndex == txt.Length - 1)//关键词在末(当前字符串只要一个关键词)
            {
                splitText(txt.Substring(0, startIndex));
                string k = txt.Substring(startIndex, keyword.Length);//拿到关键词
                data.Add(k);//添加到集合
            }
            else//关键词在中
            {
                splitText(txt.Substring(0, startIndex));
                string k = txt.Substring(startIndex, keyword.Length);//拿到关键词
                data.Add(k);
                splitText(txt.Substring(endIndex));
            }
        }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 代码注释很详细了,就是递归拆分获取字符串数组,以便后面遍历画到控件上

####2、 重写Paint方法

  private void l_Paint(object sender, PaintEventArgs e)
 {
            Graphics g = e.Graphics;//获取画笔
            //设置背景色
            if (mBackColor != null)
                g.FillRectangle(mBackColor, 0, 0, this.Width, this.Height);//填充控件大小

            if (mText == null || mText.Length < 1)
            {
                return;
            }

            data.Clear();
            splitText(mText);//拆分字符串
            //文字居中显示 dx水平偏移  dy垂直偏移
            //使用Graphics 的 MeasureString()方法测量指定字体的字符串宽高
            float dx = (this.Width - (g.MeasureString(mText, font)).Width) / 2.0f;
            float dy = (this.Height - (g.MeasureString(mText, font)).Height) / 2.0f;
            if (keyword == null || keyword.Length < 1)//没有关键词  关键词为空
            {
                g.DrawString(mText, font, mNormalColor, dx, dy);
                return;
            }
            //拆分文字 拿到关键词
            int startIndex = mText.IndexOf(keyword);//关键词索引
            int endIndex = startIndex + keyword.Length;
            string[] chunks = data.ToArray();//
            if (keyfont != null)//当设置了关键词字体时  需要根据该字体测量 偏移量
            {
                float ddx = 0;
                float ddy = 0;
                ddx = this.Width;
                //遍历
                for (int i = 0; i < chunks.Length; i++)
                {
                    if (chunks[i].Equals(keyword))//关键词
                    {
                        ddx -= g.MeasureString(keyword, keyfont).Width;
                    }
                    else
                    {
                        ddx -= g.MeasureString(chunks[i], font).Width;
                    }

                }
                ddx = ddx / 2.0f;
                if (ddx < 0)
                    ddx = 0;
                //遍历着色 
                for (int i = 0; i < chunks.Length; i++)
                {
                    if (chunks[i].Equals(keyword))//关键词
                    {
                        ddy = (this.Height - (g.MeasureString(keyword, keyfont)).Height) / 2.0f;
                        g.DrawString(chunks[i], keyfont, mKeyColor, ddx, ddy);
                        //水平偏移自增
                        ddx += (g.MeasureString(chunks[i], keyfont)).Width;
                    }
                    else
                    {
                        ddy = (this.Height - (g.MeasureString(chunks[i], font)).Height) / 2.0f;
                        g.DrawString(chunks[i], font, mNormalColor, ddx, ddy);
                        //水平偏移自增
                        ddx += (g.MeasureString(chunks[i], font)).Width;
                    }
                }
            }
            else
            {
                //遍历着色 
                for (int i = 0; i < chunks.Length; i++)
                {
                    if (chunks[i].Equals(keyword))//关键词
                    {

                        g.DrawString(chunks[i], font, mKeyColor, dx, dy);
                    }
                    else
                    {
                        g.DrawString(chunks[i], font, mNormalColor, dx, dy);

                    }
                    //水平偏移自增
                    dx += (g.MeasureString(chunks[i], font)).Width;
                }
            }
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 代码注释得也很详细了,首先根据关键词拆分字符串,然后遍历数组,需要根据设置的关键词的字体来计算偏移量,以便最后文字居中显示

完整代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace lColorfulLabel
{
    public partial class lColorfulLabel : UserControl
    {



        private string txt;
        public string mText//显示文本
        {
            get { return txt; }
            set
            {
                txt = value;
                l.Invalidate();
            }
        }

        private string keyword;
        public string mKeyword//关键词
        {
            get { return keyword; }
            set
            {
                keyword = value;
                l.Invalidate();
            }
        }
        private Font font;
        public Font mFont//字体样式
        {
            get { return font; }
            set
            {
                font = value;
                l.Invalidate();
            }
        }


        private Font keyfont;
        public Font mKeyFont//关键词字体样式
        {
            get { return keyfont; }
            set
            {
                keyfont = value;
                l.Invalidate();
            }
        }

        private SolidBrush normalColor;
        public SolidBrush mNormalColor//普通字颜色
        {
            get { return normalColor; }
            set
            {
                normalColor = value;
                l.Invalidate();
            }
        }

        private SolidBrush keyColor;//关键词颜色
        public SolidBrush mKeyColor//关键词颜色
        {
            get { return keyColor; }
            set
            {
                keyColor = value;
                l.Invalidate();
            }
        }
        private SolidBrush backColor;//背景色
        public SolidBrush mBackColor//背景色
        {
            get { return backColor; }
            set
            {
                backColor = value;
                l.Invalidate();
            }
        }

        public lColorfulLabel()
        {
            InitializeComponent();

            mText = "请使用支付宝扫码支付";
            keyword = "支付宝";
            normalColor = new SolidBrush(Color.Black);//默认文字黑色
            backColor = new SolidBrush(Color.White);//默认背景白色
            keyColor = new SolidBrush(Color.DeepSkyBlue);//默认关键词红色
            font = new System.Drawing.Font("微软雅黑", 12f);

            keyfont = new System.Drawing.Font("微软雅黑", 20f);

            l.Paint += l_Paint;




        }


        List<string> data = new List<string>();


        //将所给字符串根据关键词 拆分成数组
        private void splitText(string txt)
        {
            if (txt == null || txt.Length < 1)
                return;

            if (txt.Equals(keyword))//当前字符串就是关键词
            {
                data.Add(txt);
                return;
            }

            //拆分文字 拿到关键词
            int startIndex = txt.IndexOf(keyword);//关键词索引

            if (startIndex == -1)//该字符串不包含关键词 直接添加到集合
            {
                data.Add(txt);
                return;
            }

            //包含关键词 还有其他字符窜
            int endIndex = startIndex + keyword.Length;

            if (startIndex == 0)//关键词在首(第一次出现 可能后面还包含有关键词)
            {
                string k = txt.Substring(startIndex, endIndex);//拿到关键词
                data.Add(k);

                splitText(txt.Substring(endIndex));
            }
            else if (endIndex == txt.Length - 1)//关键词在末(当前字符串只要一个关键词)
            {
                splitText(txt.Substring(0, startIndex));
                string k = txt.Substring(startIndex, keyword.Length);//拿到关键词
                data.Add(k);//添加到集合

            }
            else//关键词在中
            {
                splitText(txt.Substring(0, startIndex));
                string k = txt.Substring(startIndex, keyword.Length);//拿到关键词
                data.Add(k);
                splitText(txt.Substring(endIndex));
            }




        }

        private void l_Paint(object sender, PaintEventArgs e)
        {

            Graphics g = e.Graphics;//获取画笔
            //设置背景色
            if (mBackColor != null)
                g.FillRectangle(mBackColor, 0, 0, this.Width, this.Height);//填充控件大小

            if (mText == null || mText.Length < 1)
            {
                return;
            }

            data.Clear();

            splitText(mText);

            //文字居中显示 dx水平偏移  dy垂直偏移
            //使用Graphics 的 MeasureString()方法测量指定字体的字符串宽高
            float dx = (this.Width - (g.MeasureString(mText, font)).Width) / 2.0f;
            float dy = (this.Height - (g.MeasureString(mText, font)).Height) / 2.0f;

            if (keyword == null || keyword.Length < 1)//没有关键词  关键词为空
            {
                g.DrawString(mText, font, mNormalColor, dx, dy);
                return;
            }

            //拆分文字 拿到关键词
            int startIndex = mText.IndexOf(keyword);//关键词索引
            int endIndex = startIndex + keyword.Length;

            string[] chunks = data.ToArray();//


            if (keyfont != null)//当设置了关键词字体时  需要根据该字体测量 偏移量
            {
                float ddx = 0;
                float ddy = 0;
                ddx = this.Width;
                //遍历
                for (int i = 0; i < chunks.Length; i++)
                {
                    if (chunks[i].Equals(keyword))//关键词
                    {
                        ddx -= g.MeasureString(keyword, keyfont).Width;

                    }
                    else
                    {
                        ddx -= g.MeasureString(chunks[i], font).Width;
                    }

                }

                ddx = ddx / 2.0f;

                if (ddx < 0)
                    ddx = 0;

                //遍历着色 
                for (int i = 0; i < chunks.Length; i++)
                {
                    if (chunks[i].Equals(keyword))//关键词
                    {
                        ddy = (this.Height - (g.MeasureString(keyword, keyfont)).Height) / 2.0f;
                        g.DrawString(chunks[i], keyfont, mKeyColor, ddx, ddy);
                        //水平偏移自增
                        ddx += (g.MeasureString(chunks[i], keyfont)).Width;

                    }
                    else
                    {
                        ddy = (this.Height - (g.MeasureString(chunks[i], font)).Height) / 2.0f;
                        g.DrawString(chunks[i], font, mNormalColor, ddx, ddy);
                        //水平偏移自增
                        ddx += (g.MeasureString(chunks[i], font)).Width;

                    }
                }

            }
            else
            {
                //遍历着色 
                for (int i = 0; i < chunks.Length; i++)
                {
                    if (chunks[i].Equals(keyword))//关键词
                    {

                        g.DrawString(chunks[i], font, mKeyColor, dx, dy);

                    }
                    else
                    {
                        g.DrawString(chunks[i], font, mNormalColor, dx, dy);

                    }

                    //水平偏移自增
                    dx += (g.MeasureString(chunks[i], font)).Width;

                }
            }


        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277

这是自定义控件,若有需要的朋友,需要将编译后的dll文件添加到你的解决方案里,不会的朋友请百度“C#使用自定义控件”。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/729035
推荐阅读
  

闽ICP备14008679号