Disabling visual styles for ToolStripDropDownMenu

1.7k views Asked by At

I'm trying to prevent MenuStrip control to be affected by visual styles.

So I've created simple form with simple menu strip and disabled visual styles in Main():

// Application.EnableVisualStyles(); <-- no visual styles
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());

which yields this appearance (note grey border and background):

enter image description here

Now I want to do the same in the way that other controls of this form are affected by visual styles (I want to disable visual styles for this drop down only).

So at first I tried to disable visual styles for DropDown of every ToolStripMenuItem, but it didn't work. Then I tried to disable visual styles for whole MenuStrip, but neither that one worked.

Here's the code of my custom MenuStrip:

class MPMenuStrip : MenuStrip
{
    [DllImportAttribute("uxtheme.dll")]
    private static extern int SetWindowTheme(IntPtr hWnd, string appname, string idlist);

    protected override void OnHandleCreated(EventArgs e)
    {
        // first attempt:
        foreach (ToolStripMenuItem menuItem in this.Items)
            SetWindowTheme(menuItem.DropDown.Handle, "", "");

        // second attempt:
        SetWindowTheme(this.Handle, "", "");
        base.OnHandleCreated(e);
    }
}

Despite of using of custom MenuStrip control, when Application.EnableVisualStyles(); in Main() is being called, the appearance of form is following (blue):

enter image description here

Any ideas what else could possibly affect the appearance of this dropdown?
What am I missing?

Thanks.

1

There are 1 answers

1
Hans Passant On BEST ANSWER

You are missing that MenuStrip is not a native Windows control at all and is thus not directly affected by the visual styles theme. All rendering is done by .NET code. The base class that does this is ToolStripRenderer, with two provided implementations: ToolStripProfessionalRenderer and ToolStripSystemRenderer. The latter one tries to emulate the operating system look, you get it by setting the MenuStrip's RenderMode to "System". The emulation is so-so and starkly wrong on later operating systems like Windows 7.

If you want the real operating system look-and-feel then use MainMenu instead. It does have some limitations compared to MenuStrip. If you want to alter the colors then you can implement your own renderer and modify its color table. As shown in this answer.