65.9K
CodeProject 正在变化。 阅读更多。
Home

实现 OwnerDrawn ComboBox

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.83/5 (21投票s)

2002年1月27日

1分钟阅读

viewsIcon

333269

downloadIcon

3596

本文展示了如何创建 OwnerDrawn 组合框

Sample Image - OwnerDrawnComboBox.gif

引言

大家好,我等了很久很久,希望有人能分享一个所有者绘制的 ComboBox 的代码,但一直没有找到。没错,我只是一个普通的、卑微的“吸血鬼”,长期以来一直在 CodeProject 上“偷”代码。好吧,看来这次我不得不自己写一些东西了。我想这可能对你们来说太简单了,不值得费心。但请继续撰写面向相对初学者的文章,因为我们大多数人必须从简单的东西开始。幸运的是,使用 .NET 框架编写代码变得轻而易举。

好的,让我们开始吧。首先,将一个 ComboBox 拖放到窗体上,并将它的 DrawMode 属性更改为 OwnerDrawFixed。这将意味着出现在 ComboBox 中的所有项目都将具有相同的尺寸。如果要在 ComboBox 中绘制大小可变的项,则将属性更改为 OwnerDrawVariable,并被迫处理 MeasureItem 事件。我不会在这里介绍这个。接下来,为 ComboBox 创建一个 DataSource,以及相应的图像集合。在我的例子中,我只是使用了两个 Array。以下代码应该很容易理解。

代码

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication7
{    
    public class Form1 : System.Windows.Forms.Form
    {        
        private System.ComponentModel.Container components = null;
        private String[] arr;
        private Image[] imageArr;
        private System.Windows.Forms.ComboBox comboBox1;        
        public Font myFont;        
        
        public Form1()
        {            
            InitializeComponent();
            myFont = new System.Drawing.Font("Comic Sans", 11);
            
            arr = new String[4];
            arr[0] = "Smiley Red Face";
            arr[1] = "Smiley Cry";
            arr[2] = "Smiley Big Grin";
            arr[3]  = "Smiley Suss";
            
            this.comboBox1.DataSource = arr; //set the combo's data source to out array

            imageArr = new Image[4];

            /* I stole these images off CP. I specifically chose these images because they 
            are all little 16x16 sized gifs. If you're going to use your own images make 
            sure they are all the same size, otherwise your code won't work as advertised. 
            In that case you will have to set the DrawMode property of your combobox to 
            OwnnerDrawVariable and catch the MeasureItem event*/

            imageArr[0] = new Bitmap("C:\\smiley_redface.gif");
            imageArr[1] = new Bitmap("C:\\smiley_cry.gif");
            imageArr[2] = new Bitmap("C:\\smiley_biggrin.gif");
            imageArr[3] = new Bitmap("C:\\smiley_suss.gif");
        }        
        
        //You have your windows forms designer generated code here

        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }

        private void comboBox1_DrawItem(object sender, 
                                        System.Windows.Forms.DrawItemEventArgs e)
        {    
            // Let's highlight the currently selected item like any well 
            // behaved combo box should
            e.Graphics.FillRectangle(Brushes.Bisque, e.Bounds);                
            e.Graphics.DrawString(arr[e.Index], myFont, Brushes.Blue, 
                                  new Point(imageArr[e.Index].Width*2,e.Bounds.Y));       
            e.Graphics.DrawImage(imageArr[e.Index], new Point(e.Bounds.X, e.Bounds.Y));   
            
            //is the mouse hovering over a combobox item??            
            if((e.State & DrawItemState.Focus)==0)
            {                                                    
                //this code keeps the last item drawn from having a Bisque background. 
                e.Graphics.FillRectangle(Brushes.White, e.Bounds);                    
                e.Graphics.DrawString(arr[e.Index], myFont, Brushes.Blue, 
                                      new Point(imageArr[e.Index].Width*2,e.Bounds.Y));
                e.Graphics.DrawImage(imageArr[e.Index], new Point(e.Bounds.X, e.Bounds.Y)); 
            }    
        }                                                                
    }
}

结论

好的,这是我在 CodeProject 上的第一篇文章,请多多包涵。代码一开始又长又繁琐,但随着我对 DrawItemEventArgs 的理解越来越清晰,我将其精简了。我不确定 ComboBox 的默认背景色是否可以设置为白色以外的任何颜色。但那似乎更多的是个人喜好问题。

此致,Senkwe

© . All rights reserved.