C#实现Menu和ContextMenu自定义风格及contextMenu自定义

所属分类: 软件编程 / C#教程 阅读数: 86
收藏 0 赞 0 分享

为了实现自定义的Menu和ContextMenu效果,下面演示代码通过派生ProfessionalColorTable类,在自定义的类中重写ProfessionalColorTable类的相关联的属性,从而实现自定义菜单效果。

using System.Drawing;
using System.Windows.Forms;
public class CustomToolStripColorTable : ProfessionalColorTable
{
  /// <summary>
  /// 主菜单项被点击后,展开的下拉菜单面板的边框
  /// </summary>
  public override Color MenuBorder
  {
    get
    {
      return Color.FromArgb(37, 37, 37);
    }
  }
  /// <summary>
  /// 鼠标移动到菜单项(主菜单及下拉菜单)时,下拉菜单项的边框
  /// </summary>
  public override Color MenuItemBorder
  {
    get
    {
      return Color.Transparent;
    }
  }
  #region 顶级菜单被选中背景颜色
  public override Color MenuItemSelectedGradientBegin
  {
    get
    {
      return Color.FromArgb(37, 37, 37);
    }
  }
  public override Color MenuItemSelectedGradientEnd
  {
    get
    {
      return Color.FromArgb(37, 37, 37);
    }
  }
  #endregion
  #region 顶级菜单被按下是,菜单项背景色
  public override Color MenuItemPressedGradientBegin
  {
    get
    {
      return Color.Black;
    }
  }
  public override Color MenuItemPressedGradientMiddle
  {
    get
    {
      return Color.FromArgb(37, 37, 37);
    }
  }
  public override Color MenuItemPressedGradientEnd
  {
    get
    {
      return Color.Black;
    }
  }
  #endregion
  /// <summary>
  /// 菜单项被选中时的颜色
  /// </summary>
  public override Color MenuItemSelected
  {
    get
    {
      return Color.FromArgb(37, 37, 37);
    }
  }
  #region 下拉菜单面板背景设置(不包括下拉菜单项)
  //下拉菜单面板背景一共分为2个部分,左边为图像区域,右侧为文本区域,需要分别设置
  //ToolStripDropDownBackground设置文本部分的背景色
  public override Color ToolStripDropDownBackground
  {
    get
    {
      return Color.Black;
    }
  }
  //以ImageMarginGradient开头的3个设置的是图像部分的背景色,begin->end是从左到右的顺序
  public override Color ImageMarginGradientBegin
  {
    get
    {
      return Color.Black;
    }
  }
  public override Color ImageMarginGradientMiddle
  {
    get
    {
      return Color.Black;
    }
  }
  public override Color ImageMarginGradientEnd
  {
    get
    {
      return Color.Black;
    }
  }
  #endregion
}

然后对需要实现自定义风格的菜单(如:contextMenuStrip1)应用如下代码:

contextMenuStrip1.RenderMode = ToolStripRenderMode.Professional;
contextMenuStrip1.Renderer = new ToolStripProfessionalRenderer(new CustomToolStripColorTable());

ContextMenu的自定义

1.针对整个ContextMenu, 自定义一个Style,去掉竖分割线

<Style x:Key="DataGridColumnsHeaderContextMenuStyle" TargetType="{x:Type ContextMenu}">
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="Grid.IsSharedSizeScope" Value="true"/>
        <Setter Property="HasDropShadow" Value="True"/>
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="{x:Type ContextMenu}">
              <Border Uid="Border_93">
                <Border.Style>
                  <Style TargetType="{x:Type Border}">
                    <Setter Property="Tag" Value="{DynamicResource {x:Static SystemParameters.DropShadowKey}}"/>
                    <Style.Triggers>
                      <DataTrigger Binding="{Binding Tag, RelativeSource={RelativeSource Self}}" Value="True">
                        <Setter Property="Effect">
                          <Setter.Value>
                            <DropShadowEffect BlurRadius="4" Opacity="0.8" ShadowDepth="1"/>
                          </Setter.Value>
                        </Setter>
                      </DataTrigger>
                    </Style.Triggers>
                  </Style>
                </Border.Style>
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Uid="Border_50">
                  <ScrollViewer CanContentScroll="True" Uid="ScrollViewer_9"
              Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}">
                    <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Cycle" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Uid="ItemsPresenter_5"/>
                  </ScrollViewer>
                </Border>
              </Border>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>

2. 针对其中的ItemContainerStyle来写个MenuItem的control template

<Style x:Key="MenuItemStyle1" TargetType="{x:Type MenuItem}"> <Setter Property="Template" Value="{DynamicResource MenuItemControlTemplate1}"/> <Setter Property="Margin" Value="0"></Setter> <Setter Property="Padding" Value="0"></Setter> </Style> <ControlTemplate x:Key="MenuItemControlTemplate1" TargetType="{x:Type MenuItem}"> <Grid x:Name="grid" SnapsToDevicePixels="True" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" > <ContentPresenter ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Grid.Column="0" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsHighlighted" Value="True"> <Setter Property="Background" TargetName="grid" Value="{DynamicResource Brush_PA_CSW_ListBoxItemDefaultHighlight}"/> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Foreground" Value="#FF9A9A9A"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate>
3. contextMenu使用上述style
 <ContextMenu x:Key="DataGridColumnsHeaderContextMenu" 
    ItemTemplate="{DynamicResource HeaderConfigItemTemplate}" 
    ItemContainerStyle="{DynamicResource MenuItemStyle1}"
        Style="{DynamicResource DataGridColumnsHeaderContextMenuStyle}"
/>

以上就是本文通过C#实现Menu和ContextMenu自定义风格及contextMenu自定义的全部内容,希望大家喜欢。

更多精彩内容其他人还在看

C#抽象类与抽象方法详解

这篇文章主要为大家详细介绍了C#抽象类与抽象方法的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C#代码实现扑克牌排序的几种方式

今天小编就为大家分享一篇关于C#代码实现扑克牌排序,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#泛型概念的简介与泛型的使用

今天小编就为大家分享一篇关于C#泛型概念的简介与泛型的使用,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C# 7.0 使用下划线忽略使用的变量的原因分析

这篇文章主要介绍了C# 7.0 使用下划线忽略使用的变量的原因浅析,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C# 中使用正则表达式匹配字符的含义

正则表达式的作用用来描述字符串的特征。本文重点给大家介绍C# 中使用正则表达式匹配字符的含义,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

C# Dictionary和SortedDictionary的简介

今天小编就为大家分享一篇关于C# Dictionary和SortedDictionary的简介,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#中SQL Command的基本用法

今天小编就为大家分享一篇关于C#中SQL Command的基本用法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#使用SQL DataReader访问数据的优点和实例

今天小编就为大家分享一篇关于C#使用SQL DataReader访问数据的优点和实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#使用SQL Dataset数据集代码实例

今天小编就为大家分享一篇关于的文章,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#使用SQL DataAdapter数据适配代码实例

今天小编就为大家分享一篇关于C#使用SQL DataAdapter数据适配代码实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享
查看更多