111,125
社区成员
发帖
与我相关
我的任务
分享
string AppPath;
public Form1()
{
InitializeComponent();
AppPath = Application.StartupPath + "\\";
for (int i = 0; i < 10; i++)
{
comboBox2.Items.Add(Image.FromFile(AppPath + "Images\\" + i.ToString() + ".png"));
}
this.comboBox2.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
this.comboBox2.DropDownHeight = 400;
this.comboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBox2.FormattingEnabled = true;
this.comboBox2.IntegralHeight = false;
this.comboBox2.ItemHeight = 40;
this.comboBox2.Location = new System.Drawing.Point(228, 12);
this.comboBox2.Name = "comboBox2";
this.comboBox2.Size = new System.Drawing.Size(247, 46);
this.comboBox2.TabIndex = 0;
this.comboBox2.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox2_DrawItem);
}
private void comboBox2_DrawItem(object sender, DrawItemEventArgs e)
{
int i = e.Index;
e.DrawBackground();
Graphics g = e.Graphics;
RectangleF RectF;
Font font = new Font(this.Font.Name, 30, FontStyle.Bold);
Brush brush = new SolidBrush(this.ForeColor);
Font fontChar = new Font(this.Font.Name, 14, FontStyle.Bold);
int ImageSize = comboBox2.ItemHeight - 2;
if(i > - 1)
{
RectF = new RectangleF(8, e.Bounds.Top + 2, ImageSize, ImageSize);
g.DrawImage((Image)comboBox2.Items[i], RectF, new RectangleF(0, 0, ((Image)comboBox2.Items[i]).Width,
((Image)comboBox2.Items[i]).Height), GraphicsUnit.Pixel);
g.DrawString("这是第【" + i.ToString() + "】张图", fontChar, brush, RectF.Right + 4,
RectF.Top + (RectF.Height - g.MeasureString("图", fontChar).Height) / 2);
}
e.DrawFocusRectangle();
}