C#实现Menu和ContextMenu自定义风格及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());
ContextMenu的自定义
1.针对整个ContextMenu,自定义一个Style,去掉竖分割线
<Stylex:Key="DataGridColumnsHeaderContextMenuStyle"TargetType="{x:TypeContextMenu}"> <SetterProperty="SnapsToDevicePixels"Value="True"/> <SetterProperty="Grid.IsSharedSizeScope"Value="true"/> <SetterProperty="HasDropShadow"Value="True"/> <SetterProperty="Template"> <Setter.Value> <ControlTemplateTargetType="{x:TypeContextMenu}"> <BorderUid="Border_93"> <Border.Style> <StyleTargetType="{x:TypeBorder}"> <SetterProperty="Tag"Value="{DynamicResource{x:StaticSystemParameters.DropShadowKey}}"/> <Style.Triggers> <DataTriggerBinding="{BindingTag,RelativeSource={RelativeSourceSelf}}"Value="True"> <SetterProperty="Effect"> <Setter.Value> <DropShadowEffectBlurRadius="4"Opacity="0.8"ShadowDepth="1"/> </Setter.Value> </Setter> </DataTrigger> </Style.Triggers> </Style> </Border.Style> <BorderBorderBrush="{TemplateBindingBorderBrush}"BorderThickness="{TemplateBindingBorderThickness}"Background="{TemplateBindingBackground}"Uid="Border_50"> <ScrollViewerCanContentScroll="True"Uid="ScrollViewer_9" Style="{DynamicResource{ComponentResourceKeyResourceId=MenuScrollViewer,TypeInTargetAssembly={x:TypeFrameworkElement}}}"> <ItemsPresenterKeyboardNavigation.DirectionalNavigation="Cycle"SnapsToDevicePixels="{TemplateBindingSnapsToDevicePixels}"Uid="ItemsPresenter_5"/> </ScrollViewer> </Border> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style>
2.针对其中的ItemContainerStyle来写个MenuItem的controltemplate
<Stylex:Key="MenuItemStyle1"TargetType="{x:TypeMenuItem}"><SetterProperty="Template"Value="{DynamicResourceMenuItemControlTemplate1}"/><SetterProperty="Margin"Value="0"></Setter><SetterProperty="Padding"Value="0"></Setter></Style><ControlTemplatex:Key="MenuItemControlTemplate1"TargetType="{x:TypeMenuItem}"><Gridx:Name="grid"SnapsToDevicePixels="True"VerticalAlignment="Stretch"HorizontalAlignment="Stretch"><ContentPresenterContentTemplate="{TemplateBindingHeaderTemplate}"Content="{TemplateBindingHeader}"Grid.Column="0"ContentStringFormat="{TemplateBindingHeaderStringFormat}"ContentSource="Header"RecognizesAccessKey="True"SnapsToDevicePixels="{TemplateBindingSnapsToDevicePixels}"/></Grid><ControlTemplate.Triggers><TriggerProperty="IsHighlighted"Value="True"><SetterProperty="Background"TargetName="grid"Value="{DynamicResourceBrush_PA_CSW_ListBoxItemDefaultHighlight}"/></Trigger><TriggerProperty="IsEnabled"Value="False"><SetterProperty="Foreground"Value="#FF9A9A9A"/></Trigger></ControlTemplate.Triggers></ControlTemplate> 3.contextMenu使用上述style <ContextMenux:Key="DataGridColumnsHeaderContextMenu" ItemTemplate="{DynamicResourceHeaderConfigItemTemplate}" ItemContainerStyle="{DynamicResourceMenuItemStyle1}" Style="{DynamicResourceDataGridColumnsHeaderContextMenuStyle}" />
以上就是本文通过C#实现Menu和ContextMenu自定义风格及contextMenu自定义的全部内容,希望大家喜欢。