精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。”需要全文内容也请联系孙老师。
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. 在这个网站上有很多颜色选择器,但是我没有找到基于菜单项的任何颜色选择器。所以,我写了一个基于子菜单的颜色选择器。
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. 在Visual Studio .NET2003中,子菜单类包含一个Break属性。Break属性是创建一个新的子菜单,新菜单里每个菜单紧挨着下一个菜单水平放置而不是一个垂直列表。
I use the property to make this picker. 我使用这个属性来创建选择器。
First copy the CtxCol class into your project. 首先复制CtxCol类到您的项目。
Declare a variable for the CtxCol. 给CtxCol声明一个变量。
In the Load Event, create a new instance and set the Context Menu or DropDownMenu of ToolbarButton to the declared variable.
在Load事件里,创建一个新的实例并设置上下文菜单或下拉菜单上的工具栏按钮到定义的变量上。
private CtxCol ccol;private void Form1_Load(object sender, System.EventArgs e) { ccol = new CtxCol(rtf); tbcol.DropDownMenu = ccol; // the toolbar button rtf.ContextMenu = ccol; }
That's all. 这是所有。
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace TlbColPic { /// <summary> /// Summary description for CtxColl. /// </summary> 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; } } } }
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. 源项目格式为VS.NET 2005,但控制和类VS.NET2003的格式。我使用子菜单类因为VS.NET 2005的MenuItemStrip不包含Break属性。
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.
我添加了一个名为“更多颜色”的子菜单。如果你点击这个项目,一个颜色对话框将打开更多的颜色选项。但发现一个选择状态错误的问题。所以我没有将项目添加到类。我花了很多时间试图纠正这个但我没有成功。如果你能解决这个问题,请将解决方案写在下面的留言板。添加菜单项,打开CtxCol.cs并在AddRange方法末尾添加菜单项,。或复制Ctx代码中的Load()方法。
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});
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. 我的家乡不是美国或其他说英语的国家,请原谅有错误的英语。我也不是一个优秀的开发者。我在这篇文章中只解决了我所描述的问题。