📄 colorpicker.cs
字号:
}
// Size children controls
Rectangle rc = ClientRectangle;
int width = rc.Width;
int height = rc.Height;
// ColorPanel
displayColorRect = new Rectangle(rc.Left + 4, rc.Top + 4, COLOR_PANEL_WIDTH, height - 8);
// TextBox
colorTextBox.Left = displayColorRect.Right + 4;
colorTextBox.Top = rc.Top + MARGIN/2;
colorTextBox.Width = rc.Width - COLOR_PANEL_WIDTH - ARROW_BUTTON_WIDTH - 10;
colorTextBox.Height = height - MARGIN;
// ArrowButton
arrowButton.Left = colorTextBox.Right + 2;
arrowButton.Top = rc.Top;
arrowButton.Width = ARROW_BUTTON_WIDTH;
arrowButton.Height = height;
}
#endregion
#region Properties
internal DrawState DrawState
{
set
{
if ( drawState != value)
{
drawState = value;
Invalidate();
}
}
get { return drawState; }
}
public Color Color
{
get { return currentColor; }
set { currentColor = value; }
}
#endregion
#region Implementation
void ColorTextBox_LostFocus(object sender, EventArgs e)
{
if ( !arrowButton.ContainsFocus )
DrawState = DrawState.Normal;
}
void ColorTextBox_GotFocus(object sender, EventArgs e)
{
DrawState = DrawState.Hot;
}
void ColorTextBox_MouseEnter(object sender, EventArgs e)
{
DrawState = DrawState.Hot;
}
void ColorPanel_MouseEnter(object sender, EventArgs e)
{
DrawState = DrawState.Hot;
}
void ColorTextBox_MouseLeave(object sender, EventArgs e)
{
Point pos = Control.MousePosition;
if ( !ClientRectangle.Contains(PointToClient(pos)) )
DrawState = DrawState.Normal;
}
void ColorPanel_MouseLeave(object sender, EventArgs e)
{
Point pos = Control.MousePosition;
if ( !ClientRectangle.Contains(PointToClient(pos)) )
{
DrawState = DrawState.Normal;
Debug.WriteLine("COLOR PANEL ON MOUSE LEAVE...");
}
}
void OnNewColor(NewColorArgs e)
{
currentColor = e.NewColor;
if ( NewColor != null )
NewColor(this, e);
}
void SetupChildrenControls()
{
// Associate arrow bitmap with button
Bitmap bm = (Bitmap)rm.GetObject("Arrow");
bm.MakeTransparent(Color.White);
arrowButton.Image = bm;
// Initialize color panel to default color
currentColor = Color;
// Create color picker dropdown portion
colorDropDown = new ColorPickerDropDown();
colorDropDown.ColorChanged += new ColorChangeEventHandler(ColorChanged);
// Get LostFocus events from text box
colorTextBox.LostFocus += new EventHandler(ColorTextBox_LostFocus);
colorTextBox.GotFocus += new EventHandler(ColorTextBox_GotFocus);
colorTextBox.MouseLeave += new EventHandler(ColorTextBox_MouseLeave);
colorTextBox.MouseEnter += new EventHandler(ColorTextBox_MouseEnter);
// Change colorTextBox font to bold
colorTextBox.Font = new Font(colorTextBox.Font, colorTextBox.Font.Style | FontStyle.Bold);
// See if we have a known color, or just format the rgb values
InitializeColorTextBoxString(Color, null);
}
void InitializeColorTextBoxString(Color color, string sender)
{
bool useTransparent = false;
Color knownColor = Color.Empty;
bool bKnownColor = true;
if ( sender == "CustomTab" || sender == null )
bKnownColor = ColorUtil.IsKnownColor(color, ref knownColor, useTransparent);
else
knownColor = color;
if ( bKnownColor )
{
color = knownColor;
// update color in dropdown part
colorTextBox.Text = color.Name;
}
else
{ // Format rgb string
string sep = ",";
string rgb = color.R.ToString() + sep + color.G.ToString() + sep + color.B.ToString();
colorTextBox.Text = rgb;
}
colorDropDown.CurrentColor = color;
colorTextBox.SelectionLength = 0;
colorTextBox.SelectionStart = colorTextBox.Text.Length + 1;
// Fire event to whoever is listening
if ( sender != null )
{
NewColorArgs nca = new NewColorArgs(color);
OnNewColor(nca);
}
}
void arrowButton_Click(object sender, System.EventArgs e)
{
// Show color picker dropdown control
Point point = new Point(0,0);
CalculateSafeDisplayPoint(ref point);
colorDropDown.DesktopBounds = new Rectangle(point.X, point.Y,
colorDropDown.ClientRectangle.Width, colorDropDown.ClientRectangle.Height);
if ( !colorDropDown.Visible )
colorDropDown.Show();
else
{
colorDropDown.Visible = false;
}
}
void CalculateSafeDisplayPoint(ref Point point)
{
Rectangle rc = ClientRectangle;
rc = RectangleToScreen(rc);
int screenWidth = Screen.PrimaryScreen.Bounds.Width;
int screenHeight = Screen.PrimaryScreen.Bounds.Height;
// Correct x coordinate if necessary
point.X = rc.Right - colorDropDown.Width;
if ( point.X < 0 )
point.X = 0;
else if ( point.X + colorDropDown.Width > screenWidth )
point.X = screenWidth - colorDropDown.Width;
// Correct y coordinate if necessary
point.Y = rc.Bottom+1;
if ( point.Y < 0 )
point.Y = 0;
else if ( point.Y + colorDropDown.Height > screenHeight )
point.Y = rc.Top -1 - colorDropDown.Height;
}
void ColorChanged( object sender, ColorChangeArgs ea)
{
currentColor = ea.NewColor;
InitializeColorTextBoxString(ea.NewColor, ea.SenderName);
}
void ColorPicker_Load(object sender, System.EventArgs e)
{
SetupChildrenControls();
}
void colorTextBox_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
// If not the enter key then do not process
if ( e.KeyCode != Keys.Enter)
return;
// Check if the string contains commas -- that would be the case
// if we have a "custom" color
string text = colorTextBox.Text;
if ( text.IndexOf(',') != -1 )
{
FormatRGB(text);
return;
}
// If it is in hexadecimal format
try
{
if ( text.IndexOf('x') != -1 || text.IndexOf('X') != -1 )
{
int newValue = Convert.ToInt32(text, 16);
int Red = (0xFF0000 & newValue)>>16;
int Green = (0x00FF00 & newValue)>>8;
int Blue = 0x0000FF & newValue;
Color hexColor = Color.FromArgb(Red, Green, Blue);
InitializeColorTextBoxString(hexColor, "CustomTab");
return;
}
// If there are not commnas
// then check if we have either a valid number
int colorValue = Convert.ToInt32(text);
if ( colorValue >= 0 && colorValue <= 255 )
{
string formattedValue = "0, 0," + colorValue.ToString();
FormatRGB(formattedValue);
return;
}
}
catch ( Exception )
{
// Could not figure out what the string is
ResetTextBox("Invalid Color Value");
return;
}
// If we get here maybe we have a web or systemcolor
Color currentColor = Color.FromName(text);
if ( !(currentColor.A == 0 && currentColor.R == 0
&& currentColor.G == 0 && currentColor.B == 0) )
{
// reset color in text box
InitializeColorTextBoxString(currentColor, "CustomTab");
return;
}
// Could not figure out what the string is
ResetTextBox("Invalid Color Value");
}
void FormatRGB(string text)
{
string[] RGBs = text.Split(',');
if ( RGBs.Length != 3 )
{
// If we don't have three pieces of information, then the
// string is not properly formatted, inform the use
ResetTextBox("Invalid color value");
return;
}
string stringR = RGBs[0];
string stringG = RGBs[1];
string stringB = RGBs[2];
int R, G, B;
try
{
R = Convert.ToInt32(stringR);
G = Convert.ToInt32(stringG);
B = Convert.ToInt32(stringB);
if ( ( R < 0 || R > 255 ) || ( G < 0 || G > 255 ) || ( B < 0 || B > 255 ) )
{
ResetTextBox("Out of bounds RGB value");
return;
}
else
{
// Convert to color
Color color = Color.FromArgb(R, G, B);
// See if we have either a web color or a system color
Color knownColor = Color.Empty;
bool isKnown = ColorUtil.IsKnownColor( color, ref knownColor, true);
if ( !isKnown )
isKnown = ColorUtil.IsSystemColor(color, ref knownColor);
if ( isKnown )
color = knownColor;
// reset color in text box
currentColor = color;
InitializeColorTextBoxString(color, "CustomTab");
}
}
catch ( InvalidCastException )
{
ResetTextBox("Invalid RGB value");
return;
}
}
void ResetTextBox(string ErrorMessage)
{
MessageBox.Show(this, ErrorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
// reset to old value
InitializeColorTextBoxString(Color, null);
}
void DrawColorPickerState()
{
using (Graphics g = CreateGraphics())
{
Rectangle rc = ClientRectangle;
if ( drawState != DrawState.Disable )
{
using ( Brush b = new SolidBrush(currentColor) )
{
g.FillRectangle(b, displayColorRect);
g.DrawRectangle(Pens.Black, displayColorRect.Left,
displayColorRect.Top, displayColorRect.Width-1, displayColorRect.Height-1);
}
}
if ( drawState == DrawState.Normal )
{
// Make sure arrow button look in its
// normal state too
arrowButton.DrawState = DrawState.Normal;
// Draw white border
using ( Pen p = new Pen(Color.White) )
{
g.DrawRectangle(p, rc.Left, rc.Top, rc.Width-1, rc.Height-1);
}
}
else if ( drawState == DrawState.Hot )
{
if ( !(previousState == DrawState.Pressed &&
colorDropDown.Visible == true) )
{
// Make sure arrow button look in its
// normal state too
arrowButton.DrawState = DrawState.Hot;
}
g.DrawRectangle(new Pen(ColorUtil.VSNetBorderColor), rc.Left, rc.Top, rc.Width-1, rc.Height-1);
}
else if ( drawState == DrawState.Pressed )
{
// Make sure arrow button look in its
// normal state too
arrowButton.DrawState = DrawState.Pressed;
g.DrawRectangle(new Pen(ColorUtil.VSNetBorderColor), rc.Left, rc.Top, rc.Width-1, rc.Height-1);
}
else if ( drawState == DrawState.Disable )
{
arrowButton.DrawState = DrawState.Disable;
g.DrawRectangle(SystemPens.ControlDark, displayColorRect.Left,
displayColorRect.Top, displayColorRect.Width-1, displayColorRect.Height-1);
using ( Pen p = new Pen(Color.White) )
{
g.DrawRectangle(p, rc.Left, rc.Top, rc.Width-1, rc.Height-1);
}
}
}
previousState = drawState;
}
#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()
{
this.colorTextBox = new System.Windows.Forms.TextBox();
this.arrowButton = new ArrowButton(this);
this.SuspendLayout();
//
// colorTextBox
//
this.colorTextBox.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.colorTextBox.Location = new System.Drawing.Point(38, 4);
this.colorTextBox.Name = "colorTextBox";
this.colorTextBox.Size = new System.Drawing.Size(122, 13);
this.colorTextBox.TabIndex = 0;
this.colorTextBox.Text = "textBox1";
this.colorTextBox.KeyUp += new System.Windows.Forms.KeyEventHandler(this.colorTextBox_KeyUp);
//
// arrowButton
//
this.arrowButton.BackColor = System.Drawing.SystemColors.Control;
this.arrowButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.arrowButton.Location = new System.Drawing.Point(166, 4);
this.arrowButton.Name = "arrowButton";
this.arrowButton.Size = new System.Drawing.Size(18, 10);
this.arrowButton.TabIndex = 1;
this.arrowButton.Click += new System.EventHandler(this.arrowButton_Click);
//
// ColorPicker
//
this.BackColor = System.Drawing.Color.White;
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.arrowButton,
this.colorTextBox});
this.Name = "ColorPicker";
this.Size = new System.Drawing.Size(190, 24);
this.Load += new System.EventHandler(this.ColorPicker_Load);
this.ResumeLayout(false);
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -