美化combox 用法(二) 为其添加图片,改变显示方式
2011-03-21 14:01
411 查看
对C# 中Combox控件的一些简单的操作,改变其显示特性,主要是改变其中的项的属性
我们需要注意的是:每次为Combox添加项的时候,就会触发DrawItem事件,而我们正是通过DrawItem来改变Combox的显示特性的
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace Combox_Image
{
public partial class Form1 : Form
{
ArrayList brushArray = new ArrayList() ;
ArrayList fontArray = new ArrayList() ;
public Form1()
{
InitializeComponent();
}
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
//确定画布
Graphics g = e.Graphics;
//绘制区域
Rectangle r = e.Bounds;
Font fn = null;
if (e.Index >= 0)
{
//设置字体、字符串格式、对齐方式
fn = (Font)fontArray[e.Index];
string s = (string)comboBox1.Items[e.Index];
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
//根据不同的状态用不同的颜色表示
if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
{
e.Graphics.FillRectangle(new SolidBrush(Color.Red), r);
e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r, sf);
e.DrawFocusRectangle();
}
else
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), r);
e.Graphics.DrawString(s, fn, new SolidBrush(Color.Red), r, sf);
e.DrawFocusRectangle();
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
fontArray.Add(new Font("Ariel", 8, FontStyle.Bold));
fontArray.Add(new Font("Courier", 8, FontStyle.Italic));
fontArray.Add(new Font("Veranda", 8, FontStyle.Bold));
fontArray.Add(new Font("System", 8, FontStyle.Strikeout));
fontArray.Add(new Font("Century SchoolBook", 8, FontStyle.Underline));
fontArray.Add(new Font("Helevctia", 8, FontStyle.Italic));
//创建画刷
brushArray.Add(new SolidBrush(Color.Red));
brushArray.Add(new SolidBrush(Color.Blue));
brushArray.Add(new SolidBrush(Color.Green));
brushArray.Add(new SolidBrush(Color.Yellow));
brushArray.Add(new SolidBrush(Color.Black));
brushArray.Add(new SolidBrush(Color.Azure));
brushArray.Add(new SolidBrush(Color.Firebrick));
brushArray.Add(new SolidBrush(Color.DarkMagenta));
brushArray.Add(new SolidBrush(Color.DarkTurquoise));
brushArray.Add(new SolidBrush(Color.Khaki));
//画comboBox1,注意它要调用comboBox1_DrawItem来画
comboBox1.Items.Add("中国");
comboBox1.Items.Add("巴西");
comboBox1.Items.Add("哥斯达黎加");
comboBox1.Items.Add("土耳其");
comboBox1.Items.Add("韩国");
comboBox1.Items.Add("日本");
//画comboBox2,注意它要调用comboBox2_DrawItem来画
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
//画comboBox3,注意它要调用comboBox3_DrawItem来画
comboBox3.Items.Add("赵微");
comboBox3.Items.Add("舒淇");
comboBox3.Items.Add("谌豹");
comboBox3.Items.Add("郑巧玲");
}
private void comboBox2_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
Rectangle r = e.Bounds;
if (e.Index >= 0)
{
//设置字符串前矩形块rd的大小
Rectangle rd = r;
rd.Width = rd.Left + 20;
Rectangle rt = r;
r.X = rd.Right;
//用不同的颜色画矩形块
SolidBrush b = (SolidBrush)brushArray[e.Index];
g.FillRectangle(b, rd);
//设置字符串的格式
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
{
//字符串背景
e.Graphics.FillRectangle(new SolidBrush(Color.White), r);
//显示字符串
e.Graphics.DrawString(b.Color.Name, new Font("Ariel", 8, FontStyle.Bold), new SolidBrush(Color.Black), r, sf);
//绘制取得焦点时的虚线框
e.DrawFocusRectangle();
}
else
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), r);
e.Graphics.DrawString(b.Color.Name, new Font("Veranda", 8, FontStyle.Bold), new SolidBrush(Color.Red), r, sf);
e.DrawFocusRectangle();
}
}
}
private void comboBox3_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
Rectangle r = e.Bounds;
Size imageSize = imageList1.ImageSize;
Font fn = null;
if (e.Index >= 0)
{
fn = (Font)fontArray[0];
string s = (string)comboBox3.Items[e.Index];
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
{
//画条目背景
e.Graphics.FillRectangle(new SolidBrush(Color.Red), r);
//绘制图像
imageList1.Draw(e.Graphics, r.Left, r.Top, e.Index);
//显示字符串
e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r.Left + imageSize.Width, r.Top);
//显示取得焦点时的虚线框
e.DrawFocusRectangle();
}
else
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), r);
imageList1.Draw(e.Graphics, r.Left, r.Top, e.Index);
e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r.Left + imageSize.Width, r.Top);
e.DrawFocusRectangle();
}
}
}
}
}
三个Combox的效果如下图所示:
我们需要注意的是:每次为Combox添加项的时候,就会触发DrawItem事件,而我们正是通过DrawItem来改变Combox的显示特性的
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace Combox_Image
{
public partial class Form1 : Form
{
ArrayList brushArray = new ArrayList() ;
ArrayList fontArray = new ArrayList() ;
public Form1()
{
InitializeComponent();
}
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
//确定画布
Graphics g = e.Graphics;
//绘制区域
Rectangle r = e.Bounds;
Font fn = null;
if (e.Index >= 0)
{
//设置字体、字符串格式、对齐方式
fn = (Font)fontArray[e.Index];
string s = (string)comboBox1.Items[e.Index];
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
//根据不同的状态用不同的颜色表示
if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
{
e.Graphics.FillRectangle(new SolidBrush(Color.Red), r);
e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r, sf);
e.DrawFocusRectangle();
}
else
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), r);
e.Graphics.DrawString(s, fn, new SolidBrush(Color.Red), r, sf);
e.DrawFocusRectangle();
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
fontArray.Add(new Font("Ariel", 8, FontStyle.Bold));
fontArray.Add(new Font("Courier", 8, FontStyle.Italic));
fontArray.Add(new Font("Veranda", 8, FontStyle.Bold));
fontArray.Add(new Font("System", 8, FontStyle.Strikeout));
fontArray.Add(new Font("Century SchoolBook", 8, FontStyle.Underline));
fontArray.Add(new Font("Helevctia", 8, FontStyle.Italic));
//创建画刷
brushArray.Add(new SolidBrush(Color.Red));
brushArray.Add(new SolidBrush(Color.Blue));
brushArray.Add(new SolidBrush(Color.Green));
brushArray.Add(new SolidBrush(Color.Yellow));
brushArray.Add(new SolidBrush(Color.Black));
brushArray.Add(new SolidBrush(Color.Azure));
brushArray.Add(new SolidBrush(Color.Firebrick));
brushArray.Add(new SolidBrush(Color.DarkMagenta));
brushArray.Add(new SolidBrush(Color.DarkTurquoise));
brushArray.Add(new SolidBrush(Color.Khaki));
//画comboBox1,注意它要调用comboBox1_DrawItem来画
comboBox1.Items.Add("中国");
comboBox1.Items.Add("巴西");
comboBox1.Items.Add("哥斯达黎加");
comboBox1.Items.Add("土耳其");
comboBox1.Items.Add("韩国");
comboBox1.Items.Add("日本");
//画comboBox2,注意它要调用comboBox2_DrawItem来画
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
//画comboBox3,注意它要调用comboBox3_DrawItem来画
comboBox3.Items.Add("赵微");
comboBox3.Items.Add("舒淇");
comboBox3.Items.Add("谌豹");
comboBox3.Items.Add("郑巧玲");
}
private void comboBox2_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
Rectangle r = e.Bounds;
if (e.Index >= 0)
{
//设置字符串前矩形块rd的大小
Rectangle rd = r;
rd.Width = rd.Left + 20;
Rectangle rt = r;
r.X = rd.Right;
//用不同的颜色画矩形块
SolidBrush b = (SolidBrush)brushArray[e.Index];
g.FillRectangle(b, rd);
//设置字符串的格式
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
{
//字符串背景
e.Graphics.FillRectangle(new SolidBrush(Color.White), r);
//显示字符串
e.Graphics.DrawString(b.Color.Name, new Font("Ariel", 8, FontStyle.Bold), new SolidBrush(Color.Black), r, sf);
//绘制取得焦点时的虚线框
e.DrawFocusRectangle();
}
else
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), r);
e.Graphics.DrawString(b.Color.Name, new Font("Veranda", 8, FontStyle.Bold), new SolidBrush(Color.Red), r, sf);
e.DrawFocusRectangle();
}
}
}
private void comboBox3_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
Rectangle r = e.Bounds;
Size imageSize = imageList1.ImageSize;
Font fn = null;
if (e.Index >= 0)
{
fn = (Font)fontArray[0];
string s = (string)comboBox3.Items[e.Index];
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
{
//画条目背景
e.Graphics.FillRectangle(new SolidBrush(Color.Red), r);
//绘制图像
imageList1.Draw(e.Graphics, r.Left, r.Top, e.Index);
//显示字符串
e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r.Left + imageSize.Width, r.Top);
//显示取得焦点时的虚线框
e.DrawFocusRectangle();
}
else
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), r);
imageList1.Draw(e.Graphics, r.Left, r.Top, e.Index);
e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r.Left + imageSize.Width, r.Top);
e.DrawFocusRectangle();
}
}
}
}
}
三个Combox的效果如下图所示:
相关文章推荐
- 美化combox 用法 —— 为其添加图片,改变显示方式
- 博客园 cnblogs博客 图片显示美化 使用CSS给图片添加圆角阴影
- 史上最全的CSS hack方式一览 jQuery 图片轮播的代码分离 JQuery中的动画 C#中Trim()、TrimStart()、TrimEnd()的用法 marquee 标签的使用详情 js鼠标事件 js添加遮罩层 页面上通过地址栏传值时出现乱码的两种解决方法 ref和out的区别在c#中 总结
- div中添加背景图片及居中显示
- ASP.NET 上传图片至数据库并读取图片显示--二进制流方式
- Jquery上传图片,xml方式显示图片【1】
- phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- Xcode9中,添加图片素材无法显示的问题
- 应用CImage类用文件和流的方式显示图片和放大缩小图片
- 【Visual Studio风格开发系列 - PropertyGrid控件】PropertyGrid控件中添加自定义显示方式
- 美化自己的对话框--MFC显示GIF动画图片
- opengl es 绘图图像 保存为图片。读取并显示图片,添加在layer上
- 可以托拽的图片显示控件,JViewport 用法演示
- [Android] 给图像添加相框、圆形圆角显示图片、图像合成知识
- 如何在启用SharePoint浏览器功能的InfoPath 表单中添加托管代码以动态地加载并显示图片
- YII中CDetailView下使用CHtml image()显示图片的正确用法
- 在不改变大小的情况下,如何以最省内存的方式读取本地资源的图片
- cocos2d-x 中添加显示文字的三种方式(CCLabelTTF 、CCLabelBMFont 和CCLabelAtlas)
- Qt添加窗口背景图片、Label图片显示、、Label文字显示
- dskinlite(uieasy mfc界面库)使用记录3:绘制动态元素(按钮控件通过隐藏方式修改图片显示)