(C#)Winform修改DateTimePicker控件的背景色

本文详细介绍了如何通过继承DateTimePicker并重写WndProc方法来实现对Winform日期控件DateTimePicker的背景色和边框色自定义,包括定义属性、重写方法等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(C#)Winform修改DateTimePicker控件的背景色Winform中日期控件DateTimePicker默认是不能修改背景色和边框色的,如果想要改变它的背景色和边框色
那也是有办法的,只需要继承DateTimePicker做一个自定义控件,再重写WndProc方法。此外还要重写属性,这样就可以在外部修改它的颜色了。 自定义控件的完整代码如下: 
public class UCDateTime : DateTimePicker
    { 
[DllImport("user32.dll", EntryPoint = "SendMessageA")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, object lParam);


        [DllImport("user32")]
        private static extern IntPtr GetWindowDC(IntPtr hWnd);


        [DllImport("user32")]
        private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
        const int WM_ERASEBKGND = 0x14;
        const int WM_NC_PAINT = 0x85;
        const int WM_PAINT = 0xF;
        const int WM_PRINTCLIENT = 0x318;


        //边框颜色
        private Pen BorderPen = new Pen(SystemColors.ControlDark, 2);


        /// <summary>
        /// 定义背景色私有变量
        /// </summary>
        private Color _backColor = Color.White;
        /// <summary>
        /// 定义背景色属性
        /// </summary>
        public override Color BackColor
        {
            get
            {
                return _backColor;
            }
            set
            {
                _backColor = value;
            }
        }
        
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            IntPtr hDC = IntPtr.Zero;
            Graphics gdc = null;
            switch (m.Msg)
            {
                //画背景色
                case WM_ERASEBKGND:
                    gdc = Graphics.FromHdc(m.WParam);
                    gdc.FillRectangle(new SolidBrush(_backColor), new Rectangle(0, 0, this.Width, this.Height));
                    gdc.Dispose();
                    break;
                case WM_NC_PAINT:
                    hDC = GetWindowDC(m.HWnd);
                    gdc = Graphics.FromHdc(hDC);
                    SendMessage(this.Handle, WM_ERASEBKGND, hDC, 0);
                    SendPrintClientMsg();
                    SendMessage(this.Handle, WM_PAINT, IntPtr.Zero, 0);
                    m.Result = (IntPtr)1;    // indicate msg has been processed
                    ReleaseDC(m.HWnd, hDC);
                    gdc.Dispose();
                    break;
                //画边框
                case WM_PAINT:
                    base.WndProc(ref m);
                    hDC = GetWindowDC(m.HWnd);
                    gdc = Graphics.FromHdc(hDC);
                    OverrideControlBorder(gdc);
                    ReleaseDC(m.HWnd, hDC);
                    gdc.Dispose();
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }


        private void SendPrintClientMsg()
        {
            // We send this message for the control to redraw the client area
            Graphics gClient = this.CreateGraphics();
            IntPtr ptrClientDC = gClient.GetHdc();
            SendMessage(this.Handle, WM_PRINTCLIENT, ptrClientDC, 0);
            gClient.ReleaseHdc(ptrClientDC);
            gClient.Dispose();
        }


        private void OverrideControlBorder(Graphics g)
        {
           g.DrawRectangle(BorderPen, new Rectangle(0, 0, this.Width, this.Height));
        }
}
### 调整 C# WinForms 中 DateTimePicker 控件日历面板字体大小 对于 `DateTimePicker` 控件,默认情况下无法直接通过属性设置其内部日历弹出框的字体大小。然而,可以通过继承并重写该控件的方式实现这一需求。 #### 自定义 DateTimePicker 类以支持更大的字体 为了改变日历部分的字体,创建一个新的类继承自 `DateTimePicker` 并覆盖 `WndProc` 方法处理特定的消息: ```csharp using System; using System.Runtime.InteropServices; using System.Windows.Forms; public class CustomDateTimePicker : DateTimePicker { private const int WM_REFLECT = 0x2000; private const int WM_NOTIFY = 0x0078; protected override void WndProc(ref Message m) { base.WndProc(ref m); if ((m.Msg == WM_REFLECT + WM_NOTIFY)) { NMHDR nmhdr = (NMHDR)m.GetLParam(typeof(NMHDR)); if (nmhdr.code == -746 /*DTN_DROPDOWN*/) { IntPtr hCal = GetParent(m.HWnd); // 获取下拉日历句柄 SendMessage(hCal, WM_SETFONT, Font.ToHfont(), new IntPtr(1)); } } } [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll")] private static extern IntPtr GetParent(IntPtr hWnd); } ``` 这段代码利用 Windows API 函数拦截到日历被展开的通知消息 (`DTN_DROPDOWN`)修改其中使用的字体[^1]。 请注意上述方法适用于 .NET Framework 的 WinForms 应用程序环境;如果使用的是其他框架或版本,则可能需要寻找不同的解决方案路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值