My ComboBox looks bad when its DrawMode is not normal

When the ComboBox DropDownStyle is a DropDownList and the DrawMode is normal - it looks good, but when I change the DrawMode to OwnerDrawFixed - it looks very bad (similar to a TextBox with an arrow to drop out). Is there any solution to make it look good when DrawMode is not normal?

looks like that: looks like that

I want it to look like this: I want it to look like that

+6
source share
2 answers

I found a solution in VB here: how-to-make-a-custom-combobox-ownerdrawfixed-looks-3d-like-the-standard-combobo Added code for drawing text and arrows. He works:)

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); } 

I do not know how to remove flicker when mouse enters and exits ComboBox. When DoubleBuffering is on, the ComboBox is black. But it works great for me.

+1
source

when you change it to OwnerDrawFixed you must process the drawing

  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(); } 

See link ComboBoxRenderer Class

0
source

Source: https://habr.com/ru/post/890332/


All Articles