C# 重绘Combobox时,选择下拉框内容后不在Combobox上显示

xuhang08 2016-10-26 02:45:39

public partial class ComboBoxEx : ComboBox
{
private string imagePath = AppDomain.CurrentDomain.BaseDirectory + "Image\\{0}.png";
private Image _normalImage = null;
private Image _hoverImage = null;
private Image _disableImage = null;
private bool mouseHover;
private bool mouseDown;

public ComboBoxEx()
{
base.SetStyle(
ControlStyles.UserPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw |
ControlStyles.SupportsTransparentBackColor,
true);
this.DrawMode = DrawMode.OwnerDrawFixed;
this.DropDownStyle = ComboBoxStyle.DropDownList;
base.UpdateStyles();
}

protected override void OnCreateControl()
{
base.OnCreateControl();
if (!DesignMode && this.Items.Count != 0)
{
this.DropDownHeight = this.Items.Count * 17;
}
ResetBitmap();
}

private void ResetBitmap()
{
this._normalImage = Image.FromFile(string.Format(imagePath,"combobox-normal"));
this._hoverImage = Image.FromFile(string.Format(imagePath,"combobox-hover"));
this._disableImage = Image.FromFile(string.Format(imagePath,"combobox-disable"));
}

protected override void OnMouseEnter(EventArgs e)
{
mouseHover = true;
this.Invalidate();
base.OnMouseEnter(e);
}

protected override void OnMouseLeave(EventArgs e)
{
mouseHover = false;
this.Invalidate();
base.OnLeave(e);
}

protected override void OnMouseDown(MouseEventArgs mevent)
{
if (mevent.Button == MouseButtons.Left)
{
mouseDown = true;
this.Invalidate();
base.OnMouseDown(mevent);
}
}

protected override void OnMouseUp(MouseEventArgs mevent)
{
mouseDown = false;
this.Invalidate();
base.OnMouseUp(mevent);
}

[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetWindowDC(IntPtr handle);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr ReleaseDC(IntPtr handle, IntPtr hDC);
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0xf || m.Msg == 0x133)
{
IntPtr hDC = GetWindowDC(m.HWnd);
if (hDC.ToInt32() == 0) return;
Graphics g = Graphics.FromHdc(hDC);
OverrideDropDown(g);
OverrideControlBorder(g);
ReleaseDC(m.HWnd, hDC);
}
}

private static int DropDownButtonWidth = 17;
private void OverrideDropDown(Graphics g)
{
if (DesignMode) return;
Rectangle rect = new Rectangle(this.Width - DropDownButtonWidth - 3, 0, DropDownButtonWidth + 3, this.Height);
g.FillRectangle(new SolidBrush(Color.FromArgb(236, 240, 241)), rect);

if (!this.Enabled)
{
g.DrawImage(this._disableImage, new Rectangle(this.Width - 15, 6, 9, 9));
}
else if (mouseDown)
{
g.DrawImage(this._hoverImage, new Rectangle(this.Width - 15, 6, 9, 9));
}
else if (mouseHover)
{
g.DrawImage(this._hoverImage, new Rectangle(this.Width - 15, 6, 9, 9));
}
else if (this.Focused)
{
g.DrawImage(this._hoverImage, new Rectangle(this.Width - 15, 6, 9, 9));
}
else
{
g.DrawImage(this._normalImage, new Rectangle(this.Width - 15, 6, 9, 9));
}
}

private void OverrideControlBorder(Graphics g)
{
if (!this.Enabled)
{
g.DrawRectangle(new Pen(Color.FromArgb(189, 195, 199), 2), new Rectangle(0, 0, this.Width, this.Height));
}
else if (mouseDown)
{
g.DrawRectangle(new Pen(Color.FromArgb(41, 128, 185), 3), new Rectangle(0, 0, this.Width - 1, this.Height - 1));
}
else if (mouseHover)
{
g.DrawRectangle(new Pen(Color.FromArgb(41, 128, 185), 2), new Rectangle(0, 0, this.Width, this.Height));
}
else if (this.Focused)
{
g.DrawRectangle(new Pen(Color.FromArgb(41, 128, 185), 3), new Rectangle(0, 0, this.Width - 1, this.Height - 1));
}
else
{
g.DrawRectangle(new Pen(Color.FromArgb(127, 140, 141), 2), new Rectangle(0, 0, this.Width, this.Height));
}
}

protected override void OnDrawItem(DrawItemEventArgs e)
{
Graphics g = e.Graphics;
Rectangle r = e.Bounds;
Font fn = null;
if (e.Index >= 0)
{
fn = e.Font;
string s = this.Items[e.Index].ToString();
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
{
e.Graphics.FillRectangle(new SolidBrush(Color.White), r);
e.Graphics.DrawString(s, fn, new SolidBrush(Color.FromArgb(127, 140, 141)), r, sf);
e.DrawFocusRectangle();
}
else
{
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(41, 128, 185)), r);
e.Graphics.DrawString(s, fn, new SolidBrush(Color.FromArgb(255, 255, 255)), r, sf);
e.DrawFocusRectangle();
}

if (!this.Focused)
{
e.Graphics.FillRectangle(new SolidBrush(Color.White), r);
e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r, sf);
e.DrawFocusRectangle();
}
}
base.OnDrawItem(e);
}
}

选择时可以看到下拉框内容,点击内容后不在combobox上显示

...全文
386 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq849786358 2017-04-18
  • 打赏
  • 举报
回复
来顶下,看有没有有大神回复
wang_peng_yl 2017-04-18
  • 打赏
  • 举报
回复
我的理解是如果你加上了ControlStyles.UserPaint , 你还得 在protected override void OnPaint(PaintEventArgs e) 方法里绘制选择以后的内容
wang_peng_yl 2017-04-18
  • 打赏
  • 举报
回复
把 ControlStyles.UserPaint 去掉就好了
xuhang08 2016-10-26
  • 打赏
  • 举报
回复
怎么没大神来指点下

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧