覆盖ComboBox的DrawItem [英] Override DrawItem of ComboBox

查看:243
本文介绍了覆盖ComboBox的DrawItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更改了各种控件的高亮颜色,我打算进行更多更改。所以我最好创建自己的控件并重用它们,而不是为每个控件进行更改。

I changed the highlight color of various of the controls, and I am planning to make more changes. So I though is better to create my own controls and reuse them instead of making the changed for each and every one of them.

我创建了一个新的用户控件,并继承了来自 System.Windows.Forms.ComboBox
问题是我找不到覆盖 onDraw 的方法,就像我想要 onClick

I created a new user control, and inherited from System.Windows.Forms.ComboBox. The problem is I cannot find a way to override onDraw like I would for onClick.

那我该如何去覆盖呢?以下是我用于每个控件的代码 onDraw 事件

So how I would go and override it? Here is the code I used for each control onDraw event

public void comboMasterUsers_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground();

        Graphics g = e.Graphics;
        Brush brush = ((e.State & DrawItemState.Selected) == DrawItemState.Selected) ?
                      Brushes.LightSeaGreen : new SolidBrush(e.BackColor);

        g.FillRectangle(brush, e.Bounds);
        e.Graphics.DrawString(comboMasterUsers.Items[e.Index].ToString(), e.Font,
                 new SolidBrush(e.ForeColor), e.Bounds, StringFormat.GenericDefault);

        e.DrawFocusRectangle();
    }

谢谢!

推荐答案

在这里:

public class myCombo : ComboBox
{
    // expose properties as needed
    public Color SelectedBackColor{ get; set; }

    // constructor
    public myCombo()
    {
        DrawItem += new DrawItemEventHandler(DrawCustomMenuItem);
        DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
        SelectedBackColor= Color.LightSeaGreen;
    }

    protected  void DrawCustomMenuItem(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground();
         // a dropdownlist may initially have no item selected, so skip the highlighting:
        if (e.Index >= 0) 
        {  
          Graphics g = e.Graphics;
          Brush brush = ((e.State & DrawItemState.Selected) == DrawItemState.Selected) ?
                         new SolidBrush(SelectedBackColor) : new SolidBrush(e.BackColor);
          Brush tBrush = new SolidBrush(e.ForeColor);

          g.FillRectangle(brush, e.Bounds);
          e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font,
                     tBrush, e.Bounds, StringFormat.GenericDefault);
          brush.Dispose();
          tBrush.Dispose();
        }
        e.DrawFocusRectangle();
    }
}

您可以考虑在扩展自定义时公开更多属性,所以你可以在你想要的时候为每个实例更改它们。

You may consider exposing more Properties as you expand your customziation, so you can change them for each instance when you want to..

另外不要忘记处理你创建的GDI对象,比如画笔和笔!

Also don't forget to dispose GDI objects you create, like brushes and pens!

编辑:注意 BackColor 会隐藏原始属性。将其更改为 SelectedBackColor ,实际上说它是什么!

Just noticed that BackColor would hide the original property. Changed it to SelectedBackColor, which actually says what it is!

编辑2:正如Simon在评论中指出的那样,有一个 HasFlag 方法,因此.Net 4.0也可以写:

Edit 2: As Simon noted in the comments, there is a HasFlag method and so as of .Net 4.0 one can also write:

      Brush brush = ((e.State.HasFlag(DrawItemState.Selected) ?

更清晰,更短。

这篇关于覆盖ComboBox的DrawItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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