如何:创建非矩形按钮 (Visual C#)
更新:2007 年 11 月
此示例演示如何创建一个形状与标准矩形按钮不同的按钮。此代码向窗体中添加一个圆形按钮,并创建一个事件处理程序,使得在单击此圆形按钮时显示一条消息。
示例
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");
}
编译代码
此示例需要一个包含名为 Form2 的窗体的 Windows 窗体应用程序项目。