Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

c# - Combobox draw image on selected

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.

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change your 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.

ComboBox Ownerdraw with Images


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...