14.C#编程学习—— 按钮

126 阅读1分钟

14.C#编程学习—— 按钮

源码如下:

usingSystem;

usingSystem.Drawing;

usingSystem.Windows.Forms;

 

classSimpleButton : Form

{

    publicstaticvoid Main()

    {

        Application.Run(newSimpleButton());

    }

    public SimpleButton()

    {

        Text = "Simple Button";

 

        Button btn = newButton();

        btn.Parent = this;

        btn.Text = "Click Me!";

        btn.Location = newPoint(100, 100);

        btn.Click += newEventHandler(ButtonOnClick);

    }

    void ButtonOnClick(objectobj, EventArgsea)

    {

        Graphics grfx = CreateGraphics();

        Point ptText = Point.Empty;

        string str = "Button clicked!";

 

        grfx.DrawString(str, Font, newSolidBrush(ForeColor), ptText);

        System.Threading.Thread.Sleep(250);

        grfx.FillRectangle(newSolidBrush(BackColor),

             newRectangleF(ptText,grfx.MeasureString(str, Font)));

 

        grfx.Dispose();

    }

}