DateTimePicker 和 UserPaint...文本和按钮丢失
我有一个 DateTimePicker,我“动态”分配 Paint-Event,不幸的是,只要分配了此事件,文本和 DropDown-Button 都不会呈现(但正在工作)。
我编写了一个组件,它接受另一个控件并在其上绘制一些内容。
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() 或类似方法...但这是一个事件处理程序...我现在在这里完全无能为力。
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
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DateTimePicker
没有OnPaint
方法来绘制自身。它实际上是 Windows 控件SysDateTimePick32
的包装,因此当您设置 UserPaint = true 时,控件(真正的控件)不再绘制自身,并且只有您的绘图。您可以继承
DateTimePicker
,重写WndProc
,通过执行Paint事件
订阅者来响应WM_PAINT
消息。这里有一个例子:然后使用这个新控件代替常规的
DateTimePicker
&订阅它的 Paint2 事件来绘制您需要的内容:(您不需要设置
UserPaint = True
,您无论如何都会捕获WM_PAINT
) >希望这有帮助,
The
DateTimePicker
have noOnPaint
method to draw itself. It's in fact a wrapper around the windows controlSysDateTimePick32
, so when you set UserPaint = true, the control (real one) no longer draws itself and there will be only your drawings.You can inherit from
DateTimePicker
, overrideWndProc
, respond toWM_PAINT
message by executingPaint event
subscribers. Here an example:then use this new control instead of the regular
DateTimePicker
& subscribe to it's Paint2 event to draw what you need:(you don't need to set
UserPaint = True
, you're capturingWM_PAINT
anyway)hope this helps,
我想出了一些办法。看来 UserPaint 发生在底层控件中,而不是在 DateTimePicker 本身中,导致它呈现错误。只要设置了 Style UserPaint,就会发生这种情况。
一种解决方法是让它在 Paint-Event 中正确渲染...我担心这并不容易,但可能:
我们删除 Paint-Event 的处理程序和样式,刷新控件('导致它正确渲染) ),然后在新渲染的控件之上绘制我们的东西,并重新添加样式和 Paint-Eventhandler。
缺点是,当有人输入一些信息时,我们绘制的所有内容都会消失(如果您使用日历框,它就在那里),并且在 some_one_ 刷新控件之前可能不会回来。所以这似乎是一个丑陋的黑客,然后是一个真正的解决方法,甚至是一个解决方案[1]。
不过,我只使用 DateTimePicker 对此进行了测试,并且不知道这是否也适用于其他控件(但看起来是这样)。
[1]我的意思是,我知道解决方案,在框架内重写控件...
I figured something out. As it seems the UserPaint happens within the underlying control and not in the DateTimePicker itself, 'causing it to render faulty. This will happen as long as the Style UserPaint is set.
One workaround is to let it render correct within the Paint-Event...which is not that easy I fear, but possible:
We remove the Handler and the Style for the Paint-Event, refresh the control ('causing it to render correctly), then drawing our things on top of the newly rendered control, and re-adding the Style and the Paint-Eventhandler.
The downside is, that everything we draw disappears while somebody is entering some information (it's there if you use the calendar box), and might not come back until some_one_ refreshes the control. So this seems to be an ugly hack then a real workaround or even a solution[1].
Though, I've only tested this with a DateTimePicker, and have no clue if this also works with other controls (but it seems so).
[1] I mean, I know the solution, rewriting the Control within the Framework...