当ComboBox的DropDownStyle为DropDownList且DrawMode为Normal时,它看起来不错,但是当我将DrawMode更改为OwnerDrawFixed时 - 它看起来非常糟糕(类似于带有箭头的TextBox下拉)。当DrawMode不正常时,有什么解决方案可以让它看起来很好吗?
看起来像这样:
我希望它看起来像那样:
答案 0 :(得分:1)
我在VB中找到了解决方案:how-to-make-a-custom-combobox-ownerdrawfixed-looks-3d-like-the-standard-combobo 添加了一些用于绘制文本和箭头的代码。它有效:)
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);
}
我不知道当鼠标进入和离开ComboBox时如何消除闪烁。当DoubleBuffering启用时,ComboBox为黑色。但对我来说工作正常。
答案 1 :(得分:0)
当您将其更改为OwnerDrawFixed时,您应该自己处理绘图
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();
}
请参阅此链接ComboBoxRenderer Class