C# Windows 程序设计 学习笔记2 Paint事件

程序一:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace PaintEvent
{
    class Program
    {
        static void Main(string[] args)
        {
            Form form = new Form();
            form.Text = "PaintEvent";
            form.Paint +=new PaintEventHandler(MyPaintHander);

            Application.Run(form);
        }

        static void MyPaintHander(object objSender , PaintEventArgs pea)
        {
            Graphics gl = pea.Graphics;
            Console.WriteLine("PaintEvent");
            gl.Clear(Color.Chocolate);
        }
    }
}


程序二:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace PaintHello
{
    class Program
    {
        static void Main(string[] args)
        {
            Form form = new Form();
            form.Text = "PaintHello";
            form.BackColor = Color.White;
            form.Paint += new PaintEventHandler(MyPaintHander);
            
            Application.Run(form);
        }

        static void MyPaintHander(object objSender, PaintEventArgs pea)
        {
            Form form = (Form)objSender;
            Graphics gl = pea.Graphics;

            gl.DrawString("PaintHello",form.Font,Brushes.Black,0,0);
        }
    }
}


 程序三:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace PaintTwoForms
{
    class Program
    {
        static Form form1 = new Form();
        static Form form2 = new Form();
        
        static void Main(string[] args)
        {
          

            form1.Text = "First Form";
            form1.BackColor = Color.White;
            form1.Paint += new PaintEventHandler(MyPaintHander);

            form2.Text = "Second Form";
            form2.BackColor = Color.White;
            form2.Paint += new PaintEventHandler(MyPaintHander);

            form2.Show();
            Application.Run(form1);
        }

        static void MyPaintHander(object objSender, PaintEventArgs pea)
        {
            Form form = (Form)objSender;
            Graphics gl = pea.Graphics;
            string str;
            if (form == form1)
            {
                str = "Hello From the form1";
            }
            else
                str = "Hello From the form2";

            gl.DrawString(str, form.Font, Brushes.Black, 0, 0);
        }
    }
}


 程序四:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace TwoPaintHanders
{
    class Program
    {
        static void Main(string[] args)
        {
            Form form1 = new Form();
            form1.Text = "TwoPaintHanders";
            form1.BackColor = Color.White;
            form1.Paint += new PaintEventHandler(MyPaintHander1);
            form1.Paint += new PaintEventHandler(MyPaintHander2);

            Application.Run(form1);

        }

        static void MyPaintHander1(object objSender, PaintEventArgs pea)
        {
            Form form = (Form)objSender;
            Graphics gl = pea.Graphics;
            gl.DrawString("First PaintEventHandler", form.Font, Brushes.Black, 0, 0);
        }

        static void MyPaintHander2(object objSender, PaintEventArgs pea)
        {
            Form form = (Form)objSender;
            Graphics gl = pea.Graphics;
            gl.DrawString("Second PaintEventHandler", form.Font, Brushes.Black, 0, 100);
        }
    }
}


 程序五:

using System;
using System.Drawing;
using System.Windows.Forms;

class HelloWorld: Form
{
    static void Main(string[] args)
    {
        Application.Run(new HelloWorld());
    }

    public HelloWorld()
    {
        Text = "HelloWorld";
        BackColor = Color.Write;
    }

    protected override void OnPaint(PaintEventArgs pea)
    {
        Graphics gl = pea.Graphics;
        gl.DrawString("Hello Windows Forms!", Font, Brushes.Black, 0, 0);
    }
}


 程序六:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace inHeritHelloWorld
{
    class inHeritHelloWorld : HelloWorld
    {
        public new static void Main(string[] args)
        {
            Application.Run(new inHeritHelloWorld());
        }

        public inHeritHelloWorld()
        {
            Text = "inHerit" + Text;
        }

        protected override void OnPaint(PaintEventArgs pea)
        {
            Graphics gl = pea.Graphics;
            gl.DrawString("Hello From inHeritHelloWorld!", Font, Brushes.Black, 0, 0);
        }
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值