如何在C#winforms中将背景图像放入DateTimePicker?

如何在C#winforms中将背景图像放入DateTimePicker?,c#,background-image,datetimepicker,C#,Background Image,Datetimepicker,我正在尝试创建一些用户控件 并尝试通知必须使用用户输入选择或写入控件 所以,我的想法是在控件的右上角绘制图像 我已经成功地完成了文本框控件 但是对于DateTimePicker控件,我不知道从哪里开始 下面是我的代码: public partial class DateTimePicker : System.Windows.Forms.DateTimePicker { public DateTimePicker() { InitializeComponent()

我正在尝试创建一些用户控件

并尝试通知必须使用用户输入选择或写入控件

所以,我的想法是在控件的右上角绘制图像

我已经成功地完成了文本框控件

但是对于DateTimePicker控件,我不知道从哪里开始

下面是我的代码:

public partial class DateTimePicker : System.Windows.Forms.DateTimePicker
{
    public DateTimePicker()
    {
        InitializeComponent();

    }

    protected override void OnLayout(LayoutEventArgs levent)
    {
        base.OnLayout(levent);


        Bitmap bmp  = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("MyControls.Resources.Images.required01.gif"));
        this.Parent.CreateGraphics().DrawImage(bmp, 10, 10);
    }
}
this.Parent.CreateGraphics()不会在表单上绘制任何图像。

Joshua

DateTimePicker控件在进行自定义绘制时有一些限制,因此您可能必须隐藏并覆盖控件的窗口过程

以下实现使用了一种“标签外”方式,即在完成默认绘制后,使用WM_PAINT消息在控件上绘制。请注意,在处理WM_PAINT消息时,我们通常不调用GetDC()或其等效的Graphics.FromHwnd(),但在这种情况下,我们不希望覆盖原始绘图的任何部分。我们只想在位图通过基本过程完成处理后绘制它

public partial class UserControl1 : System.Windows.Forms.DateTimePicker
{
    private Bitmap bmp = null;
    public UserControl1()
    {
        InitializeComponent();
        bmp = new Bitmap(5, 5);
        bmp.SetPixel(2, 2, Color.Red); //Placeholder, Load the bitmap here
    }
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (m.Msg == 0xf) //WM_PAINT message
        {
            Graphics g = Graphics.FromHwnd(m.HWnd);
            g.DrawImage(bmp, ClientRectangle.Width - 8, 3);
            g.Dispose();
        }
    }
}

我建议您应该创建一个由DateTimePicker和PicImages组成的自定义控件如果您想绘制背景图像,最好使用WM_ERASEBKGND:)WM_ERASEBKGND在其他情况下是正确的选择,但不是此选项,原因有二:它在Windows 7中无法使用此控件(前面提到的自定义绘制限制),程序员似乎希望控件顶部的小图像不在背景中