C# 组合框在所选对象上绘制图像

C# 组合框在所选对象上绘制图像,c#,winforms,combobox,drawimage,C#,Winforms,Combobox,Drawimage,当项目被选中时,我尝试从组合框中的图像列表中绘制图像 我能够绘制图像,但是当onselctedIndex更改事件完成时,我丢失了图像 我的组合框已具有DrawMode.OwnerDrawFixed 我有一个名为ImageList的ListImage控件,其中包含10张图片 在我的简短示例中,我只需要在组合框中的ImageList位置1处绘制图像,这就是我得到这个.ImageList.draw(g,0,0,1)的原因 可能我没有参加正确的活动。有什么建议吗 请参见下图,在绘图后的IndexChan

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

我能够绘制图像,但是当
onselctedIndex更改
事件完成时,我丢失了图像

我的组合框已具有
DrawMode.OwnerDrawFixed

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

在我的简短示例中,我只需要在组合框中的ImageList位置1处绘制图像,这就是我得到这个.ImageList.draw(g,0,0,1)的原因

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

请参见下图,在绘图后的IndexChanged中有一个断点。这是工作,但事后我失去了形象


将您的
组合框更改为
OwnerDrawVariable

使用事件从ComboBox项边界内的源(本例中为ImageList)绘制图像

如果组合框设置为
DropDownList
,则图像将显示在选择框中;如果设置为
下拉列表
,则仅绘制文本

这里,焦点矩形仅在鼠标点悬停在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)));
}
//这些属性可用于自定义组合框外观
颜色CBR=颜色。黑色;
Color cboBackColor=Color.WhiteSmoke;
私有void comboBox1\u DrawItem(对象发送方,DrawItemEventArgs e)
{
如果(e.指数<0)返回;
彩色前向色=e.前向色;
e、 Graphics.SmoothingMode=SmoothingMode.AntiAlias;
e、 Graphics.TextRenderingHint=TextRenderingHint.ClearTypeGridFit;
if(e.State.HasFlag(DrawItemState.Focus)和&!e.State.HasFlag(DrawItemState.ComboBoxEdit)){
e、 牵引杆接地();
e、 DrawFocusRectangle();
}
否则{
使用(笔刷backgbrush=新的SolidBrush(cboBackColor)){
e、 图形。填充矩形(backgbrush,e.Bounds);
前景色=前景色;
}
}
使用(笔刷文本笔刷=新的SolidBrush(前景色)){
e、 Graphics.DrawString(comboBox1.Items[e.Index].ToString(),
e、 字体,文本笔刷,e.Bounds.Height+10,e.Bounds.Y,
StringFormat.GenericTypographic);
}
e、 Graphics.DrawImage(this.imageList1.Images[e.Index],
新矩形(如边界、位置、,
新尺寸(e.Bounds.Height-2,e.Bounds.Height-2);
}
这里的幻数(
10,-2
)只是偏移量:
e.Bounds.Height+10=>
e.Bounds.Height-2=>
2像素小于
item.Bounds.Height


将您的
组合框更改为
OwnerDrawVariable

使用事件从ComboBox项边界内的源(本例中为ImageList)绘制图像

如果组合框设置为
DropDownList
,则图像将显示在选择框中;如果设置为
下拉列表
,则仅绘制文本

这里,焦点矩形仅在鼠标点悬停在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)));
}
//这些属性可用于自定义组合框外观
颜色CBR=颜色。黑色;
Color cboBackColor=Color.WhiteSmoke;
私有void comboBox1\u DrawItem(对象发送方,DrawItemEventArgs e)
{
如果(e.指数<0)返回;
彩色前向色=e.前向色;
e、 Graphics.SmoothingMode=SmoothingMode.AntiAlias;
e、 Graphics.TextRenderingHint=TextRenderingHint.ClearTypeGridFit;
if(e.State.HasFlag(DrawItemState.Focus)和&!e.State.HasFlag(DrawItemState.ComboBoxEdit)){
e、 牵引杆接地();
e、 DrawFocusRectangle();
}
否则{
使用(笔刷backgbrush=新的SolidBrush(cboBackColor)){
e、 图形。填充矩形(backgbrush,e.Bounds);
前景色=前景色;
}
}
使用(笔刷文本笔刷=新的SolidBrush(前景色)){
e、 Graphics.DrawString(comboBox1.Items[e.Index].ToString(),
e、 字体,文本笔刷,e.Bounds.Height+10,e.Bounds.Y,
StringFormat.GenericTypographic);
}
e、 Graphics.DrawImage(this.imageList1.Images[e.Index],
新矩形(如边界、位置、,
新尺寸(e.Bounds.Height-2,e.Bounds.Height-2);
}
这里的幻数(
10,-2
)只是偏移量:
e.Bounds.Height+10=>
e.Bounds.Height-2=>
2像素小于
item.Bounds.Height


我真的很感激人们在没有解释的情况下给出负面分数!!!如果要绘制东西,通常需要OwnerDraw模式控件,这意味着您使用DrawItem方法,
CreateGraphics
,但这几乎从来都不是正确的方法,而且可能是图像“丢失”的原因……除此之外,这有点模糊。我的DrawMode已设置为OwnerDrawFixed
var g=this.CreateGraphics()切勿使用
CreateGraphics
!使用
DrawXX
PaintXX
事件参数的图形对象!您可能希望通过在SelChanged事件中调用控件来触发
Paint/DrawItem
事件。因此,将代码移到那里并调用
this.Invalidate()请注意,它通常还有有用的信息来帮助绘制。。如果
ImageList.Draw
是您的函数,您可以