欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-16 22:30:52
首先添加一个timer,50s 复制代码 代码如下:using system;using system.collections.generic;using syste...


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

        }
    }
}