C#combox框所有者绘制

时间:2012-05-31 04:53:47

标签: c#

我的组合框是空的。

   public frmDlgGraphOptions()
    {

        InitializeComponent();
        this.comboBox1 = new System.Windows.Forms.ComboBox();
        arr = new String[4];
        arr[0] = "Solid Line";
        arr[1] = "Dashed line";
        arr[2] = "Dotted Line";
        arr[3] = "Dotted Line";
         imageArr = new Image[4];
         imageArr[0] = new Bitmap("C:\\SolidLine.png");
        imageArr[1] = new Bitmap("C:\\dashedline.png");
        imageArr[2] = new Bitmap("C:\\Dotted.png");
        imageArr[3] = new Bitmap("C:\\Dotted.png");

        comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
        comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
        comboBox1.DrawItem += new DrawItemEventHandler(comboBox1_DrawItem);
        this.comboBox1.TabIndex = 1;    
        this.comboBox1.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));
        this.comboBox1.ItemHeight = 20;
        this.comboBox1.Location = new System.Drawing.Point(84, 137);
        this.comboBox1.Name = "comboBox1";
        this.comboBox1.Size = new System.Drawing.Size(200, 26);
        groupBox1.Controls.Add(comboBox1);

这是OnDrawItem的代码

 private void comboBox1_DrawItem(object sender, 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.DrawImage(imageArr[e.Index], new Point(e.Bounds.X, e.Bounds.Y));   
        e.Graphics.FillRectangle(Brushes.Bisque, e.Bounds);
        //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.DrawImage(imageArr[e.Index], new Point(e.Bounds.X, e.Bounds.Y));
        }    

首先,我在组合框中看不到任何项目。我看到空的组合框。 此外,当我将鼠标悬停在组合框上时,e.index = -1并抛出错误

谢谢你 太阳

    }

1 个答案:

答案 0 :(得分:0)

重写Paint事件并将ownerdraw属性设置为True