Winfrom重绘ComboBox-DropDownList样式

原生winfrom的ComboBox 样式设置为DropDownList 样式固定,且大多数重绘多为重绘下拉选择框,此代码主要重绘ComboBox的边框 背景色等。

/// <summary>
/// 主要为DropDownList样式重绘(特定性较强)
/// </summary>
public partial class EVCombobox : ComboBox
{
    public EVCombobox()
    {
        InitializeComponent();
    }
    [DllImport("User32.dll")]
    public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

    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 = Win32API.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);
        }
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风干牛肉巴旦木

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值