ComboBox DropDownList以及图片和文本中的项目 [英] ComboBox DropDownList and items from picture and text
问题描述
我在WindowsForms中有一个ComboBox,并且我手动绘制项目。每个项目均由图片和文字组成,因此项目的高度为34像素。
I have a ComboBox in WindowsForms and I draw items manually. Each item is composed from picture and text, so item is 34 px height.
我想将ComboBox的DropDownStyle设置为DropDownList以启用用户输入。但是,当我选择某些项目时,它会变形,因为图片和文本是可见的。而且我只想在用户选择某些项目时显示文本。
I want to set DropDownStyle of ComboBox to DropDownList to enable user input. But when I select some of the item, it is deformed, because picture and text is visible. And I want to display only text if user select some item.
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
if (e.Index > -1)
{
Piece item = this.Items[e.Index] as Piece;
e.Graphics.FillRectangle(Brushes.Gray, new Rectangle(e.Bounds.Left + 6, e.Bounds.Top + 6, 22, 22));
e.Graphics.DrawImage(item.Image, new Rectangle(e.Bounds.Left + 7, e.Bounds.Top + 7, 20, 20));
e.Graphics.DrawString(item.Title, e.Font,
new SolidBrush(e.ForeColor), e.Bounds.Left + 34, e.Bounds.Top + 10);
}
e.DrawFocusRectangle();
}
谢谢
推荐答案
1)您是说DropDown的DropDownStyle吗?
1) Do you mean DropDownStyle of DropDown? This is the setting which enables user input.
2)变形是什么意思-您在哪里看到它?
2) What do you mean by 'deformed' - and where are you seeing this?
编辑:如果此OnDrawItem调用是为了渲染顶部框-e.State设置了ComboBoxEdit位标志。
If this OnDrawItem call is to render the top box - e.State has the ComboBoxEdit bit flag set. Check for this to render differently.
if( (e.State & DrawItemState.ComboBoxEdit) != DrawItemState.ComboBoxEdit )
{
// Do drawing logic just for the top edit part
}
else
{
// Draw logic here for rendering in the drop-down
}
这篇关于ComboBox DropDownList以及图片和文本中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!