wsPanelView

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;

namespace wsControls
{
    [ToolboxItem(true), ToolboxBitmap(typeof(System.Windows.Forms.Panel))]
    public class wsPanelView : Panel
    {
        public decimal[] dataSourceA { get; set; }

        public Collection<decimal> dataSourceB { get; set; }

        //public Color chanColorA { get; set; }
        public PointF[] pathA = new PointF[32768];

        [Description("Plot"), DisplayName("PlotProperty")]
        public wsPlotProperty MonitorProperty { get; set; }

        [Description("Plot"), DisplayName("NOP")]
        public int NOP { get; set; }

        public bool WholeTrace { get; set; }

        public int SubStart { get; set; }

        public int SubStop { get; set; }        

        public wsPlot plot;

        public bool DisableMarker { get; set; }

        private ContextMenuStrip contex = new ContextMenuStrip();
        ToolStripMenuItem contextPeak = new ToolStripMenuItem("Peak Search");
        ToolStripMenuItem contextPeakNext = new ToolStripMenuItem("Peak Next");

        ToolStripMenuItem contextMarkerOff = new ToolStripMenuItem("Marker Off");
        ToolStripSeparator contexSeperator0 = new ToolStripSeparator();
        private wsForms.FormPropety propertyform = new wsForms.FormPropety();
        ToolStripMenuItem contextProperty = new ToolStripMenuItem("繪圖屬性");
        ToolStripMenuItem contextRefresh = new ToolStripMenuItem("刷新");
        ToolStripSeparator contexSeperator = new ToolStripSeparator();
        ToolStripMenuItem contextSave = new ToolStripMenuItem("另存爲");
        #region [ Cunstructor ] [ SizeChanged ]
        public wsPanelView()
        {
            this.DoubleBuffered = true;
            this.ResizeRedraw = true;

            MonitorProperty = new wsPlotProperty();
            plot = new wsPlot(this.Width, this.Height, MonitorProperty);
            propertyform.Object = MonitorProperty;

            dataSourceA = null;

            NOP = 201;
            WholeTrace = true;
            SubStart = 0;
            SubStop = 0;

            DisableMarker = true;

            contex.Items.Add(contextPeak);
            contex.Items.Add(contextPeakNext);
            contex.Items.Add(contextMarkerOff);
            contex.Items.Add(contexSeperator0);
            contex.Items.Add(contextProperty);
            contex.Items.Add(contextRefresh);
            contex.Items.Add(contexSeperator);
            contex.Items.Add(contextSave);

            contextPeak.Click+=contextPeak_Click;
            contextPeakNext.Click+=contextPeakNext_Click;
            contextMarkerOff.Click+=contextMarkerOff_Click;
            contextProperty.Click+=contextProperty_Click;
            contextRefresh.Click+=contextRefresh_Click;
            contextSave.Click+=contextSave_Click;
            this.ContextMenuStrip = contex;

            MonitorProperty.ScaleChanged+=MonitorProperty_ScaleChanged;
            this.SizeChanged += new EventHandler(MonitorView_SizeChanged);
            this.Size = new Size(240, 240);
            this.MinimumSize = new Size(240, 240);
        }
        #endregion

        #region [ Context ] Marker 
        protected virtual void contextPeak_Click(object sender, EventArgs e)
        {
            DisableMarker = false;
            markernumber = 1;
            if (dataSourceA != null)
                markerindex = wsArray.GetPeakIndex(dataSourceA, markernumber);
            else if (dataSourceB != null)
                markerindex = wsArray.GetPeakIndex(dataSourceB, markernumber);
            else { }
            this.Refresh();
        }

        protected virtual void contextPeakNext_Click(object sender, EventArgs e)
        {
            DisableMarker = false;
            markernumber++;
            if (dataSourceA != null)
                markerindex = wsArray.GetPeakIndex(dataSourceA, markernumber);
            else if (dataSourceB != null)
                markerindex = wsArray.GetPeakIndex(dataSourceB, markernumber);
            else { }
            this.Refresh();
        }

        protected virtual void contextMarkerOff_Click(object sender, EventArgs e)
        {
            DisableMarker = true;
            markernumber = 1;
            this.Refresh();
        }

        protected virtual void MonitorProperty_ScaleChanged(object sender, EventArgs e)
        {
            MonitorView_SizeChanged(sender, e);
        }

        protected virtual void MonitorView_SizeChanged(object sender, EventArgs e)
        {
            plot.ReSize(this.Width, this.Height, MonitorProperty);
            plot.GetPositionX(NOP, MonitorProperty);
        }
        #endregion

        #region [ Context Menu ] Plot Property 
        public virtual void contextProperty_Click(object sender, EventArgs e)
        {
            propertyform.Object = MonitorProperty;
            propertyform.ShowDialog();
            MonitorProperty = (wsPlotProperty)propertyform.Object;
            plot.ReSize(this.Width, this.Height, MonitorProperty);
            plot.GetPositionX(NOP, MonitorProperty);
            this.Refresh();
        }

        public virtual void contextRefresh_Click(object sender, EventArgs e)
        {
            plot.ReSize(this.Width, this.Height, MonitorProperty);
            plot.GetPositionX(NOP, MonitorProperty);
            this.Refresh();
        }
        #endregion

        SaveFileDialog sf = new SaveFileDialog();
        public virtual void contextSave_Click(object sender, EventArgs e)
        {
            sf.Filter = "Bitmap File (*.bmp)|*.bmp|JPEG File (*.jpg)|*.jpg";
            if (sf.ShowDialog() == DialogResult.OK)
            {
                this.Image.Save(sf.FileName);   
            }
        }

        public void GetPosition()
        {
            plot.GetPositionX(NOP, MonitorProperty);
        }

        #region [ Property ] Image - Export
        public Image Image
        {
            get
            {
                Bitmap bp = new Bitmap(this.Width, this.Height);
                Graphics bg = Graphics.FromImage(bp);
                bg.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                bg.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

                OnDraw(bg);
                return bp;
            }
        }
        #endregion

        int markernumber = 1;
        int[] markerindex = new int[0];
        #region [ Function ] OnDraw
        private void OnDraw(Graphics g)
        {
            MonitorProperty.xMax = MonitorProperty.xMax;
            plot.posx = plot.GetPositionX(NOP, MonitorProperty);

            plot.DrawBackground(g, MonitorProperty);

            if (!WholeTrace)
            {
                if (dataSourceA != null && dataSourceA.Length > 1)
                {
                    pathA = plot.AddTrace(g, dataSourceA, Color.FromArgb(MonitorProperty.Alpha, MonitorProperty.TraceColor), MonitorProperty, SubStart, SubStop);

                    if (DisableMarker) return;
                    for (int i = 0; i < markerindex.Length; i++)
                        if (markerindex[i] >= dataSourceA.Length)
                            markerindex[i] = dataSourceA.Length - 1;
                    plot.AddMarker(g, pathA, dataSourceA, markerindex, MonitorProperty);

                    return;
                }

                if (dataSourceB != null && dataSourceB.Count > 1)
                {
                    pathA = plot.AddTrace(g, dataSourceB, Color.FromArgb(MonitorProperty.Alpha, MonitorProperty.TraceColor), MonitorProperty, SubStart, SubStop);

                    if (DisableMarker) return;
                    for (int i = 0; i < markerindex.Length; i++)
                        if (markerindex[i] >= dataSourceB.Count)
                            markerindex[i] = dataSourceB.Count - 1;
                    plot.AddMarker(g, pathA, dataSourceB, markerindex, MonitorProperty);

                    return;
                }
            }

            else
            {
                if (dataSourceA != null && dataSourceA.Length > 1)
                {
                    pathA = plot.AddTrace(g, dataSourceA, Color.FromArgb(MonitorProperty.Alpha, MonitorProperty.TraceColor), MonitorProperty);

                    if (DisableMarker) return;
                    for (int i = 0; i < markerindex.Length; i++)
                        if (markerindex[i] >= dataSourceA.Length)
                            markerindex[i] = dataSourceA.Length - 1;
                    plot.AddMarker(g, pathA, dataSourceA, markerindex, MonitorProperty);

                    return;
                }

                if (dataSourceB != null && dataSourceB.Count > 1)
                {
                    pathA = plot.AddTrace(g, dataSourceB, Color.FromArgb(MonitorProperty.Alpha, MonitorProperty.TraceColor), MonitorProperty);

                    if (DisableMarker) return;
                    for (int i = 0; i < markerindex.Length; i++)
                        if (markerindex[i] >= dataSourceB.Count)
                            markerindex[i] = dataSourceB.Count - 1;
                    plot.AddMarker(g, pathA, dataSourceB, markerindex, MonitorProperty);

                    return;
                }
            }

            
        }
        #endregion

        #region [ Function ] Double Buffered Painting --- Do Not Modify Here
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            BufferedGraphicsContext bgc = BufferedGraphicsManager.Current;
            BufferedGraphics bg = bgc.Allocate(e.Graphics, new Rectangle(0, 0, ClientSize.Width, ClientSize.Height));

            OnDraw(bg.Graphics);

            bg.Render(e.Graphics);
            bg.Dispose();
            bgc.Dispose();
        }
        #endregion
    }

    #region [ Class ] wsPlotViewProperty
    [TypeConverter(typeof(wsPlotViewPropertyTypeConverter)), Description("wsPlotViewProperty"), DisplayName("wsPlotViewProperty")]
    public class wsPlotViewProperty
    {
        #region [ Property ] Display: PlotProperty, NOP, EnablePlot, EnableMarker
        [Category("Display"), Description("Plot"), DisplayName("Plot")]
        public wsPlotProperty PlotProperty { get; set; }

        [Category("Display"), Description("Trace Points"), DisplayName("Trace Points")]
        public int NOP { get; set; }

        [Category("Display"), Description("Trace Points"), DisplayName("Trace Points")]
        public bool EnablePlot { get; set; }

        [Category("Display"), Description("Trace Points"), DisplayName("Trace Points")]
        public bool EnableMarker { get; set; }

        #endregion

        public wsPlotViewProperty()
        {
            PlotProperty = new wsPlotProperty();
            NOP = 201;
            PlotProperty.Refer = 0;

            EnablePlot = true;
            EnableMarker = false;
        }

        public void FromStringX(StringX value)
        {

        }

        public StringX ToStringX()
        {
            StringX sx = new StringX();
            sx = PlotProperty.ToStringX();

            return sx;
        }
    }

    public class wsPlotViewPropertyTypeConverter : ExpandableObjectConverter
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if(destinationType == typeof(wsPlotViewProperty))
                return true;
            return base.CanConvertTo(context, destinationType);
        }

        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if(sourceType==typeof(StringX))
                return true;
            return base.CanConvertFrom(context, sourceType);
        }


    }
   
    #endregion
}

發佈了58 篇原創文章 · 獲贊 6 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章