您的位置:首页 > 其它

美化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的效果如下图所示:





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐