📄 colorpickerdropdown.cs
字号:
if ( dlg.ShowDialog(this) == DialogResult.OK )
{
customColors[index] = dlg.CurrentColor;
ColorChangeArgs ca = new ColorChangeArgs(dlg.CurrentColor, "CustomTab");
OnColorChanged(ca);
}
}
rightButtonDown = false;
return;
}
Graphics g = customColorsPage.CreateGraphics();
Color clickedColor = ColorUtil.ColorFromPoint(g, clientMousePos.X, clientMousePos.Y);
g.Dispose();
Color color = FindCustomColor(clientMousePos);
if ( color != Color.Empty )
{
ColorChangeArgs ca = new ColorChangeArgs(color, "CustomTab");
OnColorChanged(ca);
// Hide the dropdown
exitLoop = true;
Visible = false;
}
}
Color FindCustomColor(Point point)
{
Color color = Color.Empty;
for ( int i = 0 ; i < CUSTOM_COLORS_COUNT; i++)
{
if ( customColorsRects[i].Contains(point) )
{
color = customColors[i];
currentCustomColorIndex = i;
return color;
}
}
// Did not find the color
// Should only happen if the user click between colors
return color;
}
bool IsSpareColor(Point p, ref int index)
{
for ( int i = 0; i < CUSTOM_COLORS_HORIZ_ITEMS; i++ )
{
for ( int j = (CUSTOM_COLORS_VERT_ITEMS-2); j < CUSTOM_COLORS_VERT_ITEMS; j++ )
{
if ( customColorsRects[i*8 + j].Contains(p) )
{
index = i*8 + j;
currentCustomColorIndex = index;
return true;
}
}
}
return false;
}
void customColorsPage_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if ( e.Button == MouseButtons.Right )
{
rightButtonDown = true;
}
}
void tabControl_SelectedIndexChanged(object sender, System.EventArgs e)
{
int index = tabControl.SelectedIndex;
if ( index == 1 )
webColorsList.Focus();
else if ( index == 2)
systemColorsList.Focus();
}
void webColorsList_Click(object sender, System.EventArgs e)
{
Color color = Color.FromName(webColorsList.Text);
ColorChangeArgs ca = new ColorChangeArgs(color, "WebTab");
OnColorChanged(ca);
exitLoop = true;
Visible = false;
}
void systemColorsList_Click(object sender, System.EventArgs e)
{
Color color = Color.FromName(systemColorsList.Text);
currentColor = color;
ColorChangeArgs ca = new ColorChangeArgs(color, "SystemTab");
OnColorChanged(ca);
exitLoop = true;
Visible = false;
}
int GetCustomColorIndex(Color color)
{
for ( int i = 0; i < CUSTOM_COLORS_COUNT; i++ )
{
if ( customColors[i].Equals(color) )
return i;
}
// Should not happen
return -1;
}
bool GetSystemColorIndex(Color color, out int index)
{
index = -1;
string colorName = color.Name;
index = systemColorsList.FindStringExact(colorName);
if ( index != ListBox.NoMatches )
{
systemColorsList.SelectedIndex = index;
return true;
}
return false;
}
bool GetWebColorIndex(Color color, out int index)
{
index = -1;
string colorName = color.Name;
index = webColorsList.FindStringExact(colorName);
if ( index != ListBox.NoMatches )
{
webColorsList.SelectedIndex = index;
return true;
}
return false;
}
void ColorPickerDropDown_VisibleChanged(object sender, System.EventArgs e)
{
if ( !Visible )
return;
// Select tab base on current color index
int index;
bool isNamed = currentColor.IsNamedColor;
if ( !isNamed )
{
// It got to be a custom color
index = GetCustomColorIndex(currentColor);
currentCustomColorIndex = index;
tabControl.SelectedIndex = 0;
return;
}
bool isSystemColor = GetSystemColorIndex(currentColor, out index);
if ( isSystemColor )
{
tabControl.SelectedIndex = 2;
return;
}
bool isWebColor = GetWebColorIndex(currentColor, out index);
if ( isWebColor )
{
tabControl.SelectedIndex = 1;
}
}
bool IsChild(IntPtr hWnd)
{
// Consider a child the tab control itself
// the tab pages, or the list controls in the tab pages
if ( tabControl.Handle == hWnd )
return true;
else if ( webColorsList.Handle == hWnd )
return true;
else if ( systemColorsList.Handle == hWnd )
return true;
else if ( customColorsPage.Handle == hWnd )
return true;
else if ( webColorsPage.Handle == hWnd )
return true;
else if ( systemColorsPage.Handle == hWnd )
return true;
return false;
}
void StartPeekMessageLoop()
{
// Create an object for storing windows message information
Win32.MSG msg = new Win32.MSG();
bool leaveMsg = false;
// Process messages until exit condition recognised
while(!exitLoop)
{
// Suspend thread until a windows message has arrived
if (WindowsAPI.WaitMessage())
{
// Take a peek at the message details without removing from queue
while(!exitLoop && WindowsAPI.PeekMessage(ref msg, 0, 0, 0, (int)Win32.PeekMessageFlags.PM_NOREMOVE))
{
//Console.WriteLine("Track {0} {1}", this.Handle, ((Msg)msg.message).ToString());
//Console.WriteLine("Message is for {0 }", msg.hwnd);
// Mouse was pressed in a window of this application
if ((msg.message == (int)Msg.WM_LBUTTONDOWN) ||
(msg.message == (int)Msg.WM_MBUTTONDOWN) ||
(msg.message == (int)Msg.WM_RBUTTONDOWN) ||
(msg.message == (int)Msg.WM_NCLBUTTONDOWN) ||
(msg.message == (int)Msg.WM_NCMBUTTONDOWN) ||
(msg.message == (int)Msg.WM_NCRBUTTONDOWN))
{
// Is the mouse event for this popup window?
if (msg.hwnd != this.Handle && !IsChild(msg.hwnd) )
{
// No, then we need to exit the popup menu tracking
exitLoop = true;
// DO NOT process the message, leave it on the queue
// and let the real destination window handle it.
leaveMsg = true;
}
}
else
{
// Mouse move occured
if ( msg.message == (int)Msg.WM_MOUSEMOVE )
{
// Is the mouse event for this popup window?
if ((msg.hwnd != this.Handle && !IsChild(msg.hwnd)) )
{
// Eat the message to prevent the destination getting it
Win32.MSG eat = new Win32.MSG();
WindowsAPI.GetMessage(ref eat, 0, 0, 0);
// Do not attempt to pull a message off the queue as it has already
// been eaten by us in the above code
leaveMsg = true;
}
}
}
// Should the message we pulled from the queue?
if (!leaveMsg)
{
if (WindowsAPI.GetMessage(ref msg, 0, 0, 0))
{
WindowsAPI.TranslateMessage(ref msg);
WindowsAPI.DispatchMessage(ref msg);
}
}
else
leaveMsg = false;
}
}
}
}
#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.tabControl = new System.Windows.Forms.TabControl();
this.customColorsPage = new System.Windows.Forms.TabPage();
this.webColorsPage = new System.Windows.Forms.TabPage();
this.systemColorsPage = new System.Windows.Forms.TabPage();
this.tabControl.SuspendLayout();
this.SuspendLayout();
//
// tabControl
//
this.tabControl.Controls.AddRange(new System.Windows.Forms.Control[] {
this.customColorsPage,
this.webColorsPage,
this.systemColorsPage});
this.tabControl.Cursor = System.Windows.Forms.Cursors.Default;
this.tabControl.Location = new System.Drawing.Point(0, 4);
this.tabControl.Name = "tabControl";
this.tabControl.SelectedIndex = 0;
this.tabControl.Size = new System.Drawing.Size(200, 188);
this.tabControl.TabIndex = 0;
this.tabControl.SelectedIndexChanged += new System.EventHandler(this.tabControl_SelectedIndexChanged);
//
// customColorsPage
//
this.customColorsPage.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.customColorsPage.Location = new System.Drawing.Point(4, 22);
this.customColorsPage.Name = "customColorsPage";
this.customColorsPage.Size = new System.Drawing.Size(192, 162);
this.customColorsPage.TabIndex = 0;
this.customColorsPage.Text = "Custom";
this.customColorsPage.Click += new System.EventHandler(this.customColorsPage_Click);
this.customColorsPage.Paint += new System.Windows.Forms.PaintEventHandler(this.customColorsPage_Paint);
this.customColorsPage.MouseDown += new System.Windows.Forms.MouseEventHandler(this.customColorsPage_MouseDown);
//
// webColorsPage
//
this.webColorsPage.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.webColorsPage.Location = new System.Drawing.Point(4, 22);
this.webColorsPage.Name = "webColorsPage";
this.webColorsPage.Size = new System.Drawing.Size(192, 162);
this.webColorsPage.TabIndex = 1;
this.webColorsPage.Text = "Web";
//
// systemColorsPage
//
this.systemColorsPage.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.systemColorsPage.Location = new System.Drawing.Point(4, 22);
this.systemColorsPage.Name = "systemColorsPage";
this.systemColorsPage.Size = new System.Drawing.Size(192, 162);
this.systemColorsPage.TabIndex = 2;
this.systemColorsPage.Text = "System";
//
// ColorPickerDropDown
//
this.AutoScale = false;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(198, 215);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.tabControl});
this.ForeColor = System.Drawing.SystemColors.Control;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "ColorPickerDropDown";
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
this.VisibleChanged += new System.EventHandler(this.ColorPickerDropDown_VisibleChanged);
this.tabControl.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -