Thera are many Color Pickers on this site, but I did not find any color picker base on MenuItem
. So, I have written a Color Picker based on MenuItem
.
Background
In Visual Studio .NET 2003, the MenuItem
class contains a property Break
. The Break
property is to create a new menuitem
where each menu is placed next to each other horizontally instead of in a vertical list.
The Break
property is to create a menu where each menu is placed next to each other horizontally instead of in a vertical list.
I use the property to make this picker.
Using the Picker
First copy the CtxCol
class into your project.
Declare a variable for the CtxCol
.
In the Load
Event, create a new instance and set the Context Menu or DropDownMenu
of ToolbarButton
to the declared variable.
private CtxCol ccol;
private void Form1_Load(object sender, System.EventArgs e)
{
ccol = new CtxCol(rtf);
tbcol.DropDownMenu = ccol;
rtf.ContextMenu = ccol;
}
That's all.
CtxCol.cs Code
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace TlbColPic
{ public class CtxCol :ContextMenu
{
#region Declares
private RichTextBox rtf;
private MenuItemC m1 = new MenuItemC(Color.FromArgb(0,0,0),"Black");
private MenuItemC m2 = new MenuItemC(Color.FromArgb(128,0,0),"DarkRed");
private MenuItemC m3 = new MenuItemC(Color.FromArgb(255,0,0),"Red");
private MenuItemC m4 = new MenuItemC(Color.FromArgb(255,0,255),"Pink");
private MenuItemC m5 = new MenuItemC(Color.FromArgb(255,153,204),"Rose");
private MenuItemC m6 = new MenuItemC(Color.FromArgb(153,51,0),"Brown");
private MenuItemC m7 = new MenuItemC(Color.FromArgb(255,102,0),"Orange");
private MenuItemC m8 = new MenuItemC(Color.FromArgb(255,153,0),"LightOrange");
private MenuItemC m9 = new MenuItemC(Color.FromArgb(255,204,0),"Gold");
private MenuItemC m10 = new MenuItemC(Color.FromArgb(255,204,153),"Tan");
private MenuItemC m11 = new MenuItemC( Color.FromArgb(51,51,0),"Ollive Green");
private MenuItemC m12 = new MenuItemC(Color.FromArgb(128,128,0),"DarkYellow");
private MenuItemC m13 = new MenuItemC(Color.FromArgb(153,204,0),"Lime");
private MenuItemC m14 = new MenuItemC( Color.FromArgb(255,255,0),"Yellow");
private MenuItemC m15 = new MenuItemC(Color.FromArgb(255,255,153),"LightYellow");
private MenuItemC m16 = new MenuItemC( Color.FromArgb(0,51,0),"DarkGreen");
private MenuItemC m17 = new MenuItemC(Color.FromArgb(0,128,0),"Green");
private MenuItemC m18 = new MenuItemC( Color.FromArgb(51,153,102),"SeaGreen");
private MenuItemC m19 = new MenuItemC( Color.FromArgb(0,255,0),"BrightGreen");
private MenuItemC m20 = new MenuItemC(Color.FromArgb(204,255,204),"LightGreen");
private MenuItemC m21 = new MenuItemC(Color.FromArgb(0,51,102),"Dark Teal");
private MenuItemC m22 = new MenuItemC(Color.FromArgb(128,0,0),"Teal");
private MenuItemC m23 = new MenuItemC(Color.FromArgb(51,204,204),"Aqua");
private MenuItemC m24 = new MenuItemC(Color.FromArgb(0,255,255),"Turquoise");
private MenuItemC m25 =
new MenuItemC(Color.FromArgb(204,255,255),"Light Turquoise");
private MenuItemC m26 = new MenuItemC(Color.FromArgb(0,0,128),"Dark Blue");
private MenuItemC m27 = new MenuItemC(Color.FromArgb(0,0,225),"Blue");
private MenuItemC m28 = new MenuItemC(Color.FromArgb(51,102,255),"Light Blue");
private MenuItemC m29 = new MenuItemC(Color.FromArgb(0,204,255),"Sky Blue");
private MenuItemC m30 = new MenuItemC(Color.FromArgb(153,204,255),"Pale Blue");
private MenuItemC m31 = new MenuItemC( Color.FromArgb(51,51,153),"Indigo");
private MenuItemC m32 = new MenuItemC(Color.FromArgb(102,102,153),"Blue-Gray");
private MenuItemC m33 = new MenuItemC(Color.FromArgb(128,0,128),"Violet");
private MenuItemC m34 = new MenuItemC( Color.FromArgb(153,51,102),"Plum");
private MenuItemC m35 = new MenuItemC(Color.FromArgb(204,153,255),"Lavender");
private MenuItemC m36 = new MenuItemC( Color.FromArgb(51,51,51),"Gray-80%");
private MenuItemC m37 = new MenuItemC(Color.FromArgb(128,128,128),"Gray-50%");
private MenuItemC m38 = new MenuItemC( Color.FromArgb(153,153,153),"Gray-40%");
private MenuItemC m39 = new MenuItemC( Color.FromArgb(192,192,192),"Gray-25%");
private MenuItemC m40 = new MenuItemC(Color.FromArgb(255,255,255),"White");
private MenuItemC mt = new MenuItemC(Color.Black,"More Colors..");
#endregion
private Pen border=new Pen(Color.FromArgb(0,0,128));
public CtxCol(RichTextBox _rtf)
{
LoadCtx();
rtf = _rtf;
}
private void LoadCtx()
{
MenuItems.AddRange(new MenuItemC[]{m1,m2,m3,m4,m5,m6,m7,
m8,m9,m10,m11,m12,m13,
m14,m15,m16,m17,m18,m19,m20 ,
m21,m22,m23,m24,m25,m26,m27,
m28,m29,m30,m31,m32,m33,
m34,m35,m36,m37,m38,m39,m40
});
m6.Break = true;
m11.Break = true;
m16.Break = true;
m21.Break = true;
m26.Break = true;
m31.Break = true;
m36.Break = true;
for(int i = 0; i < MenuItems.Count; i ++)
{
MenuItem m = MenuItems[i];
m.OwnerDraw = true;
m.Click +=new EventHandler(m_Click);
m.DrawItem +=new DrawItemEventHandler(m_DrawItem);
m.MeasureItem +=new MeasureItemEventHandler(m_MeasureItem);
}
}
private void m_Click(object sender, EventArgs e)
{
MenuItemC mi = (MenuItemC)sender;
Color c = mi.Color;
if(mi.ColorName != "More Colors..")
{
if(rtf != null)
rtf.SelectionColor = c;
}
else
{
ColorDialog coldlg = new ColorDialog();
coldlg.Color = rtf.SelectionColor;
if(coldlg.ShowDialog() == DialogResult.OK)
{
if(rtf != null)
rtf.SelectionColor = coldlg.Color;
}
}
}
private void m_DrawItem(object sender, DrawItemEventArgs e)
{
MenuItemC mi = (MenuItemC)sender;
Color c = mi.Color;
Rectangle rct = e.Bounds;
if(mi.ColorName != "More Colors..")
{
e.Graphics.FillRectangle(new SolidBrush(Color.White),rct);
rct.Inflate(-3,-3);
e.Graphics.FillRectangle(new SolidBrush(c),rct);
e.Graphics.DrawRectangle
(new Pen(new SolidBrush(SystemColors.ControlDark),1),rct);
}
else
{
rct.Inflate(-3,-3);
e.Graphics.FillRectangle(new SolidBrush(Color.White),e.Bounds);
DrawText(e.Graphics,rct,mi.ColorName);
}
if ( (e.State & DrawItemState.Selected) == DrawItemState.Selected )
{
if(mi.ColorName == "More Colors..")
{
try
{
Rectangle r = e.Bounds;
r.Inflate(-1,-1);
e.Graphics.FillRectangle
(new SolidBrush(Color.FromArgb(255,244,204)),r);
e.Graphics.DrawRectangle(border,r);
DrawText(e.Graphics,rct,mi.ColorName);
}
catch(Exception ex)
{
rtf.Text = ex.ToString();
return;
}
}
else
{
rct.Inflate(2,2);
e.Graphics.DrawRectangle(new Pen
(new SolidBrush(Color.Blue),1),rct);
}
}
else
{
}
e.Graphics.Dispose();
}
private void m_MeasureItem(object sender, MeasureItemEventArgs e)
{
MenuItemC mi = (MenuItemC)sender;
if(mi.ColorName != "More Colors..")
{
e.ItemWidth = 5;
e.ItemHeight = 15;
}
else
{
SizeF f = e.Graphics.MeasureString
(mi.ColorName,SystemInformation.MenuFont);
e.ItemWidth = (int)f.Width +8;
e.ItemHeight = (int)f.Height + 12;
}
}
private void DrawText(Graphics g,Rectangle rec,string text)
{
string st = text;
StringFormat str = new StringFormat();
str.Alignment = StringAlignment.Center;
str.LineAlignment = StringAlignment.Center;
g.DrawString(st, SystemInformation.MenuFont,
new SolidBrush(Color.Black),rec,
str);
g.Dispose();
}
}
public class MenuItemC: MenuItem
{
private string colname;
private Color col;
public MenuItemC(Color _col, string cname)
{
col = _col;
colname = cname;
Text = colname;
}
public Color Color
{
get
{
return col;
}
set
{
col = value;
}
}
public string ColorName
{
get
{
return colname;
}
set
{
colname = value;
}
}
}
}
Problems
There are two problems that I found:
- The source project format is VS.NET 2005 but the controls and classes are of VS.NET 2003 format. I use the
MenuItem
class because MenuItemStrip
of VS.NET 2005 does not contain Break
property. - I have added a
MenuItem
named "More Colors..". If you click this Item, a color dialog will be opened for picking more colors. But the problem found is an Error when the state is selected. So I did not add the item to the class. I spent a lot of time trying to correct this but I can't. If you can solve the problem, please post the solution in the message board below. To add the menuitem
, open CtxCol.cs and add mt
at the end of the MenuItems.AddRange
methods. Or copy the code in the LoadCtx()
method.
MenuItems.AddRange(new MenuItemC[]{m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12,m13,
m14,m15,m16,m17,m18,m19,m20 ,
m21,m22,m23,m24,m25,m26,m27,m28,m29,m30,m31,m32,m33,
m34,m35,m36,m37,m38,m39,m40,mt});
Conclusion
My homeland is not America or any other English-speaking country. Please forgive the incorrect English. AND I am not also a good developer. I have sent this article for solving the problems that I have described.
Wishing you to be hale and hearty.
History
- 21st September, 2007: Initial post