C # Windows Programming Study Note 2 paint event

Source: Internet
Author: User

Procedure 1:

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);        }    }}

Procedure 2:

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);        }    }}

Procedure 3:

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);        }    }}

Procedure 4:

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);        }    }}

Procedure 5:

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);    }}

Procedure 6:

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);        }    }}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.