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

📄 performancegraph.cs

📁 rudp可靠保障得udp传输
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Windows.Forms;
using System.Text;


namespace Test.Helper.Windows
{

	#region LineHandle

	public class LineHandle
	{
		private Line m_Line = null;
		private PerformanceGraph m_Owner = null;

		public LineHandle(ref Object line, PerformanceGraph owner)
		{
			// A small hack to get around the compiler error CS0051
			if (string.Compare(line.GetType().Name, "Line") != 0)
			{
				throw new System.ArithmeticException(
					"LineHandle: First Parameter must be " +
					"type of 'Line' cast to base 'Object'");
			}

			m_Line = (Line)line;
			m_Owner = owner;
		}

		/// <summary> 
		/// Clears any currently displayed magnitudes.
		/// </summary>
		public void Clear()
		{
			m_Line.m_MagnitudeList.Clear();
			m_Owner.UpdateGraph();
		}

		#region Properties

		/// <summary> 
		/// Sets or gets the line's current color.
		/// </summary>
		public Color Color
		{
			set
			{
				if (m_Line.m_Color != value)
				{
					m_Line.m_Color = value;
					m_Owner.Refresh();
				}
			}
			get
			{
				return m_Line.m_Color;
			}

		}

		/// <summary> 
		/// Sets or gets the line's thickness in pixels. NOTE: It is advisable
		/// to set HighQuality to false if using a thickness greater than
		/// 2 pixels as the antialiasing creates imperfections.
		/// </summary>
		public uint Thickness
		{
			set
			{
				if (m_Line.m_Thickness != value)
				{
					m_Line.m_Thickness = value;
					m_Owner.Refresh();
				}
			}
			get
			{
				return m_Line.m_Thickness;
			}
		}

		/// <summary> 
		/// Gets or sets a value indicating whether the line is visible.
		/// </summary>
		public bool Visible
		{
			set
			{
				if (m_Line.m_bVisible != value)
				{
					m_Line.m_bVisible = value;
					m_Owner.Refresh();
				}
			}
			get
			{
				return m_Line.m_bVisible;
			}
		}

		/// <summary> 
		/// Gets or sets a value indicating whether this line's magnitudes are
		/// displayed in a bar graph style.
		/// </summary>
		public bool ShowAsBar
		{
			set
			{
				if (m_Line.m_bShowAsBar != value)
				{
					m_Line.m_bShowAsBar = value;
					m_Owner.Refresh();
				}
			}
			get
			{
				return m_Line.m_bShowAsBar;
			}
		}

		#endregion
	}

	#endregion

	#region Line

	internal class Line
	{
		public List<int> m_MagnitudeList = new List<int>();
		public Color m_Color = Color.Green;
		public string m_NameID = "";
		public int m_NumID = -1;
		public uint m_Thickness = 1;
		public bool m_bShowAsBar = false;
		public bool m_bVisible = true;

		public Line(string name)
		{
			m_NameID = name;
		}

		public Line(int num)
		{
			m_NumID = num;
		}
	}

	#endregion

	sealed public class PerformanceGraph : Control
	{

		#region Variables

		private Color m_TextColor = Color.Yellow;
		private Color m_GridColor = Color.Green;
		private string m_MaxLabel = "Max";
		private string m_MinLabel = "Minimum";
		private bool m_bHighQuality = true;
		private bool m_bAutoScale = false;
		private bool m_bMinLabelSet = false;
		private bool m_bMaxLabelSet = false;
		private bool m_bShowMinMax = true;
		private bool m_bShowGrid = true;
		private int m_MoveOffset = 0;
		private int m_MaxCoords = -1;
		private int m_LineInterval = 5;
		private int m_MaxPeek = 100;
		private int m_MinPeek = 0;
		private int m_GridSize = 15;
		private int m_OffsetX = 0;

		private List<Line> m_Lines = new List<Line>();
		private System.ComponentModel.IContainer components = null;

		#endregion

		#region Constructor

		public PerformanceGraph()
		{
			InitializeComponent();
			InitializeStyles();
		}

		public PerformanceGraph(Form Parent)
		{
			Parent.Controls.Add(this);

			InitializeComponent();
			InitializeStyles();
		}

		public PerformanceGraph(Form parent, Rectangle rectPos)
		{
			parent.Controls.Add(this);

			Location = rectPos.Location;
			Height = rectPos.Height;
			Width = rectPos.Width;

			InitializeComponent();
			InitializeStyles();
		}


		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#endregion

		#region Component Designer generated code

		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>

		private void InitializeComponent()
		{
			components = new System.ComponentModel.Container();
		}

		#endregion

		#region InitializeStyles

		private void InitializeStyles()
		{
			BackColor = Color.Black;

			/* Enable double buffering and similiar techniques to 
			 * eliminate flicker */

			SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
			SetStyle(ControlStyles.UserPaint, true);
			SetStyle(ControlStyles.AllPaintingInWmPaint, true);
			DoubleBuffered = true;

			SetStyle(ControlStyles.ResizeRedraw, true);
		}

		#endregion

		#region Properties

		/// <summary> 
		/// Gets or sets the color of any text displayed in the graph (labels).
		/// </summary>
		public Color TextColor
		{
			set
			{
				if (m_TextColor != value)
				{
					m_TextColor = value;
					Refresh();
				}
			}
			get
			{
				return m_TextColor;
			}
		}

		/// <summary> 
		/// Gets or sets the graph's grid color.
		/// </summary>
		public Color GridColor
		{
			set
			{
				if (m_GridColor != value)
				{
					m_GridColor = value;
					Refresh();
				}
			}
			get
			{
				return m_GridColor;
			}
		}

		/// <summary> 
		/// Gets or sets the number of pixels between each displayed magnitude.        
		/// </summary>
		public ushort LineInterval
		{
			set
			{
				if ((ushort)m_LineInterval != value)
				{
					m_LineInterval = (int)value;
					m_MaxCoords = -1; // Recalculate
					Refresh();
				}
			}
			get
			{
				return (ushort)m_LineInterval;
			}
		}

		/// <summary> 
		/// Gets or sets the string to display as the graph's 'maximum label'.
		/// </summary>
		public string MaxLabel
		{
			set
			{
				m_bMaxLabelSet = true;

				if (string.Compare(m_MaxLabel, value) != 0)
				{
					m_MaxLabel = value;
					m_MaxCoords = -1; // Recalculate
					Refresh();
				}
			}
			get
			{
				return m_MaxLabel;
			}
		}

		/// <summary> 
		/// Gets or sets the string to display as the graph's 'minimum label'.
		/// </summary>
		public string MinLabel
		{
			set
			{
				m_bMinLabelSet = true;

				if (string.Compare(m_MinLabel, value) != 0)
				{
					m_MinLabel = value;
					m_MaxCoords = -1; // Recalculate
					Refresh();
				}
			}
			get
			{
				return m_MinLabel;
			}
		}

		/// <summary> 
		/// Gets or sets the width/height (in pixels) of each square in
		/// the graph's grid.
		/// </summary>
		public ushort GridSize
		{
			set
			{
				if (m_GridSize != (int)value)
				{
					m_GridSize = (int)value;
					Refresh();

				}
			}
			get
			{
				return (ushort)m_GridSize;
			}
		}

		/// <summary> 
		/// Gets or sets the maximum peek magnitude of the graph, which should be
		/// the largest value you could potentially push to the graph. NOTE: If you
		/// have set AutoScale to true, this value will automatically adjust to
		/// the highest magnitude pushed to the graph.
		/// </summary>
		public int MaxPeekMagnitude
		{
			set
			{
				m_MaxPeek = value;
				RefreshLabels();
			}
			get
			{
				return m_MaxPeek;
			}
		}

		/// <summary> 
		/// Gets or sets the minimum magnitude of the graph, which should be
		/// the smallest value you could potentially push to the graph.
		/// NOTE: If you have set AutoScale to true, this value will 
		/// automatically adjust to the lowest magnitude pushed to the graph.
		/// </summary>
		public int MinPeekMagnitude
		{
			set
			{
				m_MinPeek = value;
				RefreshLabels();
			}
			get
			{
				return m_MinPeek;
			}
		}

		/// <summary> 
		/// Gets or sets the value indicating whether the graph automatically
		/// adjusts MinPeekMagnitude and MaxPeekMagnitude to the lowest and highest
		/// values pushed to the graph.
		/// </summary>
		public bool AutoAdjustPeek
		{
			set
			{
				if (m_bAutoScale != value)
				{
					m_bAutoScale = value;
					Refresh();
				}
			}
			get
			{
				return m_bAutoScale;
			}
		}

		/// <summary> 
		/// Gets or sets the value indicating whether the graph is rendered in
		/// 'high quality' mode (with antialiasing). It is suggested that this property
		/// be set to false if you intend to display your graph using bar graph 
		/// styles, line thickness greater than two, or if maximum performance 
		/// is absolutely crucial.
		/// </summary>
		public bool HighQuality
		{
			set
			{
				if (value != m_bHighQuality)
				{
					m_bHighQuality = value;
					Refresh(); // Force redraw
				}
			}
			get
			{
				return m_bHighQuality;
			}
		}

		/// <summary> 
		/// Gets or sets the value indicating whether the mimimum and maximum labels
		/// are displayed.
		/// </summary>
		public bool ShowLabels
		{
			set
			{
				if (m_bShowMinMax != value)
				{
					m_bShowMinMax = value;

					/* We're going to need to recalculate our maximum 
					 * coordinates since our graphable width changed */
					m_MaxCoords = -1;

					Refresh();
				}
			}
			get
			{
				return m_bShowMinMax;
			}
		}

		/// <summary> 
		/// Gets or sets the value indicating whether the graph's grid is 
		/// displayed.
		/// </summary>
		public bool ShowGrid
		{
			set
			{
				if (m_bShowGrid != value)
				{
					m_bShowGrid = value;
					Refresh();
				}
			}
			get
			{
				return m_bShowGrid;
			}
		}

		#endregion

		#region OnSizeChanged

		protected override void OnSizeChanged(EventArgs e)
		{
			/* We're going to need to recalculate our maximum 
			 * coordinates since our graphable width changed */
			m_MaxCoords = -1;

			Refresh();

			base.OnSizeChanged(e);
		}

		#endregion

		#region OnPaint

⌨️ 快捷键说明

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