组合框在选定位置绘制图像 [英] Combobox draw image on selected
问题描述
选择项目后,我尝试从组合框中的图像列表中绘制图像。
我可以绘制图像,但是当 onSelctedIndexChanged
事件结束,我丢失了图像。
我的组合框已经具有 DrawMode.OwnerDrawFixed
我有一个名为ImageList的 ListImage
控件,带有10张图片。
对于我的简短示例,我只需要在组合框中绘制ImageList位置1处的图像,这就是为什么我得到this.ImageList.Draw(g,0,0, 1 );
受保护的覆盖无效OnSelectedIndexChanged(EventArgs e)
{
base.OnSelectedIndexChanged(e);
if(this.SelectedIndex> -1)
{
var g = this.CreateGraphics();
this.ImageList.Draw(g,0,0,1);
}
}
我可能是不重视正确的事件。有任何建议吗?
在抽奖后,在索引更改中看到带有断点的图片。可以,但是活动结束后我迷失了我的形象。
更改您的组合框
I try to draw an image from a image list in a combobox when the item is selected.
I am able to draw the image, but when the onSelctedIndexChanged
event finish, i lost my image.
My combobox already have the DrawMode.OwnerDrawFixed
I have a ListImage
control named ImageList with 10 pictures.
For my short example i just need to draw in my combobox the image at position 1 of my ImageList, it's the reason why i get this.ImageList.Draw(g, 0, 0, 1);
protected override void OnSelectedIndexChanged(EventArgs e)
{
base.OnSelectedIndexChanged(e);
if (this.SelectedIndex > -1)
{
var g = this.CreateGraphics();
this.ImageList.Draw(g, 0, 0, 1);
}
}
Probably i am not attach to the right event. Any suggestion?
See the picture bellow with a breakpoint in the IndexChanged after the Draw. It's work, but i lost my image after the event.
Change you ComboBox
DrawMode
to OwnerDrawVariable
.
Use the DrawItem
event to draw the images from your source (an ImageList, in this case) inside the ComboBox item Bounds.
If the ComboBox DropDownStyle
is set to DropDownList
, the image will be shown in the selection box; if it's set to DropDown
, only the text will be drawn.
Here, the Focus rectangle is only drawn when the mouse point hovers the ListControl's items, while it's not used when an item is selected, which is determined by:
(e.State.HasFlag(DrawItemState.Focus) && !e.State.HasFlag(DrawItemState.ComboBoxEdit))
.
// These could be properties used to customize the ComboBox appearance
Color cboForeColor = Color.Black;
Color cboBackColor = Color.WhiteSmoke;
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index < 0) return;
Color foreColor = e.ForeColor;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
if (e.State.HasFlag(DrawItemState.Focus) && !e.State.HasFlag(DrawItemState.ComboBoxEdit)) {
e.DrawBackground();
e.DrawFocusRectangle();
}
else {
using (Brush backgbrush = new SolidBrush(cboBackColor)) {
e.Graphics.FillRectangle(backgbrush, e.Bounds);
foreColor = cboForeColor;
}
}
using (Brush textbrush = new SolidBrush(foreColor)) {
e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(),
e.Font, textbrush, e.Bounds.Height + 10, e.Bounds.Y,
StringFormat.GenericTypographic);
}
e.Graphics.DrawImage(this.imageList1.Images[e.Index],
new Rectangle(e.Bounds.Location,
new Size(e.Bounds.Height - 2, e.Bounds.Height - 2)));
}
The Magic Numbers here (10, -2
) are just offsets:
e.Bounds.Height + 10 =>
10 pixels to the right of the image.
e.Bounds.Height -2 =>
2 pixels less than the item.Bounds.Height
.
这篇关于组合框在选定位置绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!