C#波形图控件制作示例程序

时间:2022-02-24 21:50:40 


C#波形图控件制作示例程序

首先添加一个timer,50s


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace High_Tech_Watch
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        int[] oldLine;
        int SIZE = 15; //方格的大小
        Pen LINEPEN = new Pen(Color.FromArgb(3,64, 129), 1); //背景线条颜色
        Pen FORELINEPEN = new Pen(Color.LightBlue);  //前景线条颜色
        private void UserControl1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            int Bvalue;
            Bvalue = Value;
            if (shake != 0)
            {
                Random ro = new Random();
                int r = ro.Next(0, shake);
                Value += (ro.Next(-shake, 0) / 2) + r/2;
                if (Value>100)
                {
                    Value = 100;
                }
                if (Value < 0)
                {
                    Value = 0;
                }
            }
            int h = (int)(this.Size.Height / SIZE);
            int w = (int)(this.Size.Width / SIZE )+ 1;//这里加1保证了滚动时最右侧垂直线及时出现
            for (; h >= 0;h-- )
            {
                g.DrawLine(LINEPEN, new Point(0, h * SIZE), new Point(this.Size.Width, h * SIZE));
            }
            for (; w>=0;w-- )
            {
                g.DrawLine(LINEPEN, new Point((w * SIZE) - limits, 0), new Point((w * SIZE) - limits, this.Size.Height));
            }
            for (int i = oldLine.Length - 1,j = 0;i >j ;j++ )
            {
                g.DrawLine(FORELINEPEN, new Point(j,(this.Height - (int)(((float)oldLine[j] / (float)100) * (float)this.Height) ) -1),
                    new Point(j + 1, (this.Height - (int)(((float)oldLine[j+1] / (float)100) * (float)this.Height))-1) );
            }
            for (int i = oldLine.Length - 1, j = 0; i > j; j++)
            {
                oldLine[j] = oldLine[j + 1];
            }
            oldLine[oldLine.Length - 1] = Value;
            pintLightPoint(e);
            Value = Bvalue;
        }

        private void pintLightPoint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.DrawImage(global::High_Tech_Watch.Resource1.未标题_2,new Rectangle(new Point(this.Width - 50,this.Height - (int)(((float)lightPointValue / (float)100) * (float)this.Height ) - 10),new Size(20,20)));

        }

        int lightPointValue = 50;
        int limits = 0;//滚动就靠他了,是一个范围
        private void timer1_Tick(object sender, EventArgs e)
        {
            limits++;
            if (limits >= SIZE)
            {
                limits = 0;
            }
            this.Invalidate();
        }

        private void UserControl1_Load(object sender, EventArgs e)
        {

            oldLine = new int[this.Width - 40];

        }

        int shake = 0;
        [DefaultValue(0),Description("抖动率,值控件输入的值自动抖动(禁用是为0)"),Category("属性值")]
        public int Shake
        {
            get{return shake;}
            set{shake = value;}
        }
        [DefaultValue(0),Description("当前数值"),Category("属性值")]
        public int Value
        {
            get { return lightPointValue; }
            set { lightPointValue = value; }
        }
        [Description("当前数值"), Category("属性值")]
        public Pen LinePen
        {
            get { return LINEPEN; }
            set
            {
                LINEPEN = value;
                this.Invalidate();
            }
        }

        private void UserControl1_Resize(object sender, EventArgs e)
        {

            if ((this.Width - 40) > oldLine.Length)
            {
                 int[] newArry = new int[this.Width - 40];
                oldLine.CopyTo(newArry, newArry.Length - oldLine.Length);
                oldLine = new int[this.Width - 40];
                oldLine = newArry;
            }
            if ((this.Width - 40) < oldLine.Length)
            {
                int[] newArry = new int[this.Width - 40];
                for (int i = newArry.Length - 1,j = oldLine.Length - 1; i >=0 ;i--,j-- )
                {
                   newArry[i] = oldLine[j];
                }
                oldLine = new int[this.Width - 40];
                oldLine = newArry;
            }

        }
    }
}

标签:C#,波形图控件
0
投稿

猜你喜欢

  • Spring学习笔记之bean的基础知识

    2021-09-08 10:09:27
  • 如何使用Spring自定义Xml标签

    2022-11-14 19:01:08
  • java框架之maven是用来做什么的

    2023-04-20 21:59:43
  • C#开发微信门户及应用(5) 用户分组信息管理

    2022-08-03 04:52:20
  • SpringBoot在Controller层接收参数的n种姿势(超详细)

    2023-01-28 00:54:39
  • 深入了解JAVA HASHMAP的死循环

    2023-11-28 00:32:12
  • SpringBoot绿叶显示yml和端口问题及解决方法

    2023-12-09 00:29:13
  • java 中使用匿名类直接new接口详解及实例代码

    2021-08-13 22:26:12
  • Spring Boot整合Lombok的方法详解

    2023-11-22 09:06:21
  • Java异步调用转同步方法实例详解

    2023-11-01 20:38:25
  • springcloud LogBack日志使用详解

    2021-12-23 15:17:05
  • C#使用linq对数组进行筛选排序的方法

    2023-12-06 06:21:21
  • C#实现DataTable映射成Model的方法(附源码)

    2023-03-12 06:10:56
  • java设计模式之工厂方法模式

    2022-08-29 05:14:10
  • 从Cocos2d-x2迁移到Cocos2d-x3的过程分享

    2021-06-07 23:52:28
  • VC实现屏幕截词功能的方法详解

    2022-08-05 07:22:18
  • 浅析java中的取整(/)和求余(%)

    2023-04-30 23:46:23
  • 使用java生成字母验证码

    2021-10-29 23:50:25
  • 详解Java实现批量压缩图片裁剪压缩多种尺寸缩略图一键批量上传图片

    2022-12-07 15:56:04
  • SpringBoot工程搭建打包、启动jar包和war包的教程图文详解

    2022-05-04 22:09:10
  • asp之家 软件编程 m.aspxhome.com