更新 : 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");
}
コードのコンパイル方法
この例では、Windows フォーム アプリケーション プロジェクトに Form2 という名前のフォームがあることが必要です。
参照
概念
ユーザー インターフェイスのデザイン (Visual C#)