问题描述
||
当ComboBox的DropDownStyle为DropDownList且DrawMode为Normal时-看起来不错,但是当我将DrawMode更改为OwnerDrawFixed时-看起来很糟糕(类似于带有下拉箭头的TextBox)。有什么解决方法可以使DrawMode不正常时看起来不错?
看起来像这样:
我希望它看起来像这样:
解决方法
我在这里的VB中找到了解决方案:如何制作一个自定义组合框ownerdrawfixed的外观看起来像标准组合框的3d
添加了一些用于绘制文本和箭头的代码。有用 :)
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(),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.Height - 12);
base.OnPaint(e);
}
我不知道如何在鼠标进入和离开ComboBox时消除闪烁。启用DoubleBuffering后,ComboBox为黑色。但是对我来说很好。
, 当您将其更改为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类