首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >.NET Winforms中的拆分按钮

.NET Winforms中的拆分按钮
EN

Stack Overflow用户
提问于 2009-10-20 21:09:08
回答 1查看 15.6K关注 0票数 19

我正在寻找.NET WinForms中的拆分按钮。其中一边是一个按钮,另一边有一个下拉按钮。

我看到它们在窗口中到处使用,比如在Visual Studio的另存为窗口中,所以我认为它们一定是在某个库中拥有控件。

我知道有一个用于工具条,但我需要一个可以在工具条之外使用的工具条。

有没有微软的库有一个,或者最好是免费的库?我使用的是.NET 3.5

举个例子:

EN

回答 1

Stack Overflow用户

发布于 2015-01-15 19:37:28

你可以自己做一个简单的版本,使用按钮的图像。我有自己的类,它是从Button派生的。

我设置的图像(这是一个向下箭头)如下:

代码语言:javascript
复制
{
    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和适当的填充,如下所示:

代码语言:javascript
复制
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);

这是我的一个实际操作按钮:

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1597320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档