當組合框的DropDownStyle是DropDownList並且DrawMode是Normal時 - 它看起來不錯,但是當我將DrawMode更改爲OwnerDrawFixed時 - 它看起來非常糟糕(類似於帶有箭頭的TextBox下拉) 。 DrawMode不正常時是否有任何解決方案使它看起來不錯?當它的DrawMode不正常時,我的組合框看起來很糟
看起來像這樣:
我希望它看起來像:
當組合框的DropDownStyle是DropDownList並且DrawMode是Normal時 - 它看起來不錯,但是當我將DrawMode更改爲OwnerDrawFixed時 - 它看起來非常糟糕(類似於帶有箭頭的TextBox下拉) 。 DrawMode不正常時是否有任何解決方案使它看起來不錯?當它的DrawMode不正常時,我的組合框看起來很糟
看起來像這樣:
我希望它看起來像:
我找到了解決方案n在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);
}
我不知道如何刪除鼠標進入和離開組合框時閃爍。 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();
}
好吧,我編輯了我的帖子並添加了e.DrawBackground(); – DeveloperX 2011-06-12 09:56:38
在這種情況下,我認爲你應該改變DropDownStyle到ComboBoxStyle.DropDownList – DeveloperX 2011-06-12 10:03:18
你有沒有找到如何消除這種閃爍現在? – Otiel 2011-10-03 12:45:50
您經常打電話給'CreateGraphics'會泄漏GDI資源。你應該使用一個包含在'use()'塊中的'Graphics'對象來確保它的'Dispose'方法被調用。 – Dai 2017-07-03 22:27:30