c# Menu和ContextMenu自定义风格简单实现
为了实现自定义的Menu和ContextMenu效果,可以通过派生ProfessionalColorTable类,在自定义类中通过重写ProfessionalColorTable类的相关属性,产生自定义菜单效果。
usingSystem.Drawing;usingSystem.Windows.Forms;publicclassCustomToolStripColorTable:ProfessionalColorTable{///<summary>///主菜单项被点击后,展开的下拉菜单面板的边框///</summary>publicoverrideColorMenuBorder{get{returnColor.FromArgb(37,37,37);}}///<summary>///鼠标移动到菜单项(主菜单及下拉菜单)时,下拉菜单项的边框///</summary>publicoverrideColorMenuItemBorder{get{returnColor.Transparent;}}#region顶级菜单被选中背景颜色publicoverrideColorMenuItemSelectedGradientBegin{get{returnColor.FromArgb(37,37,37);}}publicoverrideColorMenuItemSelectedGradientEnd{get{returnColor.FromArgb(37,37,37);}}#endregion#region顶级菜单被按下是,菜单项背景色publicoverrideColorMenuItemPressedGradientBegin{get{returnColor.Black;}}publicoverrideColorMenuItemPressedGradientMiddle{get{returnColor.FromArgb(37,37,37);}}publicoverrideColorMenuItemPressedGradientEnd{get{returnColor.Black;}}#endregion///<summary>///菜单项被选中时的颜色///</summary>publicoverrideColorMenuItemSelected{get{returnColor.FromArgb(37,37,37);}}#region下拉菜单面板背景设置(不包括下拉菜单项)//下拉菜单面板背景一共分为2个部分,左边为图像区域,右侧为文本区域,需要分别设置//ToolStripDropDownBackground设置文本部分的背景色publicoverrideColorToolStripDropDownBackground{get{returnColor.Black;}}//以ImageMarginGradient开头的3个设置的是图像部分的背景色,begin->end是从左到右的顺序publicoverrideColorImageMarginGradientBegin{get{returnColor.Black;}}publicoverrideColorImageMarginGradientMiddle{get{returnColor.Black;}}publicoverrideColorImageMarginGradientEnd{get{returnColor.Black;}}#endregion}
然后对需要实现自定义风格的菜单(如:contextMenuStrip1)应用如下代码:
contextMenuStrip1.RenderMode=ToolStripRenderMode.Professional;contextMenuStrip1.Renderer=newToolStripProfessionalRenderer(newCustomToolStripColorTable());
1.菜单项点击之后,整个菜单项及下拉菜单项边框的颜色
publicoverrideColor MenuBorder
{
get
{
returnColor.Red;
}
}
2.鼠标移动到菜单项上,菜单项边框的颜色(包括下拉菜单项)
publicoverrideColor MenuItemBorder
{
get
{
returnColor.Red;
}
}
3、菜单项被按下时,菜单项背景渐变开始的颜色
publicoverrideColor MenuItemPressedGradientBegin
{
get
{
returnColor.Red;
}
}
4.菜单项被按下时,菜单项背景渐变结束的颜色
publicoverrideColor MenuItemPressedGradientEnd
{
get
{
returnColor.Red;
}
}
5、下拉菜单被选中的颜色
publicoverrideColor MenuItemSelected
{
get
{
returnColor.Red;
}
}
6、主菜单项被选中(而非按下),渐变开始的颜色
publicoverrideColor MenuItemSelectedGradientBegin
{
get
{
returnColor.Red;
}
}
7、主菜单栏渐变开始的颜色
publicoverrideColor MenuStripGradientBegin
{
get
{
returnColor.Red;
}
}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。