2011-06-12 4 views
6

Cuando DropDownStyle de ComboBox es DropDownList y DrawMode es Normal - se ve bien, pero cuando cambio DrawMode a OwnerDrawFixed - se ve muy mal (similar a TextBox con flecha desplegable) . ¿Hay alguna solución para que se vea bien cuando DrawMode no es Normal?Mi ComboBox se ve mal cuando su DrawMode no es Normal

parece que: looks like that

quiero que se vea como que: I want it to look like that

Respuesta

1

Encontré la solución n en VB aquí: how-to-make-a-custom-combobox-ownerdrawfixed-looks-3d-like-the-standard-combobo Se agregó un código para dibujar texto y flecha. Funciona :)

class MyComboBox: ComboBox 
{ 
    public MyComboBox() 
    { 
     this.SetStyle(ControlStyles.Opaque | ControlStyles.UserPaint, true); 
     Items.Add("lol"); 
     Items.Add("lol2"); 
    } 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     if (DroppedDown) 
      ButtonRenderer.DrawButton(CreateGraphics(), new System.Drawing.Rectangle(ClientRectangle.X - 1, ClientRectangle.Y - 1, ClientRectangle.Width + 2, ClientRectangle.Height + 2), PushButtonState.Pressed); 
     else 
      ButtonRenderer.DrawButton(CreateGraphics(), new System.Drawing.Rectangle(ClientRectangle.X - 1, ClientRectangle.Y - 1, ClientRectangle.Width + 2, ClientRectangle.Height + 2), PushButtonState.Normal); 
     if (SelectedIndex != -1) 
     { 
      Font font; 
      if (SelectedItem.ToString().Equals("lol")) 
       font = new Font(this.Font, FontStyle.Bold); 
      else 
       font = new Font(this.Font, FontStyle.Regular); 
      e.Graphics.DrawString(Text, font, new SolidBrush(Color.Black), 3, 3); 
     } 
     if (DroppedDown) 
      this.CreateGraphics().DrawImageUnscaled(new Bitmap("c:\\ArrowBlue.png"), ClientRectangle.Width - 13, ClientRectangle.Height - 12); 
     else 
      this.CreateGraphics().DrawImageUnscaled(new Bitmap("c:\\ArrowGray.png"), ClientRectangle.Width - 13, ClientRectangle.Height - 12); 
     base.OnPaint(e); 
    } 

No sé cómo eliminar el parpadeo cuando el mouse está entrando y saliendo de ComboBox. Cuando DoubleBuffering está habilitado, ComboBox es negro. Pero funciona bien para mí

+0

¿Por casualidad ha encontrado cómo eliminar este parpadeo ahora? – Otiel

+0

Sus frecuentes llamadas a 'CreateGraphics' perderán los recursos de GDI. Debería usar un único objeto 'Graphics' envuelto en un bloque' using() 'para asegurarse de que se llame a su método 'Dispose'. – Dai

0

cuando se cambia a OwnerDrawFixed, que debe manejar el dibujo mismo

 private void comboBox1_DrawItem(object sender, DrawItemEventArgs e) 
       { 
        //Wrtie your code here 
    e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), this.Font, Brushes.Black,e.Bounds); 
e.DrawBackground(); 

       } 

Vea este enlace ComboBoxRenderer Class

+0

Ok, edité mi publicación y agregué e.DrawBackground(); – DeveloperX

+0

en este caso, creo que debería cambiar el DropDownStyle a ComboBoxStyle.DropDownList – DeveloperX

Cuestiones relacionadas