【发布时间】:2010-10-21 08:02:50
【问题描述】:
我用 C# 编写的增强型 WinForms ComboBox 有一个奇怪的问题。该框显示带有颜色本身和颜色名称的颜色列表,并设置为 ComboBoxStyle.DropDownList、双缓冲和 DrawMode.OwnerDrawFixed。在引发 DrawItem 事件时绘制项目。
如果我打开下拉菜单并使用鼠标滚轮滚动,则行为和外观都很好。 如果我用鼠标左键使用滚动条拇指,面板中的文本就会变得很脏。只有当我再次将鼠标悬停在显示的项目上时,项目才会正确重绘。
非常感谢您的帮助!
迈克尔
编辑:绘制方法代码:
private void OnDrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index == -1)
{
return;
}
e.DrawBackground();
Graphics grfx = e.Graphics;
grfx.FillRectangle(mWhiteBrush, e.Bounds);
ColorInfo colorInfo = (ColorInfo)Items[e.Index];
Color brushColor = colorInfo.Color;
using (SolidBrush brush = new SolidBrush(brushColor))
{
Rectangle rectangleColor = e.Bounds;
Rectangle rectangleText = e.Bounds;
rectangleColor.Width = rectangleColor.Height;
rectangleText.X += rectangleColor.Width;
rectangleText.Width -= rectangleColor.Width;
grfx.FillRectangle(brush, rectangleColor);
grfx.DrawString(colorInfo.Name, e.Font, mBlackBrush, rectangleText);
}
}
【问题讨论】: