自绘制工具StripDropDownButton
我正在为 ToolStripDropDown
控件编写一些自定义行为。我还想修改 ToolStripDropDownButton 本身以显示彩色形状。
我发现我可以处理 Paint
事件并绘制我喜欢的任何内容。但是,有什么方法可以让按钮在绘制形状之前绘制默认背景吗?很难让背景完全正确,尤其是对于未来版本的.NET 和 Windows。
在普通的 Windows 中,我可以在绘制代码之前或之后调用默认的 proc 处理程序。我没有看到任何方法可以在 .NET 中实现这一点。或者也许有一种方法可以告诉按钮只绘制背景?
I'm writing some custom behavior for a ToolStripDropDown
control. I'd also like to modify the ToolStripDropDownButton
itself to display a colored shape.
I see that I can handle the Paint
event and draw whatever I like. However, is there any way to have the button paint the default background before I paint the shape? It would be hard to get the background exactly right, especially with future versions of .NET and Windows.
In plain ol' Windows, I could invoke the default proc handler before or after my paint code. I'm not seeing any way to accomplish that in .NET. Or perhaps there's a way to tell the button to paint only the background?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您处理
Paint 事件
(而不是覆盖
OnPaint
方法(在派生类中))基类(默认 proc 处理程序)已被调用。所有内容都会正常绘制,然后您实际上是在Paint
事件中在其之上进行绘制。您可以在这里清楚地看到这一点:技巧是确保保留足够的控件剪切矩形,以显示您想要的部分。
e.ClipRectangle
属性检索整个按钮的工作区,因此如果您用颜色样本填充它,您还将覆盖下拉箭头和默认背景。上面的演示是使用以下丑陋的示例代码创建的:
除此之外,我认为没有办法自定义基类到底绘制的内容。所有者绘制(至少在 WinForms 中)往往是一件要么全有要么全无的事情。您可以完全控制,
但这是以必须自己实施所有事情为代价的。
当然,如果您还没有注意到,
ToolStrip
控件已经看起来并不像本机 Windows 控件。更糟糕的是,它总是看起来和现在一模一样,即使在彻底改变 UI 的未来 Windows 版本中也是如此。 (
MenuStrip
受到同样的现象,并且差异在 Windows Vista/7 中非常明显,其中标准 API 菜单发生了巨大变化)。原因是这两个控件完全是用在其 WinForms 实现中编写的 C# 代码绘制的。就我个人而言,我认为它看起来非常俗气,并且不会在我的一个打赌应用程序中使用它。
您可以指定一个使用 UxTheme API 绘制按钮的自定义渲染器,这将更加接近原生菜单和工具栏的外观。 此处提供了相当全面的示例。我为 WinForms 开发编写了一些非常相似的内容,需要
ToolStrip
类的附加功能(例如嵌入组合框),而老式的
MainMenu
和ToolBar
控件仅包装其 Windows API 等效项。通过选择以这种方式执行操作,您可以更好地控制您希望调用基类渲染器的哪些部分,因为您自己显式地编写了代码。如果您是的话强烈推荐完全关心 UI、原生感觉或用户体验的类型。
When you handle the
Paint
event (as opposed to overriding theOnPaint
method in a derived class) the base class (default proc handler) is already getting called. Everything gets drawn as normal, and then you're essentially drawing on top of that in thePaint
event. You can see that clearly here:The trick is making sure that you leave enough of the control's clipping rectangle exposed to show the part you want. The
e.ClipRectangle
property retrieves the entire button's client area, so if you justfill that with a color swatch, you're going to cover up the drop-down arrow and default background, too. The above demonstration was created using the following ugly sample code:
Other than that, I don't think there's a way to customize what exactly gets drawn by the base class. Owner-drawing (at least in WinForms) tends to be an all-or-nothing affair. You get complete control,
but it comes at the price of having to implement everything yourself.
Of course, in case you haven't already noticed, the
ToolStrip
control already doesn't look like a native Windows control. And even worse, it is always going to look exactly the same as it does now,even in future versions of Windows that completely overhaul the UI. (The
MenuStrip
is plagued bythis same phenomenon, and the difference is very visible in Windows Vista/7 where the standard API menus have changed dramatically). The reason is that both controls are drawn entirely in C# code written in their WinForms implementations. Personally, I think it looks ridiculously cheesy, and wouldn't use it in one of my applications on a bet.
You can assign a custom renderer that uses the UxTheme API to draw the buttons, which will get much closer to approximating the look of the native menus and toolbars. A pretty thorough sample is available here. I've written something very similar for the WinForms development that I've done requiring the additional features of the
ToolStrip
class (such as embedding combo boxes) not offered by theold-school
MainMenu
andToolBar
controls that simply wrap their Windows API equivalents. By choosing to do things this way, you do have more control over exactly what parts of the base class renderer you wish to call, as you've written the code explicitly yourself. Highly recommended if you'rethe type that cares at all about UI, native feel, or user experience.