ToolStripMenuItem背景图像在鼠标悬停时消失(.net winforms)

时间:2013-06-14 02:50:58

标签: c# .net winforms

我在ToolStripMenuItem上设置了一个BackgroundImage,它是ContextMenuItem的一部分。

当我将鼠标悬停在它上面时,背景图像会消失。蓝色的“突出显示”框出现在它上面(至少我认为这是正在发生的事情)。

当鼠标悬停在背景图像上时,如何显示背景图像?

我尝试在MouseEnter事件中设置图像,但没有运气。

3 个答案:

答案 0 :(得分: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);
        }

    }

设置表单构造函数上的控件的renderes - 适用于toolstrip(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

答案 1 :(得分: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);
            }
        }
    }
}

答案 2 :(得分: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);
    }

}

这对我有用