C#自定义波形控件

做一个类似与 任务管理器里的cpu 的波形控件

为了软件的重用 把它做成一个用户自定义控件

重写控件的OnPaint 方法

GDI+ 画 竖格 、横格  和波形  ,用指定的数组值 来控制波形的坐标位置  ,我这里用的是随机数

然后控制波形的起始位置和左移动宽度MoveWidth

添加一个 Timer  控件 在Tick 事件中 重新绘制控件,调整波形的位置

波形控件代码
   /// <summary>
    ///  作者:hzy
    ///  日期 :2013 - 4 -1 
    ///  说明:波形控件 编写一个类似与 任务管理Cpu 的波形显示
    ///  水平有待提高, 有很多的不完善,只用做 交流学习吧
    ///  实现思路 :  定义自定义控件 重写  OnPaint 方法
    ///  画出 竖格 、横格和波形,设置波形的位置
    ///  然后 定义一个Timer 控件  在Tick 事件中
    ///  重新绘制图形 并且调整 做移动的宽度  MoveWidth 
    ///  我们需要一个长度至少等于波形控件宽度的数组来存放每时刻波形的值,
    ///  因此可以确定这个数组是和控件绘图画布宽度一致的。
    ///  数组值这快还没做,为了方便用的随机数 
    ///  思路部分参考 编程中国野比的状态波形图控件
    /// </summary>
    public partial class MyBoXingControl : UserControl
    {
        private Timer myTimer;

        public Timer MyTimer
        {
            get { return myTimer; }
            set { myTimer = value; }
        }
        public MyBoXingControl()
        {
            InitializeComponent();
            //添加一个定时器
            MyTimer = new Timer();
            MyTimer.Tick += new EventHandler(MyTimer_Tick);
            this.BackColor = Color.Black;
            //this.ForeColor = Color.Lime;
        }
        
        void MyTimer_Tick(object sender, EventArgs e)
        {
            MoveWidth ++;
            Invalidate();
            //throw new NotImplementedException();
        }
       //定义向左移动的宽度
        public static int MoveWidth = 0;
        private bool isLeft=true;


        //是否左移 
        public bool IsLeft
        {
            get { return isLeft; }
            set { isLeft = value; }
        }
        /// <summary>
        /// 重写 OnPaint 方法
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {


            //定义显示格的属性
            int geWidth = 12;         //格之间的宽度
            int geXNum = this.Width / geWidth + 1;  //横向格的数目
            int geX = 0;            //x 轴 的坐标 
            int geHeight = this.Height;   // 格线的高度
            Pen pen = new Pen(Color.Green);
            //画出 竖 格
            for (int i = 0; i < geXNum+MoveWidth; i++)
            {
                // 画出  竖线    两个点坐标作为参数 
                e.Graphics.DrawLine(pen, geX - MoveWidth, 0F, geX - MoveWidth, geHeight);
                geX += geWidth;
            }

            //画出 横格

            int geYNum = this.Height / geWidth + 1; //竖向 格的数目
            int geY = 0;  //y 轴的 坐标 
            for (int l = 0; l < geYNum; l++)
            {
                //画出横向的线    两个点的坐标为参数
                e.Graphics.DrawLine(pen, 0F, geY, this.Width, geY);
                geY += geWidth;
            }

            //画出波浪
            Random rnd = new Random();
            pen.Color = Color.Lime;  //改变  画笔的颜色
            int h;
            int preY = this.Height;  //定义变量存储上一个坐标的y 轴的值

            //判断是向左移动
            if (this.isLeft)
            {
                //波形开始的位置   MoveWidth % this.Width  防止波形左移的宽度超出 本身的宽度
                int w = this.Width - MoveWidth % this.Width;
                for (int p = w; p < this.Width+w; p = p + 2)
                {

                    h = rnd.Next(1, this.Height); //随机生成一个高度  如果有指定的数据 就更好了
                    e.Graphics.DrawLine(pen, p, h, p, preY);  //画出线
                    e.Graphics.DrawLine(pen, p + 1, h, p + 1, h + 1); //P+2 中间回空一个 用一个像素来连接
                    preY = h;   //把前一个画线的起始高度  存给 preY

                }
            }
            //可以在else 处理不是左移动的代码 

            //base.OnPaint(e);
        }
    }

调用的时候  直接把控件放到窗体上就可以了

计数器启动    this.myBoXingControl1.MyTimer.Start();

 实现效果

原文地址:https://www.cnblogs.com/hzy168/p/2994397.html