1

I've set a BackgroundImage on my ToolStripMenuItem that is part of a ContextMenuItem.

When I mouse over it, the background image disappears. The blue "highlight" box shows up over it (at least I think that's what's happening).

How can I display a background image when the mouse is over it?

I tried setting the image in an MouseEnter event but no luck.

4

3 回答 3

3

创建一个从 ToolStripProfessionalRenderer 派生的类

public class customRenderer : ToolStripProfessionalRenderer
    {
        protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
        {
            // check if the object being rendered is actually a ToolStripButton
            if (e.Item is ToolStripButton)
            {
                ToolStripButton button = e.Item as ToolStripButton;

                // only render checked items differently
                if (button.Checked || button.Selected)
                {
                   //do work here
                }
                else
                    base.OnRenderButtonBackground(e);
            }
            // if this object is not a ToolStripButton, use the normal render event
            else
                base.OnRenderButtonBackground(e);
        }

        protected override void OnRenderItemBackground(ToolStripItemRenderEventArgs e)
        {
            //do checking here like above
            base.OnRenderItemBackground(e);
        }

    }

设置您在表单构造函数上拥有的控件的渲染 - 适用于所有工具条(toolstrippbackground、toolstripbutton、contexmenustrip 等)我刚刚覆盖了 OnRenderButtonBackground,因为您提到了这一点,您可以覆盖 ToolStripProfessionalRenderer 的其他方法

toolStrip1.Renderer = new customRenderer();//if you have this and want to override any behaviour
            menuStrip1.Renderer = new customRenderer();//if you have this and want to override any behaviour
            contextMenuStrip1.Renderer = new customRenderer();//if you have this and want to override any behaviour
于 2013-06-14T03:53:36.747 回答
2

为此,您需要使用自定义渲染器。

在 ContextMenuStrip 上设置渲染器

contextMenuStrip1.Renderer = new BackgroundImageRenderer();

添加此“渲染器类”

根据需要自定义变量。

private class BackgroundImageRenderer : ToolStripProfessionalRenderer
{
    protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
    {
        if (!e.Item.Selected)
        {
            base.OnRenderMenuItemBackground(e);
        }
        else
        {
            if (e.Item.Name == "theNameOfMyToolStripMenuItem")
            {
                Image backgroundImage = global::YourProject.Properties.Resources.ImageFileName;
                e.Graphics.DrawImage(backgroundImage, 0, 0, e.Item.Width, e.Item.Height);
            }
            else {
                base.OnRenderMenuItemBackground(e);
            }
        }
    }
}
于 2013-06-14T02:57:41.270 回答
0
public class customRenderer : ToolStripProfessionalRenderer
{
    protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
    {
        // check if the object being rendered is actually a ToolStripButton
        if (e.Item is ToolStripButton)
        {
            ToolStripButton button = e.Item as ToolStripButton;

            // only render checked items differently
            if (button.Checked || button.Selected)
            {
               //do work here
            }
            else
                base.OnRenderButtonBackground(e);
        }
        // if this object is not a ToolStripButton, use the normal render event
        else
            base.OnRenderButtonBackground(e);
    }

    protected override void OnRenderItemBackground(ToolStripItemRenderEventArgs e)
    {
        //do checking here like above
        base.OnRenderItemBackground(e);
    }

}

这对我有用

于 2014-04-16T09:43:13.923 回答