You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

重写哪个方法可实现NotiIcon关联的ContextMenuStrip圆角效果

ContextMenuStrip自定义圆角实现问题

在Windows 11系统中,我发现几乎所有系统托盘(Systray)图标的右键菜单都自带圆角效果。由于WPF应用没有原生托盘菜单功能,我采用了WinForm的NotiIcon和ContextMenuStrip组件实现该能力。我已经基于ToolStripProfessionalRenderer配合ProfessionalColorTable完全自定义了渲染逻辑,按照客户需求实现了深色模式适配,但设置RoundedEdges = true无法生效,无法实现圆角效果。我尝试过重写OnPaint方法,但得到的圆角不够平滑,且子菜单没有应用圆角效果。请问明确需要重写哪个方法才能实现ContextMenuStrip的圆角效果?

系统原生圆角菜单示例

现有渲染器实现代码

///Menu Renderer
public class MenuRenderer : ToolStripProfessionalRenderer
{
    //Fields
    private Color primaryColor;
    private Color textColor;
    private int arrowThickness;
    private WindowsTheme systrayTheme;

    [Browsable(false)]
    public WindowsTheme SystrayTheme
    {
        get { return systrayTheme; }
        set { systrayTheme = value; }
    }

    //Constructor
    public MenuRenderer(bool isMainMenu, Color primaryColor, Color textColor, Color menuItemMouseOverColor, Color menuItemMouseOverBorderColor, WindowsTheme theme)
        : base(new MenuColorTable(isMainMenu, primaryColor, menuItemMouseOverColor, menuItemMouseOverBorderColor, theme))
    {
        RoundedEdges = true;
        
        this.primaryColor = primaryColor;
        this.systrayTheme = theme;

        if (isMainMenu)
        {
            arrowThickness = 2;
            if (textColor == Color.Empty) //Set Default Color
                this.textColor = Color.Gainsboro;
            else//Set custom text color 
                this.textColor = textColor;
        }
        else
        {
            arrowThickness = 1;
            if (textColor == Color.Empty) //Set Default Color
                this.textColor = Color.DimGray;
            else//Set custom text color
                this.textColor = textColor;
        }
    }

    //Overrides
    protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
    {
        base.OnRenderItemText(e);
        e.Item.ForeColor = e.Item.Selected ? Color.White : textColor;
    }

    protected override void OnRenderArrow(ToolStripArrowRenderEventArgs e)
    {
        //Fields
        var graph = e.Graphics;
        var arrowSize = new Size(5, 10);
        var arrowColor = e.Item.Selected ? Color.White : primaryColor;
        var rect = new Rectangle(e.ArrowRectangle.Location.X, (e.ArrowRectangle.Height - arrowSize.Height) / 2,
            arrowSize.Width, arrowSize.Height);
        using (GraphicsPath path = new GraphicsPath())
        using (Pen pen = new Pen(arrowColor, arrowThickness))
        {
            //Drawing
            graph.SmoothingMode = SmoothingMode.AntiAlias;
            path.AddLine(rect.Left, rect.Top, rect.Right, rect.Top + rect.Height / 2);
            path.AddLine(rect.Right, rect.Top + rect.Height / 2, rect.Left, rect.Top + rect.Height);
            graph.DrawPath(pen, path);
        }
    }

    protected override void OnRenderGrip(ToolStripGripRenderEventArgs e)
    {
        Rectangle rectangle = new Rectangle(e.AffectedBounds.X, e.AffectedBounds.Y, e.AffectedBounds.Width, e.AffectedBounds.Height);
        GraphicsPath graphicsPath = RoundedRect(rectangle, 8);

        using (Pen pen = new Pen(Color.Green,5))
        {
            e.Graphics.DrawPath(pen, graphicsPath);
            //e.Graphics.FillPath(pen.Brush, graphicsPath);
        }
    }

    protected override void OnRenderToolStripPanelBackground(ToolStripPanelRenderEventArgs e)
    {
        //base.OnRenderToolStripPanelBackground(e);
        Rectangle rectangle = new Rectangle(e.ToolStripPanel.Location.X, e.ToolStripPanel.Location.Y, 
            e.ToolStripPanel.ClientRectangle.Width, e.ToolStripPanel.ClientRectangle.Height);
        GraphicsPath graphicsPath = RoundedRect(rectangle, 8);

        using (Pen pen = new Pen(Color.Green, 5))
        {
            //e.Graphics.DrawPath(pen, graphicsPath);
            e.Graphics.FillPath(pen.Brush, graphicsPath);
        }
    }

    protected override void OnRenderStatusStripSizingGrip(ToolStripRenderEventArgs e)
    {
        //base.OnRenderStatusStripSizingGrip(e);
        Rectangle rectangle = new Rectangle(e.AffectedBounds.X, e.AffectedBounds.Y, e.AffectedBounds.Width, e.AffectedBounds.Height);
        GraphicsPath graphicsPath = RoundedRect(rectangle, 8);

        using (Pen pen = new Pen(Color.Green,5))
        {
            e.Graphics.DrawPath(pen, graphicsPath);
            //e.Graphics.FillPath(pen.Brush, graphicsPath);
        }
    }

    protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
    {
        //Fields
        //var graph = e.Graphics;
        //var borderZise = new Size(e.AffectedBounds.Width + 10, e.AffectedBounds.Height + 10);
        //var borderColor = Color.DeepPink;

        //var rect = new Rectangle(e.AffectedBounds.X, (e.AffectedBounds.Height - borderZise.Height) / 2,
        //    borderZise.Width, borderZise.Height);
        //using (GraphicsPath path = new GraphicsPath())
        //using (Pen pen = new Pen(borderColor, arrowThickness))
        //{
        //    //Drawing
        //    graph.SmoothingMode = SmoothingMode.AntiAlias;
        //    path.AddLine(rect.Left, rect.Top, rect.Right, rect.Top + rect.Height / 2);
        //    path.AddLine(rect.Right, rect.Top + rect.Height / 2, rect.Left, rect.Top + rect.Height);
        //    graph.DrawPath(pen, path);
        //}
        //DrawRoundedRectangle(e.Graphics, e.AffectedBounds.X, e.AffectedBounds.Y, e.AffectedBounds.Width - 5, e.AffectedBounds.Height, 15, Color.Red);

        //Rectangle rectangle = new Rectangle(e.AffectedBounds.X, e.AffectedBounds.Y, e.AffectedBounds.Width, e.AffectedBounds.Height);
        //GraphicsPath graphicsPath = RoundedRect(rectangle, 8);

        //using (Pen pen = new Pen(Color.Green, 2))
        //{
        //    //e.Graphics.DrawPath(pen, graphicsPath);
        //    e.Graphics.FillPath(pen.Brush, graphicsPath);
        //}
    }
    
    public GraphicsPath RoundedRect(Rectangle bounds, int radius)
    {
        int diameter = radius * 2;
        Size size = new Size(diameter, diameter);
        Rectangle arc = new Rectangle(bounds.Location, size);
        GraphicsPath path = new GraphicsPath();

        if (radius == 0)
        {
            path.AddRectangle(bounds);
            return path;
        }

        // top left arc  
        path.AddArc(arc, 180, 90);

        // top right arc  
        arc.X = bounds.Right - diameter;
        path.AddArc(arc, 270, 90);

        // bottom right arc  
        arc.Y = bounds.Bottom - diameter;
        path.AddArc(arc, 0, 90);

        // bottom left arc 
        arc.X = bounds.Left;
        path.AddArc(arc, 90, 90);

        path.CloseFigure();
        return path;
    }
}

////////Custom Menu     
public class CustomContextMenu : ContextMenuStrip
{
    //Fields
    private bool isMainMenu;
    private int menuItemHeight = 20;
    private int menuItemWidth = 20;
    private Color menuItemTextColor = Color.Empty;
    private Color primaryColor = Color.Empty;
    private Color MouseOverColor = Color.Empty;
    private Color MouseOverBorderColor = Color.Empty;
    private WindowsTheme systrayTheme = WindowsTheme.Light;
    private Bitmap menuItemHeaderSize;

    //Constructor
    public CustomContextMenu() 
    {
        
    }

    //Properties
    [Browsable(false)]
    public bool IsMainMenu
    {
        get { return isMainMenu; }
        set { isMainMenu = value; }
    }

    [Browsable(false)]
    public int MenuItemHeight
    {
        get { return menuItemHeight; }
        set { menuItemHeight = value; }
    }
    
    [Browsable(false)]
    public int MenuItemWidth
    {
        get { return menuItemWidth; }
        set { menuItemWidth = value; }
    }

    [Browsable(false)]
    public Color MenuItemTextColor
    {
        get { return menuItemTextColor; }
        set { menuItemTextColor = value; }
    }

    [Browsable(false)]
    public Color PrimaryColor
    {
        get { return primaryColor; }
        set { primaryColor = value; }
    }

    [Browsable(false)]
    public Color MenuItemMouseOverColor
    {
        get { return MouseOverColor; }
        set { MouseOverColor = value; }
    }
    
    [Browsable(false)]
    public Color MenuItemMouseOverBorderColor
    {
        get { return MouseOverBorderColor; }
        set { MouseOverBorderColor = value; }
    }

    [Browsable(false)]
    public WindowsTheme SystrayTheme
    { 
        get { return systrayTheme; }
        set { systrayTheme = value; }
    }

    //Private methods
    private void LoadMenuItemHeight()
    {
        if (isMainMenu)
            menuItemHeaderSize = new Bitmap(menuItemWidth, menuItemHeight);
        else menuItemHeaderSize = new Bitmap(menuItemWidth-5, menuItemHeight);

        foreach (Forms.ToolStripMenuItem menuItemL1 in this.Items)
        {
            menuItemL1.ImageScaling = ToolStripItemImageScaling.None;
            if (menuItemL1.Image == null) menuItemL1.Image = menuItemHeaderSize;

            foreach (Forms.ToolStripMenuItem menuItemL2 in menuItemL1.DropDownItems)
            {
                menuItemL2.ImageScaling = ToolStripItemImageScaling.None;
                if (menuItemL2.Image == null) menuItemL2.Image = menuItemHeaderSize;

                foreach (Forms.ToolStripMenuItem menuItemL3 in menuItemL2.DropDownItems)
                {
                    menuItemL3.ImageScaling = ToolStripItemImageScaling.None;
                    if (menuItemL3.Image == null) menuItemL3.Image = menuItemHeaderSize;

                    foreach (Forms.ToolStripMenuItem menuItemL4 in menuItemL3.DropDownItems)
                    {
                        menuItemL4.ImageScaling = ToolStripItemImageScaling.None;
                        if (menuItemL4.Image == null) menuItemL4.Image = menuItemHeaderSize;
                        ///Level 5++
                    }
                }
            }
        }
    }

    //Overrides
    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);
        if (this.DesignMode == false)
        {
            switch (SystrayTheme)
            {
                case WindowsTheme.Light:
                    {
                        menuItemTextColor = Color.Black;
                    }
                    break;
                case WindowsTheme.Dark:
                    {
                        menuItemTextColor = Color.White;
                    }
                    break;
                case WindowsTheme.HighContrast:
                    {
                        menuItemTextColor = Utility.ToDrawingColor(System.Windows.SystemColors.MenuTextColor);
                    }
                    break;
            }

            this.Renderer = new MenuRenderer(isMainMenu, primaryColor, menuItemTextColor, MouseOverColor, MouseOverBorderColor, SystrayTheme);
            LoadMenuItemHeight();
        }
    }
 }

后续尝试

我最近尝试过重写OnPaint()方法,但得到的圆角不够平滑,且子菜单没有应用圆角效果。

自定义圆角效果存在的问题示例

自定义OnPaint实现代码

private GraphicsPath GetGraphicsPath(RectangleF rect,float radious)
{
    GraphicsPath path = new GraphicsPath();
    path.StartFigure();
    path.AddArc(rect.X, rect.Y, radious, radious, 180, 90);
    path.AddArc(rect.Width - radious, rect.Y, radious, radious, 270, 90);
    path.AddArc(rect.Width - radious, rect.Height - radious, radious, radious, 0, 90);
    path.AddArc(rect.X, rect.Height - radious, radious, radious, 90, 90);

    path.CloseFigure();

    return path;
}

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

    RectangleF rectSurface = new RectangleF(0, 0, this.Width, this.Height);
    RectangleF rectBorder = new RectangleF(1, 1, this.Width - 0.8F, this.Height - 1);

    if (borderRadius > 2) //Rounder
    {
        using (GraphicsPath pathSurface = GetGraphicsPath(rectSurface, borderRadius))
        using (GraphicsPath pathBorder = GetGraphicsPath(rectBorder, borderRadius - 1F))
        using (Pen penSurface = new Pen(this.BackColor, 2))
        using (Pen penBorder = new Pen(borderColor, borderSize))
        {
            penBorder.Alignment = PenAlignment.Inset;
            //Menu surface
            this.Region = new Region(pathSurface);
            //Draw surface border for HD result
            e.Graphics.DrawPath(penSurface, pathSurface);

            //Menu Border
            if (borderSize >= 1)
                e.Graphics.DrawPath(penBorder, pathBorder);
        }
    }
    else //Normal contex menu
    {
        //Menu surface
        this.Region = new Region(rectSurface);
        //Menu border
        if (borderSize >= 1)
        {
            using (Pen penBorder = new Pen(borderColor, borderSize))
            {
                penBorder.Alignment = PenAlignment.Inset;
                e.Graphics.DrawRectangle(penBorder, 0, 0, this.Width - 1, this.Height - 1);
            }
        }
    }

}
        
//Overrides
protected override void OnHandleCreated(EventArgs e)
{
    base.OnHandleCreated(e);
    

    if (this.DesignMode == false)
    {
        //this.Parent.BackColorChanged += Parent_BackColorChanged;

        switch (SystrayTheme)
        {
            case WindowsTheme.Light:
                {
                    menuItemTextColor = Color.Black;
                }
                break;
            case WindowsTheme.Dark:
                {
                    menuItemTextColor = Color.White;
                }
                break;
            case WindowsTheme.HighContrast:
                {
                    menuItemTextColor = Utility.ToDrawingColor(System.Windows.SystemColors.MenuTextColor);
                }
                break;
        }
        this.RenderMode = ToolStripRenderMode.Professional; 
        this.Renderer = new MenuRenderer(isMainMenu, primaryColor, menuItemTextColor, MouseOverColor, MouseOverBorderColor, SystrayTheme);
        LoadMenuItemHeight();
    }
}

private void Parent_BackColorChanged(object sender, EventArgs e)
{
    if (this.DesignMode)
        this.Invalidate();
}

内容的提问来源于stack exchange,提问作者consequence007

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 17:45:05