久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 10153|回復: 1
收起左側

C#示波器控件 ScopeV1.0

[復制鏈接]
ID:72519 發表于 2015-1-23 03:10 | 顯示全部樓層 |閱讀模式

  1. C#示波器控件 ScopeV1.0

  2. // C#示波器控件 ScopeV1.0 測試版僅供參考
  3. // C# 2008
  4. // author: dzrjojo
  5. // date: 2010.07.28

  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Drawing;
  10. using System.Drawing.Drawing2D;
  11. using System.Drawing.Imaging;
  12. using System.Data;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Windows.Forms;
  16. using System.Threading;

  17. namespace Dongzr.WindPowerTool
  18. {
  19.     public partial class Scope : UserControl
  20.     {
  21.         public const int INTERVAL = 1000; // 采樣時間間隔1000ms
  22.         public const int MAX_CURVE_NUM = 10;    // 示波器顯示最大曲線數為10

  23.         private const int SCOPE_WIDTH = 500;    // 示波器寬度
  24.         private const int SCOPE_HEIGHT = 500;   // 示波器高度
  25.         private const int SCOPE_FRAME = 25;     // 示波器邊框寬度
  26.         private const int DATA_LENGTH = SCOPE_WIDTH;    // 示波器最大采樣點數設為示波器寬度

  27.         private Curve[] curve;
  28.         // private ReaderWriterLock datas_lock; // 單線程不需要讀寫鎖
  29.         private Brush brush;
  30.         private Font font;
  31.         private Pen white_dash_pen;
  32.         private bool hold;
  33.         private Bitmap bitmap;

  34.         public Scope()
  35.         {
  36.             InitializeComponent();
  37.         }
  38.         // 添加曲線
  39.         public bool CreateCurve(int id,Color color,string unit)
  40.         {
  41.             bool re = false;
  42.             if (id >= 0 && id < MAX_CURVE_NUM)
  43.             {
  44.                 // datas_lock.AcquireWriterLock(-1);
  45.                 if (this.curve[id] == null)
  46.                 {
  47.                     this.curve[id] = new Curve();
  48.                 }
  49.                 if (this.curve[id].show == false)
  50.                 {
  51.                     this.curve[id].Init(true, color, DATA_LENGTH, INTERVAL, unit);
  52.                     re = true;
  53.                 }
  54.                 // datas_lock.ReleaseWriterLock();
  55.             }
  56.             return re;
  57.         }
  58.         // 刪除曲線
  59.         public void RemoveCurve(int id)
  60.         {
  61.             // datas_lock.AcquireWriterLock(-1);
  62.             if (this.curve[id] != null)
  63.             {
  64.                 if (this.curve[id].show == true)
  65.                 {
  66.                     this.curve[id].show = false;
  67.                 }
  68.             }
  69.             // datas_lock.ReleaseWriterLock();
  70.             this.Invalidate();
  71.         }
  72.         // 添加單點數據
  73.         public void AddData(int id,float f)
  74.         {
  75.             if (this.hold == false)
  76.             {
  77.                 // datas_lock.AcquireWriterLock(-1);
  78.                 if (this.curve[id] != null)
  79.                 {
  80.                     if (this.curve[id].show == true)
  81.                     {
  82.                         for (int i = Curve.points - 1; i > 0; i--)
  83.                         {
  84.                             if (this.curve[id].counter == 0)
  85.                             {
  86.                                 this.curve[id].data_raw[i].X = 0;
  87.                             }
  88.                             else
  89.                             {
  90.                                 this.curve[id].data_raw[i].X = this.curve[id].data_raw[i - 1].X + 1;
  91.                             }
  92.                             this.curve[id].data_raw[i].Y = this.curve[id].data_raw[i - 1].Y;
  93.                         }
  94.                         this.curve[id].data_raw[0].X = 0;
  95.                         this.curve[id].data_raw[0].Y = f;
  96.                         if (this.curve[id].counter < Curve.points)
  97.                         {
  98.                             this.curve[id].counter++;
  99.                         }
  100.                     }
  101.                 }
  102.                 // datas_lock.ReleaseWriterLock();
  103.                 this.Invalidate();
  104.             }
  105.         }
  106.         // 暫停波形
  107.         public bool Hold
  108.         {
  109.             set
  110.             {
  111.                 this.hold = value;
  112.             }
  113.         }
  114.         // 設置曲線參數
  115.         private void SetCurveOffsetX(int id, int offset_x) // 暫時禁止X軸偏置設置,其原點在右,向左為正方向
  116.         {
  117.             if (this.curve[id] != null)
  118.             {
  119.                 Curve.offset_x = offset_x;
  120.                 this.Invalidate();
  121.             }
  122.         }
  123.         public void SetCurveOffsetY(int id, int offset_y)
  124.         {
  125.             if (this.curve[id] != null)
  126.             {
  127.                 this.curve[id].offset_y = offset_y;
  128.                 this.Invalidate();
  129.             }
  130.         }
  131.         public void SetCurveZoomX(int id, float zoom_x)
  132.         {
  133.             if (this.curve[id] != null)
  134.             {
  135.                 if (zoom_x >= 1.0f)     // 限制X軸縮放比例 >=1
  136.                 {
  137.                     Curve.zoom_x = zoom_x;
  138.                     this.Invalidate();
  139.                 }
  140.             }
  141.         }
  142.         public void SetCurveZoomY(int id, float zoom_y)
  143.         {
  144.             if (this.curve[id] != null)
  145.             {
  146.                 this.curve[id].zoom_y = zoom_y;
  147.             }
  148.         }

  149.         // 示波器初始化
  150.         private void Scope_Load(object sender, EventArgs e)
  151.         {
  152.             // datas_lock = new ReaderWriterLock();
  153.             curve = new Curve[MAX_CURVE_NUM];
  154.             Curve.InitStatic(DATA_LENGTH, INTERVAL, 0, 1.0f);
  155.             brush = new SolidBrush(Color.Yellow);
  156.             font = new Font("SimSun", 10, System.Drawing.FontStyle.Regular);
  157.             white_dash_pen = new Pen(Color.White, 1);
  158.             white_dash_pen.DashStyle = DashStyle.Dot;
  159.             hold = false;
  160.         }
  161.         // size改變時刷新
  162.         private void Scope_Resize(object sender, EventArgs e)
  163.         {
  164.             this.Invalidate();
  165.         }
  166.         // 右鍵菜單about
  167.         private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
  168.         {
  169.             MessageBox.Show("Scope V1.0\r\n\r\nCopyRight dongzr 2010-2012", "About...");
  170.         }
  171.         // 繪圖,雙緩沖在Scope屬性里設置
  172.         private void Scope_Paint(object sender, PaintEventArgs e)
  173.         {
  174.             Graphics g = e.Graphics;
  175.             g.SmoothingMode = SmoothingMode.HighSpeed;
  176.             g.PixelOffsetMode = PixelOffsetMode.HighSpeed;
  177.             g.Clear(Color.DarkGray);
  178.             g.ScaleTransform(-(float)this.Width / (SCOPE_WIDTH + SCOPE_FRAME*2),
  179.                              -(float)this.Height / (SCOPE_HEIGHT + SCOPE_FRAME*2) ); // 縮放,并使XY軸反向
  180.             g.TranslateTransform(-(SCOPE_WIDTH + SCOPE_FRAME) , - (SCOPE_HEIGHT/2 + SCOPE_FRAME)); // 新的坐標原點,位于波形顯示區的右側中點
  181.             // 示波器顯示范圍指示
  182.             g.SetClip(new Rectangle(0, SCOPE_HEIGHT / 2 + 5, SCOPE_WIDTH, SCOPE_FRAME - 10));
  183.             g.Clear(Color.Black);
  184.             g.DrawRectangle(Pens.YellowGreen, new Rectangle(0, SCOPE_HEIGHT / 2 + 10, (int)(SCOPE_WIDTH / Curve.zoom_x), SCOPE_FRAME - 20));
  185.             // 繪制零點指示標志
  186.             g.SetClip(new Rectangle(SCOPE_WIDTH + 5, -SCOPE_HEIGHT / 2, SCOPE_FRAME - 10, SCOPE_HEIGHT));
  187.             g.Clear(Color.Black);
  188.             for (int ls = 0; ls < MAX_CURVE_NUM; ls++)
  189.             {
  190.                 if (this.curve[ls] != null)
  191.                 {
  192.                     if (this.curve[ls].show == true)
  193.                     {
  194.                         g.DrawLine(this.curve[ls].pen, SCOPE_WIDTH + 5, this.curve[ls].offset_y, SCOPE_WIDTH + 15, this.curve[ls].offset_y + 3);
  195.                         g.DrawLine(this.curve[ls].pen, SCOPE_WIDTH + 5, this.curve[ls].offset_y, SCOPE_WIDTH + SCOPE_FRAME - 5, this.curve[ls].offset_y);
  196.                         g.DrawLine(this.curve[ls].pen, SCOPE_WIDTH + 5, this.curve[ls].offset_y, SCOPE_WIDTH + 15, this.curve[ls].offset_y - 3);
  197.                     }
  198.                 }
  199.             }           
  200.             // 設置作圖區域為波形顯示區,大小為SCOPE_WIDTH*SCOPE_HEIGHT
  201.             g.SetClip(new Rectangle(0, -SCOPE_HEIGHT / 2, SCOPE_WIDTH, SCOPE_HEIGHT));
  202.             g.Clear(Color.Black);
  203.             // 繪制坐標網格
  204.             for (int step = SCOPE_WIDTH / 50,i = 0; i <= 50; i++)
  205.             {
  206.                 if (i % 5 == 0)
  207.                 {
  208.                     g.DrawLine(white_dash_pen, i * step, -SCOPE_HEIGHT / 2, i * step, SCOPE_HEIGHT / 2);
  209.                 }
  210.                 else
  211.                 {
  212.                     g.DrawLine(white_dash_pen, i * step, -SCOPE_HEIGHT / 2, i * step, -SCOPE_HEIGHT / 2 + step / 2);
  213.                     // g.DrawLine(white_dash_pen, i * step, -step / 4, i * step, step / 4);
  214.                     g.DrawLine(white_dash_pen, i * step, SCOPE_HEIGHT / 2, i * step, SCOPE_HEIGHT / 2 - step / 2);
  215.                 }
  216.             }
  217.             for (int step = SCOPE_HEIGHT / 50, i = -25; i <= 25; i++)
  218.             {
  219.                 if (i % 5 == 0)
  220.                 {
  221.                     g.DrawLine(white_dash_pen, SCOPE_WIDTH, i * step, 0, i * step);
  222.                 }
  223.                 else
  224.                 {
  225.                     g.DrawLine(white_dash_pen, SCOPE_WIDTH, i * step, SCOPE_WIDTH - step / 2, i * step);
  226.                     // g.DrawLine(white_dash_pen, SCOPE_WIDTH / 2 + step / 4, i * step, SCOPE_WIDTH / 2 - step / 4, i * step);
  227.                     g.DrawLine(white_dash_pen, step / 2, i * step, 0, i * step);
  228.                 }
  229.             }
  230.             // 繪制data
  231.             // datas_lock.AcquireWriterLock(-1);
  232.             for (int ls = 0; ls < MAX_CURVE_NUM; ls++)
  233.             {
  234.                 if (this.curve[ls] != null)
  235.                 {
  236.                     if (this.curve[ls].show == true)
  237.                     {
  238.                     // data變換
  239.                         for (int i = 0; i < Curve.points; i++)
  240.                         {
  241.                             this.curve[ls].data_show[i].X = this.curve[ls].data_raw[i].X * Curve.zoom_x + Curve.offset_x;
  242.                             this.curve[ls].data_show[i].Y = this.curve[ls].data_raw[i].Y * this.curve[ls].zoom_y + this.curve[ls].offset_y;
  243.                         }
  244.                         // 顯示
  245.                         g.DrawLines(this.curve[ls].pen, this.curve[ls].data_show);
  246.                     }
  247.                 }
  248.             }
  249.             // datas_lock.ReleaseWriterLock();
  250.             // 繪制單位坐標
  251.             g.ScaleTransform(-1, -1);
  252.             g.SetClip(new Rectangle(-SCOPE_WIDTH, SCOPE_HEIGHT / 2 + 5, SCOPE_WIDTH, SCOPE_FRAME - 10));
  253.             g.Clear(Color.Black);
  254.             g.DrawString("10     9      8      7      6      5      4      3      2      1     0", font, brush, -SCOPE_WIDTH, SCOPE_HEIGHT / 2 + 5);
  255.             g.RotateTransform(-90);
  256.             g.SetClip(new Rectangle(-SCOPE_HEIGHT / 2 , 5, SCOPE_HEIGHT, SCOPE_FRAME - 10));
  257.             g.Clear(Color.Black);
  258.             g.DrawString("-5     -4     -3     -2     -1      0      1      2      3      4      5", font, brush, -SCOPE_HEIGHT / 2, 5);
  259.         }

  260.         private void saveToolStripMenuItem_Click(object sender, EventArgs e)
  261.         {
  262.             bool isSave = true;
  263.             SaveFileDialog saveImageDialog = new SaveFileDialog();
  264.             saveImageDialog.Title = "Save Scope image";
  265.             saveImageDialog.Filter = @".bmp|*.bmp";

  266.             if (saveImageDialog.ShowDialog() == DialogResult.OK)
  267.             {
  268.                 string fileName = saveImageDialog.FileName.ToString();
  269.                 if (fileName != "" && fileName != null)
  270.                 {
  271.                     System.Drawing.Imaging.ImageFormat imgformat = null;
  272.                     imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
  273.                     if (isSave)
  274.                     {
  275.                         try
  276.                         {
  277.                             bitmap = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
  278.                             this.DrawToBitmap(bitmap, this.ClientRectangle);
  279.                             bitmap.Save(fileName, imgformat);
  280.                             MessageBox.Show("圖片已經成功保存!");
  281.                         }
  282.                         catch
  283.                         {
  284.                             MessageBox.Show("保存失敗!");
  285.                         }
  286.                         finally
  287.                         {
  288.                             bitmap.Dispose();
  289.                         }
  290.                     }
  291.                 }
  292.             }
  293.         }
  294.     }

  295.     internal class Curve
  296.     {
  297.         public bool show;   // 是否顯示
  298.         public PointF[] data_raw;   // 數據原始值
  299.         public PointF[] data_show;   // 數據顯示值
  300.         public Pen pen;   // 畫筆
  301.         public static int points;   // 采樣點數
  302.         public string unit;   // 數據的單位
  303.         public static int interval;   // 采樣點的時間間隔,單位ms
  304.         public static int offset_x;   // 橫軸偏置
  305.         public int offset_y;   // 縱軸偏置
  306.         public static float zoom_x;   // 橫軸縮放比例
  307.         public float zoom_y;   // 縱軸縮放比例
  308.         public int counter;
  309.         public Curve()
  310.         {
  311.             this.show = false;
  312.         }
  313.         public static void InitStatic(int points, int interval, int offset_x, float zoom_x)
  314.         {
  315.             Curve.points = points;
  316.             Curve.interval = interval;
  317.             Curve.offset_x = offset_x;
  318.             Curve.zoom_x = zoom_x;
  319.         }
  320.         public void Init(bool show, Color color, int points, int interval, string unit)
  321.         {
  322.             this.show = show;
  323.             this.unit = unit;
  324.             this.pen = new Pen(color, 1);
  325.             this.data_raw = new PointF[Curve.points];
  326.             this.data_show = new PointF[Curve.points];
  327.             this.offset_y = 0;
  328.             this.zoom_y = 1.0f;
  329.             this.counter = 0;
  330.         }
  331.     }
  332. }

  333. ////////////////////////////////////////////////////////////////////////////////////////////////////

  334. this.scope1.CreateCurve(0, Color.Yellow, "");

  335. this.scope1.SetCurveZoomY(0,100);

  336. this.scope1.CreateCurve(1, Color.Cyan, "");

  337. for(int i = 0;i<500;i++)

  338. {

  339.    this.scope1.AddData(0,sin(3.14* i / 100));

  340.    this.scope1.AddData(1,cos(3.14* i / 100));

  341. }

  342. this.scope1.RemoveCurve(1);
復制代碼


回復

使用道具 舉報

ID:93490 發表于 2015-10-25 02:28 | 顯示全部樓層
這個怎么用啊?
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 91免费观看国产 | 伊人狼人影院 | 2020亚洲天堂 | 日本韩国欧美在线观看 | 成人欧美一区二区三区黑人孕妇 | 91在线资源| 亚洲精品日韩一区二区电影 | 欧美午夜精品理论片a级按摩 | 成人片免费看 | 亚洲精品久久久久久一区二区 | 成人精品久久 | 日本网站免费观看 | 亚洲成av人片在线观看无码 | 亚洲狠狠 | 国产91亚洲精品 | 免费二区| www.黄色在线观看 | 在线观看国产精品一区二区 | 国产色视频网站 | 日韩福利视频 | av中文在线观看 | 午夜免费电影 | 青青久草| a在线视频观看 | 99久久精品国产毛片 | 国产精品成人国产乱 | 久久亚洲天堂 | 日韩在线精品 | 国产精品日韩欧美一区二区 | 深夜福利影院 | 国产欧美精品区一区二区三区 | 欧美亚洲激情 | 欧美一级二级视频 | 91资源在线 | 成人精品在线观看 | 91视频.| 国产欧美日韩一区 | 影音先锋中文字幕在线观看 | jvid精品资源在线观看 | 免费视频一区二区三区在线观看 | 亚洲视频精品在线 |