有没有办法使用 Visual Studio 和 Windows 10 样式的窗体创建 ContextMenuStrip.我知道,可以更改它的渲染器以使其看起来像 Windows Vista、7 和 8.这就是我所做的:
但我也想让它在 Windows 10 中看起来像这样:
有没有办法使用 Windows 窗体来完成,或者应该以某种特殊的方式完成,使用 Metro 类等?
您可以实现您的自定义
ColorTable 代码:
公共类 MyColorTable : ProfessionalColorTable{公共覆盖颜色 MenuItemBorder{得到{返回颜色.WhiteSmoke;}}公共覆盖颜色 MenuItemSelected{得到{返回颜色.WhiteSmoke;}}公共覆盖颜色 ToolStripDropDownBackground{得到{返回颜色.白色;}}公共覆盖颜色 ImageMarginGradientBegin{得到{返回颜色.白色;}}公共覆盖颜色 ImageMarginGradientMiddle{得到{返回颜色.白色;}}公共覆盖颜色 ImageMarginGradientEnd{得到{返回颜色.白色;}}}
渲染器代码:
公共类 MyRenderer : ToolStripProfessionalRenderer{公共 MyRenderer(): 基础(新的 MyColorTable()){}受保护的覆盖无效 OnRenderArrow(ToolStripArrowRenderEventArgs e){e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;var r = new Rectangle(e.ArrowRectangle.Location, e.ArrowRectangle.Size);r.Inflate(-2, -6);e.Graphics.DrawLines(Pens.Black, new Point[]{新点(r.Left,r.Top),新点(r.Right, r.Top + r.Height/2),新点(r.Left, r.Top+ r.Height)});}受保护的覆盖无效 OnRenderItemCheck(ToolStripItemImageRenderEventArgs e){e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;var r = new Rectangle(e.ImageRectangle.Location, e.ImageRectangle.Size);r.Inflate(-4, -6);e.Graphics.DrawLines(Pens.Black, new Point[]{新点(r.Left,r.Bottom - r.Height/2),新点(r.Left + r.Width/3,r.Bottom),新点(r.Right, r.Top)});}}
表单加载代码:
private void Form_Load(object sender, EventArgs e){this.contextMenuStrip1.Renderer = new MyRenderer();}
Is there a way to create ContextMenuStrip using Visual Studio and Forms with Windows 10 style. I know, that its Renderer can be changed in order to have look like Windows Vista, 7 and 8. And that is what I made:
But I also would like to make it look like this in Windows 10:
Is there a way to do it using Windows Forms, or it should be done in some special way, using Metro classes etc?
You can implement your custom Renderer
and override OnRenderArrow
and OnRenderItemCheck
and pass your custom ColorTable
to it. Then set it as Renderer
of your ContextMenu
.
Code for ColorTable:
public class MyColorTable : ProfessionalColorTable
{
public override Color MenuItemBorder
{
get { return Color.WhiteSmoke; }
}
public override Color MenuItemSelected
{
get { return Color.WhiteSmoke; }
}
public override Color ToolStripDropDownBackground
{
get { return Color.White; }
}
public override Color ImageMarginGradientBegin
{
get { return Color.White; }
}
public override Color ImageMarginGradientMiddle
{
get { return Color.White; }
}
public override Color ImageMarginGradientEnd
{
get { return Color.White; }
}
}
Code for Renderer:
public class MyRenderer : ToolStripProfessionalRenderer
{
public MyRenderer()
: base(new MyColorTable())
{
}
protected override void OnRenderArrow(ToolStripArrowRenderEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
var r = new Rectangle(e.ArrowRectangle.Location, e.ArrowRectangle.Size);
r.Inflate(-2, -6);
e.Graphics.DrawLines(Pens.Black, new Point[]{
new Point(r.Left, r.Top),
new Point(r.Right, r.Top + r.Height /2),
new Point(r.Left, r.Top+ r.Height)});
}
protected override void OnRenderItemCheck(ToolStripItemImageRenderEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
var r = new Rectangle(e.ImageRectangle.Location, e.ImageRectangle.Size);
r.Inflate(-4, -6);
e.Graphics.DrawLines(Pens.Black, new Point[]{
new Point(r.Left, r.Bottom - r.Height /2),
new Point(r.Left + r.Width /3, r.Bottom),
new Point(r.Right, r.Top)});
}
}
Code for Form Load:
private void Form_Load(object sender, EventArgs e)
{
this.contextMenuStrip1.Renderer = new MyRenderer();
}
这篇关于Windows 10 风格的 ContextMenuStrip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!