zoukankan      html  css  js  c++  java
  • C# GDI+编程

    窗口刷新的时候,会产生Paint事件,那么我们给这个事件添加一个处理函数。然后在这个函数里画图。就能保证所画的图不被刷新掉,

    它可以总是显示。Paint事件对应的委托是:public delegate void PaintEventHandler(object sender, PaintEventArgs e);

    示例1:在窗口中间画一根线。(创建WindowsForms应用程序)

                Graphics g = this.CreateGraphics();
                Pen p = new Pen(Color.Black);
                //在屏幕中间画一根线
                g.DrawLine(p, 0, this.Height / 2, this.Width, this.Height / 2);
                //画一个矩形
                g.DrawRectangle(p, 50, 50, 200, 100);
                //画一个圆
                g.DrawEllipse(p, 150, 150, 100, 100);
                //手动释放资源
                g.Dispose();
                p.Dispose();        

    示例2:一个填充矩形

            private void formPaint(Object sender, PaintEventArgs e)
            {
                Graphics graphics = e.Graphics;
                //蓝色画刷
                SolidBrush brush = new SolidBrush(Color.FromArgb(0, 0, 255));
                //一个矩形
                Rectangle rect = new Rectangle(0, 0, 100, 100);
                //填充一个矩形
                graphics.FillRectangle(brush, rect);
            }

    示例3:画一张png图片(用PNG是因为可以显示透明的图片,GIF图片也有这个作用)

            private void formPaint(Object sender, PaintEventArgs e)
            {
                Graphics graphics = e.Graphics;
                //加载图片
                Image img = Image.FromFile(Application.StartupPath+ @"111.png");
                //图片显示起始位置
                Point strPoint=new Point(50,50);
                //不限制大小绘制
                graphics.DrawImage(img, strPoint);
                //缩小图片绘制,限制在一个矩形内
                Rectangle rect=new Rectangle(50,50,100,100);
                graphics.DrawImage(img, rect);
            }

    示例4:用DrawString显示文字

    DrawString在Grahpics类里有好几个重载,有的可以让字符串在一个矩形内显示,有的可以使用特定的显示格式。这里就不做详细介绍了。

    只讲比较常用的。

    看例子吧,处理键盘输入字符事件,在窗口显示输入的字符。如下:

        public partial class Form2 : Form
        {
            public static String strText = "";
            public Form2()
            {
                InitializeComponent();
                this.Paint += formPaint;
            }
            private void formPaint(Object sender, PaintEventArgs e)
            {
                Graphics graphics = e.Graphics;
                //创建画刷
                SolidBrush brush=new SolidBrush(Color.FromArgb(0,255,0));
                //创建字体
                Font font=new Font("宋体",20f);
                //显示字符串,在一个矩形内
                graphics.DrawString(strText, font, brush, this.ClientRectangle);
             
            }
            private void Form2_MouseDown(object sender, MouseEventArgs e)
            {
                strText += e.Button.ToString();
                //刷新整个窗口
                this.Invalidate();
            }
        }

     效果图如下:

    示例5:重写OnPaintBackground绘制背景的方法

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            //重写OnPaintBackground方法
            protected override void OnPaintBackground(PaintEventArgs e)
            {
                //禁止基类处理,我们自己来绘制背景
                //base.OnPaintBackground(e);
                //透明背景画刷
                SolidBrush brush=new SolidBrush(Color.Transparent);
                //填充整个窗口
                e.Graphics.FillRectangle(brush,this.ClientRectangle);
                //再画一个圆圈
                Pen pen = new Pen(Color.FromArgb(0, 255, 0),3);
                e.Graphics.DrawEllipse(pen, this.ClientRectangle);
            }
        }

    示例6:TextureBursh图片画刷

    可以用图片来填充一个形状,如矩形,圆形。图片不够大则平铺显示。

    private void formPaint(Object sender, PaintEventArgs e)
            {
                Graphics graphics = e.Graphics;
                //创建图片画刷
                Rectangle rect = new Rectangle(10, 10, 70, 70);
                TextureBrush brush = new TextureBrush(Image.FromFile(Application.StartupPath+ @"111.png"),rect); 
           graphics.FillEllipse(brush,
    0, 0, 200, 200);
         }

    构造函数最后一个参数,rect表示要用图片的哪部分进行填充,10,10表示图片起始位置(左上角),70,70表示宽度和高度,注意不能超出图片原有范围。整张图片填充的话,则不需要指定rect。构造函数里填一个参数就行了。

  • 相关阅读:
    c语言网络编程过程及函数说明
    c代码编译完整过程详解
    const关键字的常见作用
    c语言中static关键字的几个作用
    c语言结构体中字节对齐方式
    modbus协议数据格式
    CodeForces
    如何在Dev-Cpp中使用C++11中的函数:stoi、to_string、unordered_map、unordered_set、auto
    关于lower_bound( )和upper_bound( )的常见用法
    CodeForces 600C——思维
  • 原文地址:https://www.cnblogs.com/jiangshuai52511/p/7679217.html
Copyright © 2011-2022 走看看