组合框在所选上绘制图像 [英] Combobox draw image on selected

查看:18
本文介绍了组合框在所选上绘制图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当项目被选中时,我尝试从组合框中的图像列表中绘制图像.

我能够绘制图像,但是当 onSelctedIndexChanged 事件完成时,我丢失了图像.

我的组合框已经有了 DrawMode.OwnerDrawFixed

我有一个名为 ImageList 的 ListImage 控件,其中包含 10 张图片.

对于我的简短示例,我只需要在我的组合框中绘制 ImageList 位置 1 处的图像,这就是我得到 this.ImageList.Draw(g, 0, 0, 1);

 protected override void OnSelectedIndexChanged(EventArgs e){base.OnSelectedIndexChanged(e);如果(this.SelectedIndex > -1){var g = this.CreateGraphics();this.ImageList.Draw(g, 0, 0, 1);}}

可能我没有参加正确的活动.有什么建议吗?

在 Draw 之后的 IndexChanged 中有一个断点,请参见下图.成功了,但活动结束后我失去了形象.

解决方案

更改您的 ComboBox

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

这篇关于组合框在所选上绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆