c# listbox 添加图标和文字

给listbox 添加 DrawItem 事件

        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            int index = e.Index;//获取当前要进行绘制的行的序号,从0开始。
            Graphics g = e.Graphics;//获取Graphics对象。
            Rectangle bound = e.Bounds;//获取当前要绘制的行的一个矩形范围。
            string text = listBox1.Items[index].ToString();//获取当前要绘制的行的显示文本。
            g.FillRectangle(new SolidBrush(Color.FromArgb(255, 255, 255)), e.Bounds);//绘制背景色。

            Font fn = new Font("宋体", 14);

            Image image = Properties.Resources.pengyou;  // 从资源管理器中读取图片

            int erea = 8;
            Rectangle imgRec = new Rectangle(bound.X + erea*2, bound.Y + erea, bound.Height - erea * 2, bound.Height - erea * 2);   // 图片区域
            Rectangle textRec = new Rectangle(imgRec.Right + 10, bound.Y, bound.Width - imgRec.Right,bound.Height);                 // 文字区域   

            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)  //如果当前行为选中行。
            {
                //绘制选中时要显示的蓝色边框。
                g.DrawRectangle(Pens.DodgerBlue, bound.Left, bound.Top, bound.Width - 1, bound.Height - 1);
                Rectangle rect = new Rectangle(bound.Left + 2, bound.Top + 2, bound.Width - 4, bound.Height - 4);

                //绘制选中时要显示的蓝色背景。
                g.FillRectangle(Brushes.DodgerBlue, rect);

                if (image != null)
                {
                    e.Graphics.DrawImage(image, imgRec, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
                }

                //绘制显示文本。
                TextRenderer.DrawText(g, text, fn, textRec, Color.White, TextFormatFlags.VerticalCenter | TextFormatFlags.Left);
            }
            else
            {
                g.DrawRectangle(new Pen(Color.FromArgb(190, 190, 190)), bound.Left - 1, bound.Top, bound.Width + 1, bound.Height);
                
                if (image != null)
                {
                    e.Graphics.DrawImage(image, imgRec, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
                }

                TextRenderer.DrawText(g, text, fn, textRec, Color.Black, TextFormatFlags.VerticalCenter | TextFormatFlags.Left);
            }
        }

运行效果

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2023-12-28 14:48:05       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-28 14:48:05       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-28 14:48:05       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-28 14:48:05       18 阅读

热门阅读

  1. ansible 加密

    2023-12-28 14:48:05       37 阅读
  2. CentOS 8 安装指定版本ansible

    2023-12-28 14:48:05       36 阅读
  3. 使用aspose.Words更新表格列宽

    2023-12-28 14:48:05       30 阅读
  4. C++八股文 003:左值,右值

    2023-12-28 14:48:05       39 阅读
  5. 斯坦福大学CS224W图机器学习笔记

    2023-12-28 14:48:05       46 阅读
  6. Python:PyTorch

    2023-12-28 14:48:05       31 阅读
  7. 第9章-用户分群方法-聚类评估指标

    2023-12-28 14:48:05       30 阅读
  8. Docker jenkins 镜像制作

    2023-12-28 14:48:05       43 阅读
  9. FreeBSD下安装Jenkins(软件测试集成工具)记录

    2023-12-28 14:48:05       39 阅读
  10. Windows 源码编译 MariaDB

    2023-12-28 14:48:05       34 阅读
  11. adb和logcat常用命令

    2023-12-28 14:48:05       29 阅读
  12. Jenkins打包问题

    2023-12-28 14:48:05       42 阅读
  13. flutter flutter pub cache clean和flutter clean区别

    2023-12-28 14:48:05       35 阅读
  14. python3 编译指定openssl

    2023-12-28 14:48:05       41 阅读
  15. C++高级-STL库概述

    2023-12-28 14:48:05       28 阅读