Combobox draw image on selected

ⅰ亾dé卋堺 提交于 2019-11-27 08:42:53

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/50102456/combobox-draw-image-on-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!