如果你覺得項目中的ComboBox、ListBox或其它的Winforms控件不能滿足你的顯示要求,包括窗體在內很多控件都支持重繪修改顯示樣式。下面的示例完成對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;
namespace SimpleDemo
{
public partial class frmDrawItem : Form
{
public frmDrawItem()
{
InitializeComponent();
// 指定繪制模式,這項必須指定為,OwnerDrawFixed,OwnerDrawVariable
// Normal 由操作系統繪制,並且元素大小都相等。
// OwnerDrawFixed 手動繪制的,並且元素大小都相等。
// OwnerDrawVariable 手動繪制,元素大小可能不相等。
comboBox2.DrawMode = DrawMode.OwnerDrawFixed;
}
// 重繪項事件
private void comboBox2_DrawItem( object sender, DrawItemEventArgs e)
{
// 獲取要在其上繪制項的圖形表面
Graphics g = e.Graphics;
// 獲取表示所繪制項的邊界的矩形
System.Drawing.Rectangle rect = e.Bounds;
// 定義要繪制到控件中的圖標圖像
Image ico = Image.FromFile( " head.png " );
// 定義字體對象
System.Drawing.Font font = new System.Drawing.Font( new FontFamily( " 宋體 " ), 12 );
if (e.Index >= 0 )
{
// 獲得當前Item的文本
string tempString = comboBox2.Items[e.Index].ToString();
// 如果當前項是沒有狀態的普通項
if (e.State == DrawItemState.None)
{
// 在當前項圖形表面上划一個矩形
g.FillRectangle( new SolidBrush(Color.FromArgb( 200 , 230 , 255 )), rect);
// 在當前項圖形表面上划上圖標
g.DrawImage(ico, new Point(rect.Left, rect.Top));
// 在當前項圖形表面上划上當前Item的文本
g.DrawString(tempString, font, new SolidBrush(Color.Black), rect.Left + ico.Size.Width, rect.Top);
// 將繪制聚焦框
e.DrawFocusRectangle();
}
else
{
e.Graphics.FillRectangle( new SolidBrush(Color.LightBlue), rect);
g.DrawImage(ico, new Point(rect.Left, rect.Top));
g.DrawString(tempString, font, new SolidBrush(Color.Black), rect.Left + ico.Size.Width, rect.Top);
e.DrawFocusRectangle();
}
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SimpleDemo
{
public partial class frmDrawItem : Form
{
public frmDrawItem()
{
InitializeComponent();
// 指定繪制模式,這項必須指定為,OwnerDrawFixed,OwnerDrawVariable
// Normal 由操作系統繪制,並且元素大小都相等。
// OwnerDrawFixed 手動繪制的,並且元素大小都相等。
// OwnerDrawVariable 手動繪制,元素大小可能不相等。
comboBox2.DrawMode = DrawMode.OwnerDrawFixed;
}
// 重繪項事件
private void comboBox2_DrawItem( object sender, DrawItemEventArgs e)
{
// 獲取要在其上繪制項的圖形表面
Graphics g = e.Graphics;
// 獲取表示所繪制項的邊界的矩形
System.Drawing.Rectangle rect = e.Bounds;
// 定義要繪制到控件中的圖標圖像
Image ico = Image.FromFile( " head.png " );
// 定義字體對象
System.Drawing.Font font = new System.Drawing.Font( new FontFamily( " 宋體 " ), 12 );
if (e.Index >= 0 )
{
// 獲得當前Item的文本
string tempString = comboBox2.Items[e.Index].ToString();
// 如果當前項是沒有狀態的普通項
if (e.State == DrawItemState.None)
{
// 在當前項圖形表面上划一個矩形
g.FillRectangle( new SolidBrush(Color.FromArgb( 200 , 230 , 255 )), rect);
// 在當前項圖形表面上划上圖標
g.DrawImage(ico, new Point(rect.Left, rect.Top));
// 在當前項圖形表面上划上當前Item的文本
g.DrawString(tempString, font, new SolidBrush(Color.Black), rect.Left + ico.Size.Width, rect.Top);
// 將繪制聚焦框
e.DrawFocusRectangle();
}
else
{
e.Graphics.FillRectangle( new SolidBrush(Color.LightBlue), rect);
g.DrawImage(ico, new Point(rect.Left, rect.Top));
g.DrawString(tempString, font, new SolidBrush(Color.Black), rect.Left + ico.Size.Width, rect.Top);
e.DrawFocusRectangle();
}
}
}
}
}
總結:如果僅僅是美化可以選擇使用皮膚控件,但使用重繪更加靈活,可以滿足一些功能上的需求。另外不同的控件還有一些不同重繪事件,可以參照MSDN。