comboBox with images

Myth丶恋晨 2022-08-26 05:58 267阅读 0赞

先看效果图:

Center

实现步骤如下:

添加MyItem类:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;//
  6. namespace rePaint
  7. {
  8. class MyItem
  9. {
  10. //项文本内容
  11. private String Text;
  12. //项图片
  13. public Image Img;
  14. //构造函数
  15. public MyItem(String text, Image img)
  16. {
  17. Text = text;
  18. Img = img;
  19. }
  20. //重写ToString函数,返回项文本
  21. public override string ToString()
  22. {
  23. return Text;
  24. }
  25. }
  26. }

在主程序中:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Drawing.Drawing2D;
  10. namespace rePaint
  11. {
  12. public partial class Form1 : Form
  13. {
  14. private int itemHeight=30;
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. private void Form1_Load(object sender, EventArgs e)
  20. {
  21. //添加项
  22. comboBox1.Items.Add(new MyItem("000000", Image.FromFile(Application.StartupPath + @"\0.gif")));
  23. comboBox1.Items.Add(new MyItem("111111", Image.FromFile(Application.StartupPath + @"\1.gif")));
  24. comboBox1.Items.Add(new MyItem("222222", Image.FromFile(Application.StartupPath + @"\2.gif")));
  25. comboBox1.Items.Add(new MyItem("333333", Image.FromFile(Application.StartupPath + @"\3.gif")));
  26. //默认选中项索引
  27. comboBox1.SelectedIndex = 0;//不能删除
  28. //自绘组合框需要设置的一些属性
  29. comboBox1.DrawMode = DrawMode.OwnerDrawFixed;//不能设置为:DrawMode.OwnerDrawVariable
  30. comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
  31. comboBox1.ItemHeight = itemHeight;
  32. //添加DrawItem事件处理函数
  33. comboBox1.DrawItem += ComboBox1_DrawItem;
  34. }
  35. private void ComboBox1_DrawItem(object sender, DrawItemEventArgs e)
  36. {
  37. //鼠标选中在这个项上
  38. if ((e.State & DrawItemState.Selected) != 0)
  39. {
  40. //渐变画刷
  41. LinearGradientBrush brush = new LinearGradientBrush(e.Bounds, Color.FromArgb(255, 251, 237),
  42. Color.FromArgb(255, 236, 181), LinearGradientMode.Vertical);
  43. //填充区域
  44. Rectangle borderRect = new Rectangle(3, e.Bounds.Y, e.Bounds.Width - 4, e.Bounds.Height - 2);
  45. e.Graphics.FillRectangle(brush, borderRect);
  46. //画边框
  47. Pen pen = new Pen(Color.FromArgb(229, 195, 101));
  48. e.Graphics.DrawRectangle(pen, borderRect);
  49. }
  50. else
  51. {
  52. SolidBrush brush = new SolidBrush(Color.FromArgb(255, 255, 255));
  53. e.Graphics.FillRectangle(brush, e.Bounds);
  54. }
  55. //获得项图片,绘制图片
  56. MyItem item = (MyItem)comboBox1.Items[e.Index];
  57. Image img = item.Img;
  58. //图片绘制的区域
  59. Rectangle imgRect = new Rectangle(2, e.Bounds.Y + 1, itemHeight - 2, itemHeight - 2);//(x,y,width,height)
  60. e.Graphics.DrawImage(img, imgRect);
  61. //文本内容显示区域
  62. Rectangle textRect =
  63. new Rectangle(imgRect.Right + 2, imgRect.Y, e.Bounds.Width - imgRect.Width, e.Bounds.Height - 2);
  64. //获得项文本内容,绘制文本
  65. String itemText = comboBox1.Items[e.Index].ToString();
  66. //文本格式垂直居中
  67. StringFormat strFormat = new StringFormat();
  68. strFormat.LineAlignment = StringAlignment.Center;
  69. e.Graphics.DrawString(itemText, new Font("宋体", 12), Brushes.Black, textRect, strFormat);
  70. }
  71. }
  72. }

发表评论

表情:
评论列表 (有 0 条评论,267人围观)

还没有评论,来说两句吧...

相关阅读

    相关 easyui-combobox

        在做功能的时候用到easyui的combobox,下拉框经常使用,因此把它的使用方法简单总结如下:     html代码部分: <pre name="cod