8

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.

Community
  • 1
  • 1
Vercas
  • 8,931
  • 15
  • 66
  • 106
  • I've updated my answer for you. – Prisoner Mar 20 '11 at 17:03
  • 2
    what did you do to get it working? When I build I get the following error: Error 1 Could not find file 'Microsoft.Windows.Common-Controls, Version=6.0.0.0, Culture=*, PublicKeyToken=6595b64144ccf1df, ProcessorArchitecture=*, Type=win32' I am using Windows 7 x64 – Ade A Jan 22 '13 at 17:34
  • @AdeA From Visual Studio, Go in Project Properties -> Security -> Uncheck checkbox of "Enable ClickOnce Security Settings" This solved problem for me. – Jaynesh Shah Sep 03 '14 at 12:58

3 Answers3

18

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.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Worked for me and lots of other programmers. What did you do wrong? Post the content of your manifest in your question. – Hans Passant Mar 20 '11 at 16:41
  • I'm building a 64 bit WPF application in Windows 10 and it complains that "Could not find file 'Microsoft.Windows.Common-Controls'" when I try this solution – Andre Soares Jun 06 '16 at 14:34
9

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).

Prisoner
  • 27,391
  • 11
  • 73
  • 102
  • That is very strange, worked fine for me. Are you just calling `MessageBox.Show()`? – Prisoner Mar 20 '11 at 17:51
  • @Prisoner Please add to the answer that Visual Studio needs restart. – Vercas Mar 21 '11 at 17:02
  • @Vercas, I've added that. I didnt need to restart which seems strange. – Prisoner Mar 21 '11 at 17:09
  • @Prisoner It first worked for me when I tried to program on a 64 bit machine (previously I was working on a 32 bit one). So it was a restart, after all. I don't think it was the architecture change that made it work. After all, Visual Studio 2010 is only 32 bit. – Vercas Mar 22 '11 at 09:04
  • 1
    Excellent! This helped me too. – rgshenoy Jun 24 '15 at 09:51
3

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

Stuart
  • 66,722
  • 7
  • 114
  • 165