我正在创建Windows应用程序。在该应用程序中,我需要在MenuStrip
控制项上显示图像,而不是文本。背景图片运行正常,但是在悬停并单击时图片消失了。
有人可以有什么想法,如何在悬停或单击时显示不同的图像或相同的图像。
答案 0 :(得分:1)
您已经在设计器中设置了BackgroundImage
属性,该属性会在鼠标悬停时消失。
应改为设置Image
属性。
要在悬停时显示不同的图像,请注册MouseEnter
和MouseLeave
事件的处理程序,并从处理程序中更改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