【实例简介】ComboBox 显示图片

【实例截图】

【核心代码】

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.IO;

using System.Net;

using System.Text;

using System.Windows.Forms;

namespace ComboBox_Image

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

//添加项

comboBox1.Items.Add(new MyItem("1111111", this.GetImage("http://www.baidu.com/img/bdlogo.gif")));

comboBox1.Items.Add(new MyItem("2222222", this.GetImage("http://www.baidu.com/img/bdlogo.gif")));

comboBox1.Items.Add(new MyItem("3333333", this.GetImage("http://www.baidu.com/img/bdlogo.gif")));

comboBox1.Items.Add(new MyItem("4444444", this.GetImage("http://www.baidu.com/img/bdlogo.gif")));

comboBox1.Items.Add(new MyItem("5555555", this.GetImage("http://www.baidu.com/img/bdlogo.gif")));

//默认选中项索引

comboBox1.SelectedIndex = 0;

//自绘组合框需要设置的一些属性

comboBox1.DrawMode = DrawMode.OwnerDrawVariable;

comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

comboBox1.ItemHeight = 30;

comboBox1.Width = 200;

//添加DrawItem事件处理函数

comboBox1.DrawItem = comboBox1_DrawItem;

}

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)

{

if ((e.State & DrawItemState.Selected) != 0)//鼠标选中在这个项上

{

//渐变画刷

LinearGradientBrush brush = new LinearGradientBrush(e.Bounds, Color.FromArgb(255, 251, 237),

Color.FromArgb(255, 236, 181), LinearGradientMode.Vertical);

//填充区域

Rectangle borderRect = new Rectangle(3, e.Bounds.Y 1, e.Bounds.Width - 5, e.Bounds.Height - 2);

e.Graphics.FillRectangle(brush, borderRect);

//画边框

Pen pen = new Pen(Color.FromArgb(229, 195, 101));

e.Graphics.DrawRectangle(pen, borderRect);

}

else

{

SolidBrush brush = new SolidBrush(Color.FromArgb(217, 223, 230));

e.Graphics.FillRectangle(brush, e.Bounds);

}

//获得项图片,绘制图片

MyItem item = (MyItem)comboBox1.Items[e.Index];

Image img = item.Img;

//图片绘制的区域

Rectangle imgRect = new Rectangle(6, e.Bounds.Y 3, 25, 25);

e.Graphics.DrawImage(img, imgRect);

//文本内容显示区域

Rectangle textRect =

new Rectangle(imgRect.Right 2, imgRect.Y, e.Bounds.Width - imgRect.Width, e.Bounds.Height - 2);

//获得项文本内容,绘制文本

String itemText = comboBox1.Items[e.Index].ToString();

//文本格式垂直居中

StringFormat strFormat = new StringFormat();

strFormat.LineAlignment = StringAlignment.Center;

e.Graphics.DrawString(itemText, new Font("宋体", 12), Brushes.Black, textRect, strFormat);

}

public class MyItem

{

//项文本内容

private String Text;

//项图片

public Image Img;

public MyItem(String text, Image img)

{

Text = text;

Img = img;

}

//重写ToString函数,返回项文本

public override string ToString()

{

return Text;

}

}

public System.Drawing.Image GetImage(string strUrl)

{

try

{

string url = @"http://hiphotos.baidu.com/huyangdiy/pic/item/fd1d340bfaec3e656059f3df.jpg";

url = strUrl;

WebRequest request = WebRequest.Create(url);

WebResponse response = request.GetResponse();

Stream stream = response.GetResponseStream();

Image image = Image.FromStream(stream);

stream.Close();

//g.DrawImage(image, 0, 0);

//g.Dispose();

return image;

}

catch { return null; }

}

}

}

html.tex 下拉框,winform ComboBox 下拉框 显示图片效果 附完整源码相关推荐

  1. c#winform如何获取服务器指定文件夹中所有的文件信息,winform 读取某个目录下的所有图片并显示到pictureBox 附完整源码...

    [实例简介]产品电子报价 功能实现 [实例截图] [核心代码] using System; using System.Collections.Generic; using System.Compone ...

  2. C++在不使用任何算术运算符的情况下将两个数字相加(附完整源码)

    C++在不使用任何算术运算符的情况下将两个数字相加 C++在不使用任何算术运算符的情况下将两个数字相加完整源码(定义,实现,main函数测试) C++在不使用任何算术运算符的情况下将两个数字相加完整源 ...

  3. C++newton raphson method牛顿拉夫森法的实现算法(附完整源码)

    C++newton raphson method牛顿拉夫森法的实现算法 C++newton raphson method牛顿拉夫森法的实现算法完整源码(定义,实现,main函数测试) C++newto ...

  4. C++实现欧拉的totient 函数(Euler’s totient function)(附完整源码)

    C++实现欧拉的totient 函数 C++Adaline实现欧拉的totient 函数算法完整源码(定义,实现,main函数测试) C++Adaline实现欧拉的totient 函数算法完整源码(定 ...

  5. C语言实现牛顿-拉夫逊newton raphson求近似根roots(附完整源码)

    实现牛顿-拉夫逊newton raphson求近似根 实现以下几个相关的接口 实现牛顿-拉夫逊newton raphson求近似根的完整源码(定义,实现,main函数测试) 实现以下几个相关的接口 d ...

  6. Python:实现floor向下取整算法(附完整源码)

    Python:实现floor向下取整算法 def floor(x) -> int:"""Return the floor of x as an Integral.: ...

  7. java 获取包下的所有类,附完整源码和测试代码

    java 获取包下的所有类,完整Java代码如下: package com.example.demo.util;import java.io.File; import java.io.FileFilt ...

  8. H5小游戏1—— 是男人就下一百层(附完整源码)

  9. Python:实现newton raphson牛顿-拉夫森算法(附完整源码)

    Python:实现newton raphson牛顿-拉夫森算法 import math as m def calc_derivative(f, a, h=0.001):return (f(a + h) ...

最新文章

  1. windows popen 获取不到输出_彻底明白os.system、os.popen、subprocess.popen的用法和区别...
  2. RTEMS移植USB无线网卡的设想
  3. 查找DetailsView1数据控件中的数据
  4. 计算机连接拒绝访问,Win10系统下Windows无法连接到打印机,拒绝访问的解决办法...
  5. java学习(144):file常用方法1
  6. HTML5须知的特征和技术
  7. lisp坐标一键生成_Grasshopper自动生成坡度标注
  8. Android 记忆卡片游戏 记忆力 Android游戏 Android记忆卡片游戏源代码
  9. 钉钉扫码登录第三方_e签宝联合钉钉升级产品功能,共建企业服务生态闭环
  10. 掌握用 STL 中的 SET 动态维护 “各类型凸壳” / “凸包”
  11. Java数字抽奖游戏核心代码及分析
  12. QTP11的下载地址和破解教程
  13. 按键精灵_汉字转拼音
  14. matlab 灰度转伪彩色,matlab将灰度图像伪彩色图像处理(自己编写的color function)...
  15. word绘制表格三斜线表头
  16. lasso回归python代码_LASSO回归代码实现 坚韧不拔|静水流深|读书|写作|博雅|数据分析|Python|商业|独立·独特·自立门户 kebook...
  17. mysql 100w 查询耗时4秒_MySql百万数据0秒筛选查询
  18. 【办公基本软件】万彩办公大师教程丨批量文件目录生成器
  19. vue项目中引入.xlsx文件
  20. 对东方财经个股资金流的爬取分析

热门文章

  1. 微积分提纲+公式整理(大一上)
  2. 游戏开发中如何设计一个撤销重做系统DoUnDo
  3. 名企到访|南京汇智动力共建教学资源,推进校企项目IT人才实训!
  4. Echarts3 关系图-力导向布局图
  5. curses函数说明
  6. 手机组态软件利用4G网络直接通信三菱E700变频器
  7. Matlab基础入门,一篇就够啦(所有源代码)
  8. 计算机操作题蝴蝶效应,办公自动化上机操作测试题
  9. EF Core的学习之路01
  10. VIVO Xplay_2.13.2 目前最新ViVo官方固件,完美root,降噪点,完美支持官方OTA升级,稳定,流畅,实用ROM