2009-10-20 104 views
16

我正在寻找在.NET的WinForms一个拆分按钮。一边是按钮而另一边有下拉按钮的那种。拆分按钮

我看到他们用遍了橱窗,就像在Visual Studio另存为窗口,所以我想他们已经得到了在一些图书馆的控制。

我知道有一个toolstrips,但我需要一个那toolstrips之外使用。

是否存在有一个或最好是免费的图书馆微软库? 我使用.NET 3.5

举一个例子: Example Button

+0

哈,我不知道该图像是从.NET升ibrary。 我刚刚做了一个谷歌图片搜索拆分按钮,只是选择了我发现的最好看的一个。 – Jamiegs 2009-10-22 19:18:50

回答

4

你可以自己做一个简单的版本,使用该按钮的图像。我有我自己的课程,它来自Button

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

{ 
    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和适当的填充,像这样:

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); 

这里是我的行动按钮:
c# winforms split button with menu and arrow and icon