我正在为ToolStripDropDown控件编写一些自定义行为。我还想修改ToolStripDropDownButton本身以显示彩色形状。

我看到我可以处理Paint事件并绘制我喜欢的任何东西。但是,在绘制形状之前有没有办法绘制默认背景?很难获得完全正确的背景,特别是在未来版本的.NET和Windows。

在普通的窗口中,我可以在我的绘画代码之前或之后调用默认proc处理程序。我没有看到在.NET中实现的任何方法。或者也许有办法告诉按钮只能绘制背景?

有帮助吗?

解决方案

When you handle the Paint event (as opposed to overriding the OnPaint 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 the Paint event. You can see that clearly here:

     Lime rectangle drawn over part of the drop-down button, clearly showing the default contents underneath

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 just
fill 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:

private void ToolStripDropDownButton1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.FillRectangle(Brushes.Chartreuse,
                             e.ClipRectangle.X + 3, e.ClipRectangle.Y + 3,
                             e.ClipRectangle.Width - 12,
                             e.ClipRectangle.Height - 12);
}

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 by
this 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 the
old-school MainMenu and ToolBar 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're
the type that cares at all about UI, native feel, or user experience.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top