WinForms - 无边界 DateTimePicker
WinForms - Borderless DateTimePicker
我有一个看起来像这样的库存 winform datetimepicker...
我想让它看起来像这样...
WinForm
TextBox
有一个无边框选项,我手动创建了一条水平线来给出无缝下划线的错觉 TextBox
但 DateTimePicker
没有好像有无边框选项。
我可以做些什么来使 DateTimePicker
看起来像上面的示例,或者至少删除边框以便我可以手动放置下划线?
要实现控件的自定义外观,您必须覆盖 WndProc 方法,该方法处理此控件的所有 window 消息。
protected override void WndProc(ref Message m)
{
IntPtr hDC = GetWindowDC(m.HWnd);
Graphics gdc = Graphics.FromHdc(hDC);
switch (m.Msg)
{
case WM_NC_PAINT:
SendMessage(this.Handle, WM_ERASEBKGND, hDC, 0);
SendPrintClientMsg();
SendMessage(this.Handle, WM_PAINT, IntPtr.Zero, 0);
OverrideControlBorder(gdc);
m.Result = (IntPtr)1; // indicate msg has been processed
break;
case WM_PAINT: base.WndProc(ref m);
OverrideControlBorder(gdc);
OverrideDropDown(gdc);
break;
case WM_NC_HITTEST:
base.WndProc(ref m);
if (DroppedDown)
this.Invalidate(this.ClientRectangle, false);
break;
default:
base.WndProc(ref m);
break;
}
ReleaseDC(m.HWnd, hDC);
gdc.Dispose();
}
这是我能得到的最接近的:
您必须为下拉按钮制作图像。
public class SexyDateTimePicker : DateTimePicker
{
public SexyDateTimePicker() : base()
{
this.SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Black, 0, this.ClientSize.Height -1, this.ClientSize.Width, this.ClientSize.Height -1);
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(Color.Black), 0, 0);
e.Graphics.DrawImage(Properties.Resources.DateOrTimePicker_675, new Point(this.ClientRectangle.X + this.ClientRectangle.Width - 16, this.ClientRectangle.Y));
}
}
代码量最少的答案是放置一个面板,将控件放在面板中,然后通过减小面板的大小"clip" dateTimePicker (DTP) 的边框。
我有一个看起来像这样的库存 winform datetimepicker...
我想让它看起来像这样...
WinForm
TextBox
有一个无边框选项,我手动创建了一条水平线来给出无缝下划线的错觉 TextBox
但 DateTimePicker
没有好像有无边框选项。
我可以做些什么来使 DateTimePicker
看起来像上面的示例,或者至少删除边框以便我可以手动放置下划线?
要实现控件的自定义外观,您必须覆盖 WndProc 方法,该方法处理此控件的所有 window 消息。
protected override void WndProc(ref Message m)
{
IntPtr hDC = GetWindowDC(m.HWnd);
Graphics gdc = Graphics.FromHdc(hDC);
switch (m.Msg)
{
case WM_NC_PAINT:
SendMessage(this.Handle, WM_ERASEBKGND, hDC, 0);
SendPrintClientMsg();
SendMessage(this.Handle, WM_PAINT, IntPtr.Zero, 0);
OverrideControlBorder(gdc);
m.Result = (IntPtr)1; // indicate msg has been processed
break;
case WM_PAINT: base.WndProc(ref m);
OverrideControlBorder(gdc);
OverrideDropDown(gdc);
break;
case WM_NC_HITTEST:
base.WndProc(ref m);
if (DroppedDown)
this.Invalidate(this.ClientRectangle, false);
break;
default:
base.WndProc(ref m);
break;
}
ReleaseDC(m.HWnd, hDC);
gdc.Dispose();
}
这是我能得到的最接近的:
您必须为下拉按钮制作图像。
public class SexyDateTimePicker : DateTimePicker
{
public SexyDateTimePicker() : base()
{
this.SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Black, 0, this.ClientSize.Height -1, this.ClientSize.Width, this.ClientSize.Height -1);
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(Color.Black), 0, 0);
e.Graphics.DrawImage(Properties.Resources.DateOrTimePicker_675, new Point(this.ClientRectangle.X + this.ClientRectangle.Width - 16, this.ClientRectangle.Y));
}
}
代码量最少的答案是放置一个面板,将控件放在面板中,然后通过减小面板的大小"clip" dateTimePicker (DTP) 的边框。