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


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#是一种现代化的面向对象程序设计语言,它是由Microsoft于2000年推出的。C#语言最初是为了开发Windows应用程序而设计的,但现在已经可以用于开发各种类型的应用程序,包括Web和移动应用程序等。下面是一些C# Windows程序设计的基础知识: 1. 界面设计:在C#中,可以使用Windows窗体应用程序来创建图形界面。窗体应用程序包含一个主窗体和各种控件,如按钮、文本框、标签等,它们可以通过拖拽和放置的方式来创建和布局。您可以使用Visual Studio等集成开发环境来创建和编辑窗体应用程序。 2. 事件处理:当用户与应用程序交互时,不同的控件会触发不同的事件。在C#中,可以通过编写事件处理程序来响应这些事件事件处理程序可以是一个方法,当事件发生时,该方法会被自动调用。您可以在Visual Studio中使用设计器来创建事件处理程序。 3. 数据库访问:在Windows应用程序中,通常需要访问数据库来存储和检索数据。C#提供了一种名为ADO.NET的技术,可以用于连接和操作各种类型的数据库,如SQL Server、MySQL等。您可以使用ADO.NET提供的类和方法来执行数据库操作,如查询、插入、更新和删除数据等。 4. 多线程编程:在Windows应用程序中,为了避免阻塞用户界面,通常需要使用多线程编程技术。C#提供了一种名为Task Parallel Library的技术,可以用于创建和管理线程。您可以使用Task Parallel Library提供的类和方法来实现多线程编程,如创建和启动线程、线程间通信等。 5. 错误处理:在Windows应用程序中,可能会出现各种类型的错误,如用户输入错误、数据库连接错误等。为了提高程序的健壮性,需要对这些错误进行处理。C#提供了一种名为异常处理的技术,可以用于捕获和处理各种类型的异常。您可以使用try-catch语句来实现异常处理,如捕获异常、记录日志、显示错误消息等。 希望这些基础知识能够帮助您更好地理解C# Windows程序设计

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值