Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF MessageBox buttons aren't OS themed

So the buttons of my message box in WPF aren't themed by the OS.
I even tried this method and it didn't work.

I have a manifest, I am running under Windows 7 Ultimate x86 and .NET Framework 4 Client Profile.


EDIT: It works now.

like image 766
Vercas Avatar asked Mar 20 '11 16:03

Vercas


3 Answers

This is an odd glitch in WPF, it is missing the plumbing to activate visual styles. Doubly-odd because this isn't hard to do.

The workaround is modify the manifest that gets embedded in your program. Select your EXE project, then Project + Add New Item, General, select Application Manifest File. You get the default manifest that gets embedded, note the <assemblyIdentity> and <trustInfo> elements. Paste this in between:

  <dependency>
    <dependentAssembly>
      <assemblyIdentity
          type="win32"
          name="Microsoft.Windows.Common-Controls"
          version="6.0.0.0"
          processorArchitecture="*"
          publicKeyToken="6595b64144ccf1df"
          language="*"
        />
    </dependentAssembly>
  </dependency>

The message box will now have the operating system's visual style, the Aero look with the glowing close button by default on Vista and up.

If you do this in Visual Studio 2010 then this manifest entry is already present but is commented out. You'll find it at the bottom of the file. Just remove the comments, the <!-- before <dependency> and the --> after </dependency>

Beware that this manifest is not enabled when you run with the debugger and the Visual Studio hosting process enabled. That's a different .exe file in your build directory, yourapp.vshost.exe. Project + Properties, Debug tab, scroll down, untick "Enable the Visual Studio hosting process". That has a few side effects related to security, there isn't much point in actually doing this since your user will never have this problem.

like image 178
Hans Passant Avatar answered Nov 13 '22 00:11

Hans Passant


The changes you've made have been saved but you cannot view them whilst debugging in VS. To fix this, right click your project and click "Properties" and then go to "Debug". There is a checkbox at the bottom of the page named "Enable the Visual Studio hosting process" - un-tick this. It is recommended that you restart visual studio after making this change.

Alternatively, you can just build without debugging (CTRL+F5).

like image 31
Prisoner Avatar answered Nov 12 '22 22:11

Prisoner


You could try using http://elegantcode.com/2010/08/05/extended-wpf-toolkit-new-messagebox-control/ - code posted within http://wpftoolkit.codeplex.com/

More documentation also on http://wpftoolkit.codeplex.com/wikipage?title=MessageBox&referringTitle=Home

like image 3
Stuart Avatar answered Nov 12 '22 23:11

Stuart