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

查看:23
本文介绍了覆盖 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.问题是我找不到像 onClick 那样覆盖 onDraw 的方法.

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) ?

更清晰、更短.

编辑 3: 实际上对于在控件上绘制,推荐使用 TextRenderer.DrawText 而不是 graphics.DrawString..

Edit 3: Actually for drawing onto controls the use of TextRenderer.DrawText is recommended over graphics.DrawString..

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

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