Change BackColor of ToolStripItem on Mouse Over [d

2019-08-10 08:06发布

问题:

This question already has an answer here:

  • How to change menu hover color 4 answers

So I have a MenuStrip in C# which I am trying to do a darkish theme for, but when I press the button for the dropdown menu well....

Is there a way to make it go from white to another color? I can't seem to figure out a way to do it. This is probably my first time even customizing context menus.

回答1:

You could use MouseHover and MouseLeave event. It's easy. Just do the following steps:

  1. We have a form with these items: http://s3.picofile.com/file/8188577184/Capture.JPG

  2. Choose that dark backcolor for ToolStripMenuItem. I choosed black color for fileToolStripMenuItem in my example.

  3. Use this for MouseHover event:

    private void fileToolStripMenuItem_MouseHover(object sender, EventArgs e)
    {
            fileToolStripMenuItem.BackColor = Color.White;
            fileToolStripMenuItem.ForeColor =Color.Black;
    }
    
  4. Use this for MouseLeave event:

    private void fileToolStripMenuItem_MouseLeave(object sender, EventArgs e)
    {
            fileToolStripMenuItem.BackColor = Color.Black;
            fileToolStripMenuItem.ForeColor = Color.White;
    }