的DateTimePicker和UserPaint ...文本和按钮失踪按钮、DateTimePicker、UserPaint、文本和

2023-09-04 00:51:36 作者:习惯一个人过╮

我有一个DateTimePicker我'动态'分配油漆事件,不幸的是,只要这个事件被分配,无论是文本还是下拉键式渲染(但工作)。

I've got a DateTimePicker which I 'dynamically' assign the Paint-Event, unfortunately as long as this Event is assigned neither the text nor the DropDown-Button are rendered (but are working).

我写了一个组件,它另需控制和借鉴的东西就可以了。

I've written a component which takes another control and draws something on it

Public Sub New(Byval parent As Control)
    Me._parent = parent
    Me._setStyle = Me._parent.GetType().GetMethod("SetStyle", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
    Me._setStyle.Invoke(Me._parent, New Object() {ControlStyles.UserPaint, True})
    Me._setStyle.Invoke(Me._parent, New Object() {ControlStyles.OptimizedDoubleBuffer, True})

    AddHandler Me._parent.Paint, AddressOf RemotePaintHandler
End Sub

Private Sub RemotePaintHandler(ByVal sender As Object, ByVal e As PaintEventArgs)
    'draw something here'
End Sub

在处理我删除处理程序的组件和重置的风格。但只要处理程序被分配的DateTimePicker呈现像没有内容的正常文本框,但按钮工作和我还可以输入值。如果我已经覆盖了的OnPaint()方法,我会简单地调用MyBase.OnPaint()或类似的...但是这是一个EventHandler ......我完全一无所知现在在这里。

At disposing of the component I'm removing the handler and resetting the styles. But as long as the Handler is assigned the DateTimePicker renders like a normal Textbox without content, but the button is working and I can also enter values. If I've overwritten the OnPaint() method I'd simply call MyBase.OnPaint() or similar...but this is an eventhandler...I'm totally clueless here right now.

推荐答案

的DateTimePicker 没有的OnPaint 方法绘制自己。这实际上周围的窗口控件 SysDateTimePick32 的包装,所以当你设置UserPaint = true,则控制(真实的)不再绘制本身只会有你的图纸。

The DateTimePicker have no OnPaint method to draw itself. It's in fact a wrapper around the windows control SysDateTimePick32, so when you set UserPaint = true, the control (real one) no longer draws itself and there will be only your drawings.

您可以从的DateTimePicker 继承,覆盖的WndProc ,响应 WM_PAINT 执行 Paint事件用户信息。下面一个例子:

You can inherit from DateTimePicker, override WndProc, respond to WM_PAINT message by executing Paint event subscribers. Here an example:

Public Class DTP
    Inherits DateTimePicker

    ' Events '
    Public Event Paint2 As PaintEventHandler

    ' Methods '
    Protected Overrides Sub WndProc(ByRef m As Message)

        MyBase.WndProc((m))

        If ((m.Msg = &HF) AndAlso (Not Me.Paint2 Is Nothing)) Then

            Dim g As Graphics = MyBase.CreateGraphics
            Me.Paint2.Invoke(Me, New PaintEventArgs( _
                                       g, _
                                       Rectangle.Round(g.VisibleClipBounds) _
                                     ) _
                            )
        End If

    End Sub

End Class

然后使用这个新的控制,而不是普通的的DateTimePicker &放大器;订阅它的Paint2事件得出你所需要的:

then use this new control instead of the regular DateTimePicker & subscribe to it's Paint2 event to draw what you need:

(你不需要设置 UserPaint = TRUE ,你捕捉 WM_PAINT 反正)的

(you don't need to set UserPaint = True, you're capturing WM_PAINT anyway)

Public Sub New(Byval parent As Control)
    Me._parent = parent
    Me._setStyle = Me._parent.GetType().GetMethod("SetStyle", _
                               Reflection.BindingFlags.Instance _
                               Or Reflection.BindingFlags.NonPublic)
    'Me._setStyle.Invoke(Me._parent, New Object(){ControlStyles.UserPaint,True})'
    Me._setStyle.Invoke(Me._parent,  _
                        New Object() {ControlStyles.OptimizedDoubleBuffer, True})

    AddHandler Me._parent.Paint2, AddressOf RemotePaintHandler
End Sub

Private Sub RemotePaintHandler(ByVal sender As Object, ByVal e As PaintEventArgs)
    'draw something here'
End Sub

希望这会有所帮助,

hope this helps,