C#实现Menu和ContextMenu自定义风格及contextMenu自定义
程序员文章站
2023-12-10 23:41:16
为了实现自定义的menu和contextmenu效果,下面演示代码通过派生professionalcolortable类,在自定义的类中重写professionalcolo...
为了实现自定义的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自定义的全部内容,希望大家喜欢。