一、char曲线显示随机数数据
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;namespace WindowsFormsApp1
{public partial class Form1 : Form{public Form1(){InitializeComponent();CheckForIllegalCrossThreadCalls=false;this.chart1.MouseWheel += Chart1_MouseWheel;//系统不提供,鼠标滚动事件需要自己添加}Random random = new Random();Thread thread = null;private void Chart1_MouseWheel(object sender, MouseEventArgs e){if (e.Delta==120) // 鼠标向上滚动e.Delta==120{if (chart1.ChartAreas[0].AxisX.ScaleView.Size > 0){chart1.ChartAreas[0].AxisX.ScaleView.Size /= 2;}else if (e.Delta > 0) // 鼠标向下滚动e.Delta==-120{chart1.ChartAreas[0].AxisX.ScaleView.Size = chart1.Series[0].Points.Count / 2;}}else if (e.Delta == -120){if (chart1.ChartAreas[0].AxisX.ScaleView.Size > 0){chart1.ChartAreas[0].AxisX.ScaleView.Size *= 2;}else if (e.Delta > 0) // 鼠标向下滚动e.Delta==-120{chart1.ChartAreas[0].AxisX.ScaleView.Size = chart1.Series[0].Points.Count * 2;}}}private void Form1_Load(object sender, EventArgs e){thread = new Thread(RandomNumber);thread.Start(); //启动线程}private void RandomNumber(){while (true){this.chart1.Series[0].Points.AddY(random.Next(100)); //将随机数显示值曲线控件上Thread.Sleep(100);}}private void Form1_FormClosing(object sender, FormClosingEventArgs e){thread.Abort(); //结束线程}private void button1_Click(object sender, EventArgs e){thread.Suspend(); //暂停线程}private void button2_Click(object sender, EventArgs e){thread.Resume();//继续线程}}
}
运行结果: