关于在winform中listbox里面显示图片的问题.
2008-05-24 13:53
363 查看
在listbox中显示控件,并且是根据图像的大小来定义item的高度.在实际的开发中发现item的itemheight是受限制的.解决的办法就只有重绘.
主要的是两个事件MeasureItem,DrawItem.以及listbox 的属性DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable.
private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
//重新设定item的高度的关键代码.
if (e.Index >= 0 && e.Index < this.listBox1.Items.Count)
{
ImageHelp ih = (ImageHelp)this.listBox1.Items[e.Index];
e.ItemHeight= ih.img.Height;
}
}
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
//绘制图片到item中.
Graphics g = e.Graphics;
// draw background, which indicates if object is selected
e.DrawBackground();
if (e.Index >= 0 && e.Index < this.listBox1.Items.Count)
{
// each item in the list IPainter
IPainter painter = new ColorPainter(Color.White);
ImageHelp ih = (ImageHelp)this.listBox1.Items[e.Index];
// get brush from painter
Brush brush = painter.PaintBrush;
// fill the item with painters brush
g.FillRectangle(brush, e.Bounds);
g.DrawLine(Pens.Black, e.Bounds.X, e.Bounds.Bottom - 1, e.Bounds.Right, e.Bounds.Bottom - 1);
// draw box with painter name
string name = ih.name;
int width = (int)g.MeasureString(name, this.listBox1.Font).Width;
g.FillRectangle((e.State & DrawItemState.Selected) == DrawItemState.Selected ? Brushes.Yellow : Brushes.White, 3, e.Bounds.Top, width + 3, this.listBox1.Font.Height);
// g.DrawRectangle(Pens.Black, 3, e.Bounds.Top + 3, width + 3, this.listBox1.Font.Height );
g.DrawString(name, Font, Brushes.Black, 5, e.Bounds.Top);
Rectangle photoRct = new Rectangle(
e.Bounds.X+40,
e.Bounds.Y,
ih.img.Width,
ih.img.Height);
e.Graphics.DrawImage(ih.img, photoRct);
brush.Dispose();
}
}
程序运行图片.
文件下载http://files.cnblogs.com/cookieswolf/listboxEx.zip
主要的是两个事件MeasureItem,DrawItem.以及listbox 的属性DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable.
private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
//重新设定item的高度的关键代码.
if (e.Index >= 0 && e.Index < this.listBox1.Items.Count)
{
ImageHelp ih = (ImageHelp)this.listBox1.Items[e.Index];
e.ItemHeight= ih.img.Height;
}
}
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
//绘制图片到item中.
Graphics g = e.Graphics;
// draw background, which indicates if object is selected
e.DrawBackground();
if (e.Index >= 0 && e.Index < this.listBox1.Items.Count)
{
// each item in the list IPainter
IPainter painter = new ColorPainter(Color.White);
ImageHelp ih = (ImageHelp)this.listBox1.Items[e.Index];
// get brush from painter
Brush brush = painter.PaintBrush;
// fill the item with painters brush
g.FillRectangle(brush, e.Bounds);
g.DrawLine(Pens.Black, e.Bounds.X, e.Bounds.Bottom - 1, e.Bounds.Right, e.Bounds.Bottom - 1);
// draw box with painter name
string name = ih.name;
int width = (int)g.MeasureString(name, this.listBox1.Font).Width;
g.FillRectangle((e.State & DrawItemState.Selected) == DrawItemState.Selected ? Brushes.Yellow : Brushes.White, 3, e.Bounds.Top, width + 3, this.listBox1.Font.Height);
// g.DrawRectangle(Pens.Black, 3, e.Bounds.Top + 3, width + 3, this.listBox1.Font.Height );
g.DrawString(name, Font, Brushes.Black, 5, e.Bounds.Top);
Rectangle photoRct = new Rectangle(
e.Bounds.X+40,
e.Bounds.Y,
ih.img.Width,
ih.img.Height);
e.Graphics.DrawImage(ih.img, photoRct);
brush.Dispose();
}
}
程序运行图片.
文件下载http://files.cnblogs.com/cookieswolf/listboxEx.zip
相关文章推荐
- 关于mschart控件在mvc项目中的webform里面显示不出图片的问题
- 关于项目中的图片上传、并显示问题
- 关于phpcms上传图片成功,后台图片无法显示的问题
- 关于eclipse导出Java项目形成的jar包中没有图片以及图片显示问题
- 关于facebook分享不能显示图片链接问题
- 关于博客不能显示上传图片的问题
- 关于在IE-8下 button的背景图片不能正确显示的问题
- 关于C# WinForm中“显示桌面后”,点击任务栏右下角图标还原的问题 - [IT技术] 2011-
- 关于UIImageView的显示问题——居中显示或者截取图片的中间部分显示
- 关于图片放在服务器上 显示的问题
- GridView里面的图片不显示的问题
- 关于kindeditor编辑器批量上传图片不显示添加图片按钮的问题
- 关于web项目中的图片上传、并显示问题
- 关于百度world 编辑器改变上传图片的保存路径图片不显示的问题
- 关于Android加载HTML页面中的图片显示问题的解决办法
- 关于asp.net mvc中 weiui gallery中IOS 下不显示预览图片问题的解决方式
- 关于CSDN书写的博客内容中图片不显示的问题
- 关于winform下软件的全屏幕显示问题
- 关于SSH中tomcat下中文名称图片不显示的问题
- 关于《.net中多个图片并排循环排列显示的问题》[WEB学习示例]