C# 我的组合框在绘图模式为';t正常
当ComboBox的DropDownStyle为DropDownList且DrawMode为Normal时,它看起来不错,但当我将DrawMode更改为OwnerDrawFixed时,它看起来非常糟糕(类似于带有向下箭头的文本框)。当DrawMode不正常时,有什么解决方案可以让它看起来很好吗 看起来是这样的: 我希望它看起来像这样:C# 我的组合框在绘图模式为';t正常,c#,winforms,combobox,drop-down-menu,C#,Winforms,Combobox,Drop Down Menu,当ComboBox的DropDownStyle为DropDownList且DrawMode为Normal时,它看起来不错,但当我将DrawMode更改为OwnerDrawFixed时,它看起来非常糟糕(类似于带有向下箭头的文本框)。当DrawMode不正常时,有什么解决方案可以让它看起来很好吗 看起来是这样的: 我希望它看起来像这样: 当您将其更改为OwnerDrawFixed时,您应该自己处理绘图 private void comboBox1_DrawItem(object
当您将其更改为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();
}
查看此链接我在VB中找到了解决方案: 添加了一些用于绘制文本和箭头的代码。有效:)
我不知道如何消除鼠标进出组合框时的闪烁。启用双缓冲时,组合框为黑色。但对我来说效果很好。好的,我编辑了我的帖子并添加了e.DruckGround();在这种情况下,我认为您应该将DropDownStyle更改为ComboBoxStyle.DropDownList您现在是否找到了消除闪烁的方法?您频繁调用
CreateGraphics
将泄漏GDI资源。您应该使用一个包装在using()
块中的Graphics
对象,以确保调用其Dispose
方法。
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);
}