C#實(shí)現(xiàn)Menu和ContextMenu自定義風(fēng)格及contextMenu自定義
為了實(shí)現(xiàn)自定義的Menu和ContextMenu效果,下面演示代碼通過派生ProfessionalColorTable類,在自定義的類中重寫ProfessionalColorTable類的相關(guān)聯(lián)的屬性,從而實(shí)現(xiàn)自定義菜單效果。
using System.Drawing;
using System.Windows.Forms;
public class CustomToolStripColorTable : ProfessionalColorTable
{
/// <summary>
/// 主菜單項(xiàng)被點(diǎn)擊后,展開的下拉菜單面板的邊框
/// </summary>
public override Color MenuBorder
{
get
{
return Color.FromArgb(37, 37, 37);
}
}
/// <summary>
/// 鼠標(biāo)移動到菜單項(xiàng)(主菜單及下拉菜單)時(shí),下拉菜單項(xiàng)的邊框
/// </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 頂級菜單被按下是,菜單項(xiàng)背景色
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>
/// 菜單項(xiàng)被選中時(shí)的顏色
/// </summary>
public override Color MenuItemSelected
{
get
{
return Color.FromArgb(37, 37, 37);
}
}
#region 下拉菜單面板背景設(shè)置(不包括下拉菜單項(xiàng))
//下拉菜單面板背景一共分為2個(gè)部分,左邊為圖像區(qū)域,右側(cè)為文本區(qū)域,需要分別設(shè)置
//ToolStripDropDownBackground設(shè)置文本部分的背景色
public override Color ToolStripDropDownBackground
{
get
{
return Color.Black;
}
}
//以ImageMarginGradient開頭的3個(gè)設(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
}
然后對需要實(shí)現(xiàn)自定義風(fēng)格的菜單(如:contextMenuStrip1)應(yīng)用如下代碼:
contextMenuStrip1.RenderMode = ToolStripRenderMode.Professional; contextMenuStrip1.Renderer = new ToolStripProfessionalRenderer(new CustomToolStripColorTable());
ContextMenu的自定義
1.針對整個(gè)ContextMenu, 自定義一個(gè)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來寫個(gè)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#實(shí)現(xiàn)Menu和ContextMenu自定義風(fēng)格及contextMenu自定義的全部內(nèi)容,希望大家喜歡。
相關(guān)文章
淺談C#手機(jī)號換成111XXXX1111 這種顯示的解決思路
下面小編就為大家?guī)硪黄獪\談C#手機(jī)號換成111XXXX1111 這種顯示的解決思路。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11
解析Silverlight調(diào)用WCF/Rest異常的解決方法
本篇文章對Silverlight調(diào)用WCF/Rest異常的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C#?winform實(shí)現(xiàn)中英文切換功能的四種方式
這篇文章主要介紹了在C#?winform應(yīng)用程序中實(shí)現(xiàn)中英文切換功能的四種方式,資源文件(Resources),本地化(Localization),動態(tài)設(shè)置控件字體和切換語言環(huán)境這四種方式,下面將詳細(xì)介紹每種方式及其具體實(shí)現(xiàn),并討論它們的優(yōu)缺點(diǎn),需要的朋友可以參考下2024-04-04

