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

}
    }
}

(0)

相关推荐

  • C#中dotnetcharting的用法实例详解

    本文以实例形式详细讲述了dotnetcharting控件的用法.分享给大家供大家参考.具体用法分析如下: dotnetcharting 是一个很好用的图表控件,能画出很漂亮的报表,一般常用到的主要有柱状图.饼图.折线图三种. dotnetcharting 有web版.winform版多个版本可供使用,官方网址:http://www.dotnetcharting.com/ ,官网有很多示例(http://www.dotnetcharting.com/gallery/),而且有winform安装版示

  • C#实现窗体中的各个控件同比自动放缩大小

    实现方式主要是利用panel控件为主题,对于每个控件的大小位置和字体这几个属性进行记录,然后根据窗体改变的大小同时放缩. 简要步骤如下: 1.创建C#窗体程序项目. 2.Panel放置到窗体. 3.设置属性dock为fill. 4.注意MinnumSize不能设置为0, 改成大于0都行. 复制代码 代码如下: public partial class FrmDemo : Form      {          double dFrmWidth;          double dFrmHeig

  • c#后台线程访问前台控件并显示信息示例

    复制代码 代码如下: //设置为后台线程 Thread th = new Thread(delegate() { append();});th.IsBackground = true;th.Start(); //在append方法里面需要调用前台控件 public void append(){ // ... 业务处理 this.Invoke(new flushMessage(showMessage), new object[] { row["Code"].ToString(), res

  • C#使用RenderControl将GridView控件导出到EXCEL的方法

    本文实例展示了C#使用RenderControl将GridView控件导出到EXCEL的方法,是非常实用的一个功能,分享给大家供大家参考.具体如下: 主要功能代码如下: // 把GridView输出到Excel文件 private void ExportExcel(GridView gridView, string title, string title2, string fileName) { int nHideCols = 0; //如果不想输出出某列,将Visible设为false即可 f

  • C#之WinForm跨线程访问控件实例

    本文实例讲述了C#中WinForm跨线程访问控件的实现方法,分享给大家供大家参考. 具体实现方法如下: 1.跨线程访问控件委托和类的定义 复制代码 代码如下: using System; using System.Windows.Forms; namespace ahwildlife.Utils {     /// <summary>     /// 跨线程访问控件的委托     /// </summary>     public delegate void InvokeDeleg

  • C# GDI在控件上绘图的方法

    本文以在chart控件上和窗体上画矩形为例子讲述了C# GDI在控件上绘图的方法.分享给大家供大家参考.具体方法如下: 具体的实现方法就不多解释了,备注很详细,代码也很简单. 主要功能代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Te

  • C#实现根据指定容器和控件名字获得控件的方法

    本文所述为C#实现根据指定容器和控件名字获得控件的方法,在进行C#应用程序设计时有一定的借鉴价值.分享给大家供大家参考借鉴.具体实现方法如下: 功能代码如下: /// <summary> /// 根据指定容器和控件名字,获得控件 /// </summary> /// <param name="obj">容器</param> /// <param name="strControlName">控件名字</

  • C#实现跨线程操作控件方法

    本文实例讲述了C#实现跨线程操作控件方法,分享给大家供大家参考.具体实现方法如下: 由于在.net平台下Winform.wpf禁止跨线程直接访问控件,因此在必须跨线程访问控件的时候采用异步方式. 1.winform项目中跨线程访问控件: 编写一个Winform小实例:在做winform项目中,有时为了将系统运行的状态实时显示到Form中,因此添加一个RichTextbox控件实时显示系统运行日志.本例实现的操作是将日志以字符串的形式写入RichTextbox控件,因为是实时显示,所以涉及到跨线程

  • C#分屏控件用法实例

    本文实例中的自定义类PictureBox继承于UserControl,最终实现简单的分屏功能.分享给大家供大家参考之用.具体实现代码如下: public partial class PictureControl : UserControl { public event MouseEventHandler PicControl_DoubleClick; private int picNum; /// <summary> /// 屏幕个数 /// </summary> public i

  • C# 开发圆角控件(窗体)的具体实现

    最近在做卡片视图的程序,要求将控件做成带有圆角的效果,下面是我在网上查找的资料,经过测试,确定可以实现功能.其中方法三既适应于控件,也适应于窗体. 先上传效果图: 方法一: 增加命名空间:using System.Drawing.Drawing2D;  添加方法如下:当然各角的点可根据需要确定. 复制代码 代码如下: private void Type(Control sender, int p_1, double p_2)        {            GraphicsPath oP

  • C#遍历DataSet控件实例总结

    本文实例总结了C#遍历DataSet控件的方法.分享给大家供大家参考.具体方法如下: DataSet控件在.net主要是用来存储数据的,它更像一个表格或数组,下面就来给大家简单介绍C#遍历DataSet控件实例. 复制代码 代码如下: for(i=0;i<ds.table[0].Row.Count;i++) {    string str=ds.table[0].Row[i][0];//第I排第一列的值    string str1=ds.table[0].Row[i][1];//第I排第二列的

  • C#中改变DataGridView控件边框颜色的方法

    DataGridView是Visual Studio中一个最重要的数据控件.它可以应用在大多数场合,功能强大,使用灵活.本文要重点介绍一下,如果设置DataGridView的边框颜色. 比尔盖次说"Apple机上没有哪一个软件我是觉得应该是微软首创的",这说明盖次对微软软件功能强大的自信心.而乔布斯而说,微软的软件毫无艺术感可言!这说明什么,说明微软的东西--丑! 乔帮主不愧是乔帮主,真是入木三分,直中要害!是的,默认情况下的DataGridView,真是丑!尤其是那个黑色的边框,不是

随机推荐