【问题标题】:ComboBox draw image on selected item组合框在所选上绘制图像
【发布时间】:2018-04-30 14:20:35
【问题描述】:

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

我可以绘制图像,但是当onSelctedIndexChanged 事件结束时,我的图像丢失了。

我的组合框已经有DrawMode.OwnerDrawFixed

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

对于我的简短示例,我只需要在我的组合框中绘制 ImageList 位置 1 的图像,这就是我得到 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);   

      }

    }

可能我没有加入正确的事件。有什么建议吗?

参见下图,在 Draw 后 IndexChanged 中有一个断点。这是工作,但我在活动结束后失去了我的形象。

【问题讨论】:

  • 我真的很感激人们在没有解释的情况下给出一些负分!!!
  • 如果你要画东西,你通常需要一个 OwnerDraw 模式控件,这意味着你使用 DrawItem 方法,CreateGraphics 虽然几乎从来都不是正确的方法,而且很可能是图像出现的原因“迷路”……除此之外,这有点模糊
  • 我的 DrawMode 已经设置为 OwnerDrawFixed
  • var g = this.CreateGraphics(); 永远不要使用CreateGraphics!使用DrawXXPaintXX 事件参数的Graphics 对象!您可能希望通过Invalidate()'ing SelChanged 事件中的控件来触发Paint/DrawItem 事件。所以将代码移到那里并调用this.Invalidate(); 请注意,它通常也有帮助绘图的有用信息。如果ImageList.Draw 是您的函数,您可以传入 Graphics 对象,前提是 a) 是有效的 ( ie 来自 e.paramter) 和 b) 它没有被缓存。
  • 创建持久绘图的唯一方法是在正确的时间使用正确的 Graphics 对象。 Ownerdrawing 通常还涉及绘制背景。

标签: c# winforms graphics combobox drawimage


【解决方案1】:

将您的组合框 DrawMode 更改为 OwnerDrawVariable
使用DrawItem 事件在 ComboBox 项 Bounds 内从源(在本例中为 ImageList)绘制图像。

如果ComboBoxDropDownStyle设置为DropDownList,图片会显示在选择框中;如果设置为DropDown,则只绘制文本。

这里,焦点矩形仅在鼠标悬停在 ListControl 的项目上时绘制,而在选中项目时不使用,由以下因素决定:
(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)));
}

这里的幻数 (10, -2) 只是偏移量:
e.Bounds.Height + 10 =&gt; 图像右侧 10 像素。
e.Bounds.Height -2 =&gt; 比 @ 小 2 像素987654334@.

【讨论】:

  • 您正在创建需要处理的画笔。或者,您可以只使用内置画笔:Brushes.White、Brushes.Black 等。
  • @ LarsTech ...但是使用普通画笔会破坏拥有.ForeColor/BackColor 属性的目的,所以回到处理新的(ed)画笔。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多