C Realizes Menu and ContextMenu Custom Style and contextMenu Custom

  • 2021-07-26 08:43:26
  • OfStack

To achieve custom Menu and ContextMenu effects, the following demo code derives the ProfessionalColorTable class and overrides the associated properties of the ProfessionalColorTable class in the custom class to achieve custom menu effects.


using System.Drawing;
using System.Windows.Forms;
public class CustomToolStripColorTable : ProfessionalColorTable
{
  /// <summary>
  ///  The border of the expanded drop-down menu panel after the main menu item is clicked 
  /// </summary>
  public override Color MenuBorder
  {
    get
    {
      return Color.FromArgb(37, 37, 37);
    }
  }
  /// <summary>
  ///  The border of the drop-down menu item when the mouse moves over the menu item (main menu and drop-down menu) 
  /// </summary>
  public override Color MenuItemBorder
  {
    get
    {
      return Color.Transparent;
    }
  }
  #region  Background color selected for top-level menu 
  public override Color MenuItemSelectedGradientBegin
  {
    get
    {
      return Color.FromArgb(37, 37, 37);
    }
  }
  public override Color MenuItemSelectedGradientEnd
  {
    get
    {
      return Color.FromArgb(37, 37, 37);
    }
  }
  #endregion
  #region  Top-level menu is pressed yes, menu item background color 
  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>
  ///  Color when menu item is selected 
  /// </summary>
  public override Color MenuItemSelected
  {
    get
    {
      return Color.FromArgb(37, 37, 37);
    }
  }
  #region  Drop-down menu panel background settings (excluding drop-down menu items) 
  // Drop-down menu panel background 1 Divide into 2 The left is the image area and the right is the text area, which need to be set separately 
  //ToolStripDropDownBackground Set the background color of the text part 
  public override Color ToolStripDropDownBackground
  {
    get
    {
      return Color.Black;
    }
  }
  // With ImageMarginGradient Beginning 3 Set the background color of the image part, begin->end Is the order from left to right 
  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
}

Then apply the following code to menus that need to implement custom styles (such as contextMenuStrip1):


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

Customization of ContextMenu

1. For the whole ContextMenu, customize one Style and remove the vertical dividing line


<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. Write an control template of MenuItem for ItemContainerStyle


<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 Use the above style
 <ContextMenu x:Key="DataGridColumnsHeaderContextMenu" 
    ItemTemplate="{DynamicResource HeaderConfigItemTemplate}" 
    ItemContainerStyle="{DynamicResource MenuItemStyle1}"
        Style="{DynamicResource DataGridColumnsHeaderContextMenuStyle}"
/>

The above is the whole content of Menu and ContextMenu custom style and contextMenu custom through C #. I hope everyone likes it.


Related articles: