WinForm重绘Combobox控件无边框样式

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

WinForm重绘Combobox控件⽆边框样式
起因
其他⽂章⼤多介绍combobox控件下拉框的重绘,现在主要⽤途就是重绘DropDownList样式下的Combobox控件,使BackColor属性有效。

代码如下:
/// <summary>
/// 主要为DropDownList样式重绘(特定性较强)
/// </summary>
public partial class ComboboxEx : ComboBox
{
public ComboboxEx()
{
InitializeComponent();
this.DropDownStyle = ComboBoxStyle.DropDownList
}
public Color BoardColor { get; set; } = Color.White;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
//WM_PAINT = 0xf; 要求⼀个窗⼝重画⾃⼰,即Paint事件时
//WM_CTLCOLOREDIT = 0x133;当⼀个编辑型控件将要被绘制时发送此消息给它的⽗窗⼝;
//通过响应这条消息,所有者窗⼝可以通过使⽤给定的相关显⽰设备的句柄来设置编辑框的⽂本和背景颜⾊
//windows消息值表,可参考:
if (m.Msg == 0xf || m.Msg == 0x133)
{
IntPtr hDC = GetWindowDC(m.HWnd);
if (hDC.ToInt32() == 0) //如果取设备上下⽂失败则返回
{
return;
}
//建⽴Graphics对像
Graphics g = Graphics.FromHdc(hDC);
g.FillRectangle(new SolidBrush(BackColor), new Rectangle(0, 0, Width, Height));
//画边框的
//ControlPaint.DrawBorder(g, new Rectangle(0, 0, Width, Height), BoardColor, ButtonBorderStyle.Solid);
//画坚线
//ControlPaint.DrawBorder(g, new Rectangle(Width - Height, 0, Height, Height), Color.Red, ButtonBorderStyle.Solid);
Point pA = new Point(Width - 20, Height / 2 - 3);
Point pB = pA + new Size(6, 6);
Point pC = pA + new Size(12, 0);
g.DrawLine(new Pen(Color.White,2), pA, pB);
g.DrawLine(new Pen(Color.White,2), pB, pC);
if (this.SelectedIndex > -1)
{
string text = SelectedItem.ToString();
Size strSize = Size.Ceiling(g.MeasureString(text, this.Font));
g.DrawString(text, Font, new SolidBrush(ForeColor), 5, (Height - strSize.Height) / 2);
}
//g.DrawLine(new Pen(Brushes.Blue, 2), new PointF(this.Width - this.Height, 0), new PointF(this.Width - this.Height, this.Height)); //释放DC
ReleaseDC(m.HWnd, hDC);
}
}
[DllImport("User32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hwnd);
[DllImport("User32.dll")]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
}。

相关文档
最新文档