C#實現(xiàn)Menu和ContextMenu自定義風(fēng)格及contextMenu自定義
為了實現(xiàn)自定義的Menu和ContextMenu效果,下面演示代碼通過派生ProfessionalColorTable類,在自定義的類中重寫ProfessionalColorTable類的相關(guān)聯(lián)的屬性,從而實現(xiàn)自定義菜單效果。
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> /// 鼠標(biāo)移動到菜單項(主菜單及下拉菜單)時,下拉菜單項的邊框 /// </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 下拉菜單面板背景設(shè)置(不包括下拉菜單項) //下拉菜單面板背景一共分為2個部分,左邊為圖像區(qū)域,右側(cè)為文本區(qū)域,需要分別設(shè)置 //ToolStripDropDownBackground設(shè)置文本部分的背景色 public override Color ToolStripDropDownBackground { get { return Color.Black; } } //以ImageMarginGradient開頭的3個設(shè)置的是圖像部分的背景色,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 }
然后對需要實現(xiàn)自定義風(fēng)格的菜單(如:contextMenuStrip1)應(yīng)用如下代碼:
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#實現(xiàn)Menu和ContextMenu自定義風(fēng)格及contextMenu自定義的全部內(nèi)容,希望大家喜歡。
相關(guān)文章
C# 16 進(jìn)制字符串轉(zhuǎn) int的方法
這篇文章主要介紹了C# 16 進(jìn)制字符串轉(zhuǎn) int的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-04-04C#利用性能計數(shù)器監(jiān)控網(wǎng)絡(luò)狀態(tài)
這篇文章主要為大家詳細(xì)介紹了C#利用性能計數(shù)器監(jiān)控網(wǎng)絡(luò)狀態(tài)的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01ftp服務(wù)器搭建部署與C#實現(xiàn)ftp文件的上傳的示例
本文主要介紹了ftp服務(wù)器搭建部署與C#實現(xiàn)ftp文件的上傳的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07新手小白用C# winform 讀取Excel表的實現(xiàn)
這篇文章主要介紹了新手小白用C# winform 讀取Excel表的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01C#連接數(shù)據(jù)庫和更新數(shù)據(jù)庫的方法
這篇文章主要介紹了C#連接數(shù)據(jù)庫和更新數(shù)據(jù)庫的方法,需要的朋友可以參考下2015-08-08