有没有办法使用visual studio和windows 10风格的窗体创建contextMenuStrip。我知道,它的渲染器可以更改,以便看起来像WindowsVista,7和8。我就是这么做的:
c# - Windows 10样式的ContextMenuStrip-LMLPHP
但我也希望在Windows10中这样:
c# - Windows 10样式的ContextMenuStrip-LMLPHP
有没有使用windows窗体的方法,或者应该用一些特殊的方法,使用metro类等来完成?

最佳答案

您可以实现自定义Renderer并覆盖OnRenderArrowOnRenderItemCheck并将自定义ColorTable传递给它。然后将其设置为RendererContextMenu
c# - Windows 10样式的ContextMenuStrip-LMLPHP
颜色表代码:

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; }
    }
}

渲染器代码:
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)});
    }
}

模板加载代码:
private void Form_Load(object sender, EventArgs e)
{
    this.contextMenuStrip1.Renderer = new MyRenderer();
}

07-25 20:33