程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#完成Menu和ContextMenu自界說作風及contextMenu自界說

C#完成Menu和ContextMenu自界說作風及contextMenu自界說

編輯:C#入門知識

C#完成Menu和ContextMenu自界說作風及contextMenu自界說。本站提示廣大學習愛好者:(C#完成Menu和ContextMenu自界說作風及contextMenu自界說)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成Menu和ContextMenu自界說作風及contextMenu自界說正文


為了完成自界說的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"
              >
                    <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}" 
    ItemContainer
        
/>

以上就是本文經由過程C#完成Menu和ContextMenu自界說作風及contextMenu自界說的全體內容,願望年夜家愛好。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved