菜单项中的背景图像在悬停时消失并单击

时间:2018-10-22 07:08:56

标签: c# winforms contextmenu menustrip

我正在创建Windows应用程序。在该应用程序中,我需要在MenuStrip控制项上显示图像,而不是文本。背景图片运行正常,但是在悬停并单击时图片消失了。

菜单项上的图像

enter image description here

点击/悬停时消失

enter image description here

有人可以有什么想法,如何在悬停或单击时显示不同的图像或相同的图像。

1 个答案:

答案 0 :(得分:1)

您已经在设计器中设置了BackgroundImage属性,该属性会在鼠标悬停时消失。

应改为设置Image属性。

要在悬停时显示不同的图像,请注册MouseEnterMouseLeave事件的处理程序,并从处理程序中更改Image属性。

private void toolStripMenuItem1_MouseEnter(object sender, EventArgs e)
{
    this.toolStripMenuItem1.Image = global::WindowsFormsApp1.Properties.Resources.Hover;
}

private void toolStripMenuItem1_MouseLeave(object sender, EventArgs e)
{
    this.toolStripMenuItem1.Image = global::WindowsFormsApp1.Properties.Resources.Normal;
}

编辑:如果您需要在悬停时显示不同的BackgroundImage,请使用自定义渲染(首先删除上面的MouseEnter/MouseLeave代码,并同时设置{{1} }到设计器中的 none

Image/BackgroundImage