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

📄 customcolordlg.cs

📁 c#编写的仿OUTLOOK工具条的Winform菜单
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Diagnostics;
using System.Resources;
using System.Reflection;
using UtilityLibrary.WinControls;
using UtilityLibrary.General;

namespace UtilityLibrary.WinControls
{
	/// <summary>
	/// Summary description for CustomColorDlg.
	/// </summary>
	public class CustomColorDlg : System.Windows.Forms.Form
	{
		#region Class Variables
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label Label1;
		private Color currentColor = Color.White;
		private System.Windows.Forms.Panel currentColorPanel;
		private System.Windows.Forms.PictureBox paletteBox;
		private NumericTextBox hueEdit;
		private NumericTextBox satEdit;
		private NumericTextBox lumEdit;
		private NumericTextBox redEdit;
		private NumericTextBox greenEdit;
		private NumericTextBox blueEdit;
		private System.Windows.Forms.PictureBox lumBox;
		private Color paletteColor;
		private bool drawCrossHair = true;
		private Point crossPos = new Point(0, 0);
		private float DELTA_WIDTH = 240.0f/200.0f;
		private float DELTA_HEIGHT = 240.0f/186.0f;
		private bool updatingUI = false;
		private ResourceManager rm = null;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		#endregion

		#region Constructors
		public CustomColorDlg()
		{
			
			Assembly thisAssembly = Assembly.GetAssembly(Type.GetType("UtilityLibrary.WinControls.CustomColorDlg"));
			rm = new ResourceManager("UtilityLibrary.Resources.ImagesColorPicker", thisAssembly);
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			paletteBox.Image = (Bitmap)rm.GetObject("Palette");
		}

				
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#endregion

		#region Properties
		public Color CurrentColor
		{
			get 
			{
				return currentColor;
			}
			set
			{
				currentColor = value;
			}
		}

		#endregion

		#region Implementation
		void CustomColorDlg_Load(object sender, System.EventArgs e)
		{
			updatingUI = true;
			currentColorPanel.BackColor = CurrentColor;
			float r = CurrentColor.R;
			float g = CurrentColor.G;
			float b = CurrentColor.B;
			redEdit.Text = (Convert.ToInt32(r)).ToString();
			greenEdit.Text = (Convert.ToInt32(g)).ToString();
			blueEdit.Text = (Convert.ToInt32(b)).ToString();
			
			float h = 0;
			float s = 0;
			float l = 0;
			
			// Get h,s,l
			ColorUtil.RGBToHSL( (int)r, (int)g, (int)b, ref h, ref s, ref l);
			hueEdit.Text = Convert.ToString((int)h);
			satEdit.Text = Convert.ToString((int)s);
			lumEdit.Text = Convert.ToString((int)l);

			// Get palette color for interpolating the luminosity picture box
			ColorUtil.HSLToRGB( h, s, 120, ref r, ref g, ref b);
			paletteColor = Color.FromArgb((int)r, (int)g, (int)b);
			lumBox.Invalidate();
				
			// Calculate coordinates using default values for Hue and Sat
			CalculatePaletteMarkerCoordinates((int)h, (int)s);
			updatingUI = false;
		
		}

		void LumBox_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
			Rectangle r = new Rectangle(0, 5 , lumBox.Width - 10, lumBox.Height-10);
			
					
			// Draw the color gradient
			LinearGradientBrush b = new LinearGradientBrush(r,Color.White, CurrentColor, 
				System.Drawing.Drawing2D.LinearGradientMode.Vertical);
			ColorBlend cb = new ColorBlend(3);
			cb.Colors[0] = Color.White;
			cb.Colors[1] = paletteColor;
			cb.Colors[2] = Color.Black;
			cb.Positions[0] = 0f;
			cb.Positions[1] = 0.5f;
			cb.Positions[2] = 1.0f;
			
			b.InterpolationColors = cb;
			e.Graphics.FillRectangle(b,r);
			b.Dispose();

			// Draw border around gradient rectangle
			Rectangle borderRect = r;
			borderRect.Inflate(0, 0);
			e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black), 1), borderRect);
			
			// Draw the indicator
			Point[] pts = new Point[3];
			int iTop = 240 - Convert.ToInt32(lumEdit.Text);
			iTop = ((lumBox.Height - 10) * iTop )/240 + 4;
			
			// Make sure we don't go out of bounds
			if ( iTop > lumBox.Height - 5 ) iTop = lumBox.Height - 5;
			if ( iTop < 5 ) iTop = 5;
			pts[0] = new Point(lumBox.Width - 8, iTop);
			pts[1] = new Point(lumBox.Width - 2, iTop - 7);
			pts[2] = new Point(lumBox.Width - 2, iTop + 7);
			e.Graphics.FillPolygon(Brushes.Black, pts);
		
		}

		void lumBox_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if ( lumBox.Capture == true )
			{
					int y  = e.Y;
				if ( y < 5 ) y = 5;
				if ( y > lumBox.Height - 5 ) y = lumBox.Height - 5;
				int lum = 240 - ((y-5) * 240)/(lumBox.Height-10);
				lumEdit.Text = lum.ToString();
				LuminosityChanged();
			}
		}

		void lumBox_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			lumBox.Capture = false;
			LuminosityChanged();
		}

		void lumBox_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			lumBox.Capture = true;
			int y  = e.Y;
			if ( y < 5 ) y = 5;
			if ( y > lumBox.Height - 5 ) y = lumBox.Height - 5;
			int lum = 240 - ((y-5) * 240)/(lumBox.Height-10);
			lumEdit.Text = lum.ToString();
			LuminosityChanged();
		}

		void LuminosityChanged()
		{
			if ( lumEdit.Text.Length == 0) 
				return;
			
			updatingUI = true;
			float h = (float)Convert.ToDouble(hueEdit.Text);
			float s = (float)Convert.ToDouble(satEdit.Text);
			float l = (float)Convert.ToDouble(lumEdit.Text);
			ConvertToRGB(h, s, l);
			updatingUI = false;
			
		}

		void HueSatChanged()
		{
			// Calculate new Hue and Sat bases on mouse position coordinates
			updatingUI = true;
			float h = crossPos.X * DELTA_WIDTH;
			float s = (240.0f -(crossPos.Y * DELTA_HEIGHT));
			float l = (float)Convert.ToDouble(lumEdit.Text);

			hueEdit.Text = Convert.ToString((int)h);
			satEdit.Text = Convert.ToString((int)s);
           	ConvertToRGB(h, s, l);
			updatingUI = false;

		}

     	void HueSatTextBoxChanged()
		{
			// Calculate palette box marker coordinates positon base on
			// the new hue or sat value
			if ( hueEdit.Text == "" || satEdit.Text == "" )
				return;
			
			updatingUI = true;
			float h = (float)Convert.ToDouble(hueEdit.Text);
			float s = (float)Convert.ToDouble(satEdit.Text);
			float l = (float)Convert.ToDouble(lumEdit.Text);
			CalculatePaletteMarkerCoordinates((int)h, (int)s);
			ConvertToRGB(h, s, l);
			updatingUI = false;
                      
			
		
		}

		void RGBTextChanged()
		{
			// Make sure none of the text boxes is empty
			if ( redEdit.Text == "" || greenEdit.Text == "" || blueEdit.Text == "" )
				return;
			
			updatingUI = true;
			float r = (float)Convert.ToDouble(redEdit.Text);
			float g = (float)Convert.ToDouble(greenEdit.Text);
			float b = (float)Convert.ToDouble(blueEdit.Text);

			float h = 0.0f;
			float s = 0.0f;
			float l = 0.0f;
			
			// convert to h, s, and l
			ColorUtil.RGBToHSL((int)r, (int)g, (int)b, ref h, ref s, ref l);
			// Update text boxes
			hueEdit.Text = Convert.ToString((int)h);
			satEdit.Text = Convert.ToString((int)s);
			lumEdit.Text = Convert.ToString((int)l);
			// Update current color
			CurrentColor = Color.FromArgb((int)r, (int)g, (int)b);
			currentColorPanel.BackColor = CurrentColor;

			// Update palette color (luminosity box)
			ColorUtil.HSLToRGB( h, s, 120, ref r, ref g, ref b);
			paletteColor = Color.FromArgb((int)r, (int)g, (int)b);
			lumBox.Invalidate();

			// Update palette box
			CalculatePaletteMarkerCoordinates((int)h, (int)s);
			updatingUI = false;

		}

		void ConvertToRGB(float h, float s, float l)
		{
			// Now get the new RGB values
			float r = 0.0f;
			float g = 0.0f;
			float b = 0.0f;
			ColorUtil.HSLToRGB(h, s, l, ref r, ref g, ref b);
			CurrentColor = Color.FromArgb((int)r, (int)g, (int)b);
			redEdit.Text = (Convert.ToInt32(r)).ToString();
			greenEdit.Text = (Convert.ToInt32(g)).ToString();
			blueEdit.Text = (Convert.ToInt32(b)).ToString();
			currentColorPanel.BackColor = CurrentColor;

			// Palette color is the pure form of the hue/sat
			ColorUtil.HSLToRGB( h, s, 120, ref r, ref g, ref b);
			paletteColor = Color.FromArgb((int)r, (int)g, (int)b);
			lumBox.Invalidate();
		}

		void CalculatePaletteMarkerCoordinates( int Hue, int Sat)
		{
			crossPos.X = (int)(Hue/DELTA_WIDTH);
			crossPos.Y = (int)((240 - Sat)/DELTA_HEIGHT);
			paletteBox.Invalidate();

		}

		void paletteBox_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			
			if ( paletteBox.Capture != true )
				return;
			
			crossPos.X = e.X;
			crossPos.Y = e.Y;
			
			// Make sure mouse does not go out of the palette picture area
			Rectangle rc = paletteBox.ClientRectangle;
			bool updateMousePos = false;
			if ( e.X <= rc.Left ) 
			{
				updateMousePos = true;
				crossPos.X = rc.Left;
			}
			if ( e.X >= rc.Right)
			{   updateMousePos = true;
				crossPos.X = rc.Right;
			}
			if ( e.Y <= rc.Top )
			{

⌨️ 快捷键说明

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