我在寻找在.NET的WinForms一个拆分按钮。那种,其中一侧是一个按钮,而另一侧有一个下拉按钮。
I'm looking for a Split Button in .NET WinForms. The kind where one side is a button and the other side has a dropdown button.
我看到他们用遍了在窗口,就像在Visual Studio另存为窗口,所以我想他们已经得到了在某些库中的控制。
I see them used all over in windows, like in the Visual Studio Save As window, so I figured they've got to have the control in some library.
我知道有一个用于toolstrips,但我需要那可用外toolstrips的。
I know there's one for toolstrips, but I need one thats usable outside of toolstrips.
有没有,有一个或preferably一个免费的图书馆微软库? 我使用.NET 3.5
Is there a Microsoft library that has one or preferably a free library? I'm using .NET 3.5
有关的例子:
For an example:
您也可以自己做一个简单的版本,使用该按钮的图像。我有一个从按钮
。
You can do a simple version yourself, using the button's image. I have my own class that is derived from Button
.
我设置的图像(这是一个向下的箭头的),像这样:
I set up the image (which is of a down arrow) like so:
{
this.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
this.Image = YourResources.split_button; // Your down-arrow image
this.TextImageRelation = System.Windows.Forms.TextImageRelation.TextBeforeImage;
}
protected override void OnClick(EventArgs e)
{
var clickPos = this.PointToClient(new System.Drawing.Point(MousePosition.X, MousePosition.Y));
// If click is over the right-hand portion of the button show the menu
if (clickPos.X >= (Size.Width - Image.Width))
ShowMenuUnderControl()
else
base.OnClick(e);
}
// If you want right-mouse click to invoke the menu override the mouse up event
protected override void OnMouseUp(MouseEventArgs mevent)
{
if ((mevent.Button & MouseButtons.Right) != 0)
ShowMenuUnderControl();
else
base.OnMouseUp(mevent);
}
// Raise the context menu
public void ShowMenuUnderControl()
{
splitMenuStrip.Show(this, new Point(0, this.Height), ToolStripDropDownDirection.BelowRight);
}
如果您还想要一个图标,在OP,您可以使用的BackgroundImage
和适当的填充,比如:
If you also wanted an icon, as in the OP, you could use a BackgroundImage
and appropriate padding, like so:
this.BackgroundImageLayout = ImageLayout.None;
this.BackgroundImage = YourResources.ButtonIcon;
// Add padding so the text doesn't overlay the background image
this.Padding = new Padding(
this.Padding.Left + this.BackgroundImage.Width,
this.Padding.Top,
this.Padding.Right,
this.Padding.Bottom);
下面是我在行动的按钮:
Here's a button of mine in action: