Procedura: creare un pulsante non rettangolare (Visual C#)

Aggiornamento: novembre 2007

Nell'esempio riportato di seguito viene illustrato come creare un pulsante con una forma diversa da quella rettangolare standard. Il codice aggiunge al modulo un pulsante dalla forma circolare e crea un gestore eventi che visualizza un messaggio quando viene fatto clic sul cerchio.

Esempio

public Form2()
{
    //
    // Required for Windows Form Designer support.
    //
    InitializeComponent();
    // Initialize the user-defined button,
    // including defining handler for Click message,
    // location and size.
    myButtonObject myButton = new myButtonObject();
    EventHandler myHandler = new EventHandler(myButton_Click);
    myButton.Click += myHandler;
    myButton.Location = new System.Drawing.Point(20, 20);
    myButton.Size = new System.Drawing.Size(101, 101);
    this.Controls.Add(myButton);
}
public class myButtonObject : UserControl
{
    // Draw the new button.
    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics graphics = e.Graphics;
        Pen myPen = new Pen(Color.Black);
        // Draw the button in the form of a circle
        graphics.DrawEllipse(myPen, 0, 0, 100, 100);
        myPen.Dispose();
    }
}
// Handler for the click message.
void myButton_Click(Object sender, System.EventArgs e)
{
    MessageBox.Show("Click");
}

Compilazione del codice

Per l’esecuzione di questo esempio è necessario un progetto Applicazione Windows Forms contenente un modulo denominato Form2.

Vedere anche

Concetti

Progettazione di un'interfaccia utente in Visual C#

Altre risorse

Controlli Button

Presentazione guidata di Visual C#