2009-10-20 8 views
16

Je cherche un bouton Split dans .NET WinForms. Le genre où un côté est un bouton et l'autre côté a un bouton déroulant. Je les vois partout utilisés dans les fenêtres, comme dans la fenêtre Visual Studio Save As, donc j'ai pensé qu'ils devaient avoir le contrôle dans une bibliothèque.Bouton Split dans .NET Winforms

Je sais qu'il en existe une pour les bandes d'outils, mais j'en ai besoin d'une utilisable en dehors des bandes d'outils.

Y a-t-il une bibliothèque Microsoft qui en a une ou de préférence une bibliothèque gratuite? J'utilise .NET 3.5

Pour un exemple: Example Button

+0

Ha, je ne savais pas que l'image était un de l .NET la bibliothèque Je viens de faire une recherche d'image google sur le bouton divisé et j'ai juste choisi le meilleur que j'ai trouvé. – Jamiegs

Répondre

4

Vous pouvez faire une version simple vous-même, en utilisant l'image du bouton. J'ai ma propre classe qui est dérivée de Button.

Je mis en place l'image (qui est d'une flèche vers le bas) comme ceci:

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

Si vous vouliez aussi une icône, comme dans l'OP, vous pouvez utiliser un BackgroundImage et un rembourrage approprié, comme si :

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

est ici un bouton de mes en action:
c# winforms split button with menu and arrow and icon

Questions connexes