⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ddrawbox.cs

📁 用C#写的USB数据采集程序
💻 CS
📖 第 1 页 / 共 4 页
字号:
                    int linex = 25 + (int)((_selectedx - _minX) * dx);
                    int liney;
                    if ((_selectedx < _data.Length + _datastart) && (_selectedx >= _datastart))
                    {
                        int index = Math.Max(_minX, _datastart);
                        liney = _p[_selectedx - index + 1].Y;
                        _g.DrawLine(_penselectline, linex, 15, linex, liney);
                        info = _labelX + ":" + Convert.ToString(_selectedx) + "," + _labelY + ":" + _data[_selectedx - _datastart].ToString();
                    }
                    else
                    {
                        liney = Height - 15;
                        _g.DrawLine(_penselectline, linex, 15, linex, liney);
                        info = _labelX + ":" + Convert.ToString(_selectedx) + "  " + _labelY + ":0";
                    }
                    _g.DrawString(info, font, _brushcoordinate, _databmp.Width / 2 - _g.MeasureString(info, font).Width / 2, 0);
                }//using font
            }//if
            //画y坐标线
            _g.DrawLine(_pencoordinate, 25, 10, 25, Height - 15);
            //画x坐标线
            _g.DrawLine(_pencoordinate, 25, Height - 15, Width - 25, Height - 15);
            this.Invalidate();
        }
        #endregion

        #region "绘制ROI和显示刻度"
        /// <summary>
        /// 绘制ROI和显示刻度
        /// </summary>
        protected void PaintROI()
        {
            Point[] p;
            int minX;
            int maxX;
            Brush brush = new SolidBrush(Color.White);
            Pen pen = new Pen(Color.White);

            foreach (ROI roi in _roi.Values)
            {
                if (roi.Name == _activatedroi)
                {
                    continue;
                }
                if ((roi.StartX < _maxX) || (roi.EndX > _minX))
                {
                    minX = Math.Max(roi.StartX, Math.Max(_minX, _datastart));
                    maxX = Math.Min(roi.EndX, Math.Min(_maxX, _dataend));
                    if (maxX <= minX)
                    {
                        continue;
                    }
                    p = new Point[maxX - minX + 3];
                    Array.Copy(_p, minX - Math.Max(_minX, _datastart) + 1, p, 1, p.Length - 2);
                    p[0].X = p[1].X;
                    p[p.GetUpperBound(0)].X = p[p.GetUpperBound(0) - 1].X;
                    if (_fill)
                    {
                        p[0].Y = Height - 15;
                        p[p.GetUpperBound(0)].Y = Height - 15;
                        brush.Dispose();
                        brush = new SolidBrush(roi.ROIColor);
                        _g.FillPolygon(brush, p);
                    }
                    p[0].Y = p[1].Y;
                    p[p.GetUpperBound(0)].Y = p[p.GetUpperBound(0) - 1].Y;
                    pen.Color = roi.ROIColor;
                    _g.DrawLines(pen, p);
                }                
            }//foreach

            if (_roi.Contains(_activatedroi))
            {
                ROI roi = (ROI)_roi[_activatedroi];
                if ((roi.StartX < _maxX) || (roi.EndX > _minX))
                {
                    minX = Math.Max(roi.StartX, Math.Max(_minX, _datastart));
                    maxX = Math.Min(roi.EndX, Math.Min(_maxX, _dataend));
                    if (maxX > minX)
                    {
                        p = new Point[maxX - minX + 3];
                        Array.Copy(_p, minX - Math.Max(_minX, _datastart) + 1, p, 1, p.Length - 2);
                        p[0].X = p[1].X;
                        p[p.GetUpperBound(0)].X = p[p.GetUpperBound(0) - 1].X;
                        if (_fill)
                        {
                            p[0].Y = Height - 15;
                            p[p.GetUpperBound(0)].Y = Height - 15;
                            brush.Dispose();
                            brush = new SolidBrush(_activatedcolor);
                            _g.FillPolygon(brush, p);
                        }
                        p[0].Y = p[1].Y;
                        p[p.GetUpperBound(0)].Y = p[p.GetUpperBound(0) - 1].Y;
                        pen.Color = _activatedcolor;
                        _g.DrawLines(pen, p);
                    }//if
                }//if
            }//if

            //显示刻度
            if (_showcalibrate)
            {
                if ((_selectedx >= _minX) && (_selectedx <= _maxX))
                {
                    using (Font font = new Font("Tahoma", 7, FontStyle.Regular))
                    {
                        int maxx = _databmp.Width - 49;
                        float dx = (float)maxx / (_maxX - _minX);
                        string info;
                        int linex = 25 + (int)((_selectedx - _minX) * dx);
                        int liney;
                        if ((_selectedx < _data.Length + _datastart) && (_selectedx >= _datastart))
                        {
                            int index = Math.Max(_minX, _datastart);
                            liney = _p[_selectedx - index + 1].Y;
                        }
                        else
                        {
                            liney = Height - 15;
                        }
                        liney /= 2;
                        //计算刻度值
                        double result = 0;
                        double d;
                        for (int i = 0; i < _calibratecoefficient.Length; i++)
                        {
                            d = Math.Pow(_selectedx, i);
                            result += d * _calibratecoefficient[i];
                        }
                        //画刻度
                        info = _labelcalibrate + ":" + result.ToString("E3");
                        using (Brush b = new SolidBrush(_selectlinecolor))
                        {
                            if (linex - _g.MeasureString(info, font).Width / 2 > 27)
                            {
                                if (_g.MeasureString(info, font).Width / 2 + linex < _databmp.Width - 25)
                                {
                                    _g.FillRectangle(_brushfill, linex - (int)(_g.MeasureString(info, font).Width / 2), liney, (int)(_g.MeasureString(info, font).Width), (int)(_g.MeasureString(info, font).Height));
                                    _g.DrawString(info, font, b, linex - _g.MeasureString(info, font).Width / 2, liney);
                                }
                                else
                                {
                                    _g.FillRectangle(_brushfill, _databmp.Width - 25 - (int)(_g.MeasureString(info, font).Width), liney, (int)(_g.MeasureString(info, font).Width), (int)(_g.MeasureString(info, font).Height));
                                    _g.DrawString(info, font, b, _databmp.Width - 25 - _g.MeasureString(info, font).Width, liney);
                                }
                            }
                            else
                            {
                                _g.FillRectangle(_brushfill, 27, liney, (int)(_g.MeasureString(info, font).Width), (int)(_g.MeasureString(info, font).Height));
                                _g.DrawString(info, font, b, 27, liney);
                            }
                        }//using brush
                    }//using font
                }//if
            }//if
            this.Invalidate();
        }
        #endregion

        #region "绘制所有"
        /// <summary>
        /// 绘制所有
        /// </summary>
        protected virtual void Draw(bool paintbackground)
        {
            if (paintbackground)
            {
                PaintBG();
            }
            BuildDataPoint();
            PaintSelected();
            PaintROI();
        }
        #endregion

        #region "获取数据坐标点"
        /// <summary>
        /// 获取数据坐标点
        /// </summary>
        /// <returns></returns>
        protected Point[] GetPoints()
        {
            BuildDataPoint();
            Point[] p = new Point[_p.Length - 2];
            Array.Copy(_p, 1, p, 0, p.Length);
            return p;
        }
        #endregion

        #region "释放资源"
        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            _databmp.Dispose();
            _pencoordinate.Dispose();
            _pendata.Dispose();
            _brushcoordinate.Dispose();
            _brushdata.Dispose();
            _brushfill.Dispose();
            _brushregion.Dispose();
            _g.Dispose();
            base.Dispose(disposing);
        }
        #endregion

        #endregion

        #region "公共方法"

        #region "重设数据显示"
        /// <summary>
        /// 重设数据显示
        /// </summary>
        public void Reset()
        {
            this.Data = new int[0];
            ClearROI();
            Draw(false);
        }
        #endregion

        #region "添加感兴趣区"
        /// <summary>
        /// 添加感兴趣区
        /// </summary>
        /// <param name="name">感兴趣区名字</param>
        /// <param name="startX">感兴趣区开始的X坐标</param>
        /// <param name="endX">感兴趣区结束的X坐标</param>
        /// <param name="color">感兴趣区颜色</param>
        public void AddROI(string name, int startX, int endX, Color color)
        {
            ROI roi = new ROI(name, startX, endX, color);
            _roi[name] = roi;
            Draw(false);
        }
        #endregion

        #region "删除感兴趣区"
        /// <summary>
        /// 删除感兴趣区
        /// </summary>
        /// <param name="name">感兴趣区名字</param>
        public void RemoveROI(string name)
        {
            if (_roi.Contains(name))
            {
                _roi.Remove(name);
            }
            Draw(false);
        }
        #endregion

        #region "删除所有感兴趣区"
        /// <summary>
        /// 删除所有感兴趣区
        /// </summary>
        public void ClearROI()
        {
            _roi.Clear();
            _activatedroi = "";
            Draw(false);
        }
        #endregion

        #region "添加感兴趣区组"
        /// <summary>
        /// 添加感兴趣区组
        /// </summary>
        /// <param name="name">感兴趣区组</param>
        public void AddROIs(ROI[] rois)
        {
            _roi.Clear();
            foreach (ROI roi in rois)
            {
                AddROI(roi.Name, roi.StartX, roi.EndX, roi.ROIColor);
            }
            Draw(false);
        }
        #endregion

        #region "获得感兴趣区组"
        /// <summary>
        /// 获得感兴趣区组
        /// </summary>
        /// <param name="name">感兴趣区组</param>
        public void GetROIs(out ROI[] rois)
        {
            rois = new ROI[_roi.Values.Count];
            _roi.Values.CopyTo(rois, 0);
        }
        #endregion

        #region "获得感兴趣区"
        /// <summary>
        /// 获得感兴趣区
        /// </summary>
        /// <param name="name">感兴趣区</param>
        public ROI GetROI(string name)
        {
            if (_roi.Contains(name))
            {
                return (ROI)_roi[name];
            }
            else
            {
                throw new Exception("ROI is not exist");
            }
        }
        #endregion

        #region "测试是否存在感兴趣区"
        /// <summary>
        /// 获得感兴趣区
        /// </summary>
        /// <param name="name">感兴趣区</param>
        public bool ContainsROI(string name)
        {
            return _roi.Contains(name);
        }
        #endregion

        #endregion

        #endregion
    }

    #region "坐标枚举"
    public enum MyCoordinate
	{
		Log,Line,Calibrate
    }
    #endregion

    #region "感兴趣区数据结构"
    /// <summary>
    /// 感兴趣区数据结构
    /// </summary>
    public struct ROI
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name">感兴趣区名字</param>
        /// <param name="startX">感兴趣区开始的X坐标</param>
        /// <param name="endX">感兴趣区结束的X坐标</param>
        /// <param name="color">感兴趣区颜色</param>
        public ROI(string name, int startX, int endX, Color color)
        {
            _name = name;
            _startx = startX;
            _endx = endX;
            _color = color;
        }

        private string _name;
        /// <summary>
        /// 名字
        /// </summary>
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private int _startx;
        /// <summary>
        /// 开始X坐标
        /// </summary>
        public int StartX
        {
            get { return _startx; }
            set { _startx = value; }
        }

        private int _endx;
        /// <summary>
        /// 结束X坐标
        /// </summary>
        public int EndX
        {
            get { return _endx; }
            set { _endx = value; }
        }

        private Color _color;
        /// <summary>
        /// 颜色
        /// </summary>
        public Color ROIColor
        {
            get { return _color; }
            set { _color = value; }
        }

    }
    #endregion

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -