www.pudn.com > LinePanel.rar > LinePanel.cs


using System; 
using System.Collections; 
using System.ComponentModel; 
using System.Drawing; 
using System.Data; 
using System.Windows.Forms; 
 
namespace LinePanel 
{ 
	///  
	/// UserControl1 的摘要说明。 
	///  
	public class LinePanel : System.Windows.Forms.UserControl 
	{ 
		///  
		/// 必需的设计器变量。 
		///  
		private System.ComponentModel.Container components = null; 
 
		public LinePanel() 
		{ 
			this.Name = "LinePanel"; 
			this.backGroundImage = new Bitmap(this.Size.Width,this.Size.Height); 
			this.backGraphic = Graphics.FromImage(this.backGroundImage); 
			this.backGraphic.PageUnit = GraphicsUnit.Pixel; 
			this.backGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 
			this.backGraphic.Clear(this.BackColor); 
			this.gridColor = Color.Black; 
			this.lineColor = Color.Blue; 
			this.values = new Queue(); 
			for(int i = 0; i < this.stepCount + 1; i++) 
			{ 
				this.values.Enqueue(1.0); 
			} 
			 
			 
			 
			// 该调用是 Windows.Forms 窗体设计器所必需的。 
			InitializeComponent(); 
 
			this.RebuildImage();// TODO: 在 InitComponent 调用后添加任何初始化 
 
		} 
 
		///  
		/// 清理所有正在使用的资源。 
		///  
		protected override void Dispose( bool disposing ) 
		{ 
			if( disposing ) 
			{ 
				if( components != null ) 
					components.Dispose(); 
			} 
			base.Dispose( disposing ); 
		} 
 
		#region 组件设计器生成的代码 
		///  
		/// 设计器支持所需的方法 - 不要使用代码编辑器  
		/// 修改此方法的内容。 
		///  
		private void InitializeComponent() 
		{ 
			//  
			// UserControl1 
			// 
			this.BackColor = Color.White; 
			this.Size = new System.Drawing.Size(200, 100); 
    	} 
		#endregion 
 
		private int hGridCount = 5; 
		private int vGridCount = 5; 
		private int gridWidth = 1; 
		private int stepCount = 10; 
		private int lineWidth = 1; 
		private Bitmap backGroundImage; 
		private Graphics backGraphic; 
		private Queue values; 
		private Color gridColor; 
		private Color lineColor; 
		 
 
		public Color GridColor 
		{ 
			get 
			{ 
				return this.gridColor; 
			} 
			set 
			{ 
				this.gridColor = value; 
				this.RebuildImage(); 
			} 
		} 
 
		public int LineWidth 
		{ 
			get 
			{ 
				return this.lineWidth; 
			} 
			set 
			{ 
				this.lineWidth = value; 
				this.RebuildImage(); 
			} 
		} 
 
		public Color LineColor 
		{ 
			get 
			{ 
				return this.lineColor; 
			} 
			set 
			{ 
				this.lineColor = value; 
				this.RebuildImage(); 
			} 
		} 
 
		public int GridWidth 
		{ 
			get 
			{ 
				return this.gridWidth; 
			} 
			set 
			{ 
				this.gridWidth = value; 
				this.RebuildImage(); 
			} 
		} 
		public int HGridCount 
		{ 
			get 
			{ 
				return this.hGridCount; 
			} 
 
			set 
			{ 
				this.hGridCount = value; 
				this.RebuildImage(); 
			} 
		} 
		public int VGridCount 
		{ 
			get 
			{ 
				return this.vGridCount; 
			} 
			set 
			{ 
				this.vGridCount = value; 
				this.RebuildImage(); 
			} 
		} 
		public int StepCount 
		{ 
			get 
			{ 
				return this.stepCount; 
			} 
			set 
			{ 
				this.stepCount = value; 
				while(this.values.Count > this.stepCount + 1) 
				{ 
					this.values.Dequeue(); 
				} 
				while(this.values.Count < this.stepCount + 1) 
				{ 
					this.values.Enqueue(1.0); 
				} 
				this.RebuildImage(); 
			} 
		} 
		protected override void OnBackColorChanged(EventArgs e) 
		{ 
			base.OnBackColorChanged (e); 
			this.RebuildImage(); 
		} 
		protected override void OnResize(EventArgs e) 
		{ 
			base.OnResize (e); 
			this.RebuildImage(); 
		} 
		public void AddValue(float Value) 
		{ 
			if(Value <= 1 && Value >= 0) 
			{ 
				this.values.Enqueue(1-Value); 
				while(this.values.Count > this.stepCount + 1) 
				{ 
					this.values.Dequeue(); 
				} 
				this.RebuildImage(); 
			} 
			else 
			{ 
				throw(new Exception("Input value must between 0.0 and 1.0")); 
			} 
		} 
		public virtual void RebuildImage() 
		{ 
			try 
			{ 
				if(this.backGroundImage != null) 
				{ 
					this.backGroundImage.Dispose(); 
				} 
				if(this.backGraphic != null) 
				{ 
					this.backGraphic.Dispose(); 
				} 
			} 
			catch 
			{} 
			finally 
			{ 
				this.backGroundImage = new Bitmap(this.Size.Width,this.Size.Height); 
				this.backGraphic = Graphics.FromImage(this.backGroundImage); 
				this.backGraphic.PageUnit = GraphicsUnit.Pixel; 
				this.backGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 
				this.backGraphic.Clear(this.BackColor); 
			} 
			Pen gridPen = new Pen(this.gridColor,this.gridWidth); 
			for(int i = 1; i < this.hGridCount; i++) 
			{ 
				int x = i*this.backGroundImage.Width/this.hGridCount; 
				this.backGraphic.DrawLine(gridPen,x,0,x,this.backGroundImage.Height); 
			} 
			for(int j = 1; j < this.vGridCount; j++) 
			{ 
				int y = j*this.backGroundImage.Height/this.vGridCount; 
				this.backGraphic.DrawLine(gridPen,0,y,this.backGroundImage.Width,y); 
			} 
			object[] valueArray = this.values.ToArray(); 
			Pen linePen = new Pen(this.lineColor,this.lineWidth); 
			for(int k = 0; k < this.stepCount; k++) 
			{ 
				float x1 = float.Parse(valueArray[k].ToString()); 
				float x2 = float.Parse(valueArray[k+1].ToString()); 
				this.backGraphic.DrawLine(linePen,k*this.backGroundImage.Width/this.stepCount,x1*this.backGroundImage.Height,(k+1)*this.backGroundImage.Width/this.stepCount,x2*this.backGroundImage.Height); 
			} 
			this.BackgroundImage = this.backGroundImage; 
			//this.Refresh(); 
		} 
		protected override void OnPaint(PaintEventArgs e) 
		{ 
			base.OnPaint (e); 
		} 
 
	} 
}