51

How to apply the default Windows style to the standard MessageBox in WPF?

For example, when I execute next code:

MessageBox.Show("Hello Stack Overflow!", "Test", MessageBoxButton.OKCancel, 
    MessageBoxImage.Exclamation);

I'm getting message box:

enter image description here

But in WinForms everything is OK with style:

MessageBox.Show("Hello Stack Overflow!", "Test", MessageBoxButtons.OKCancel, 
    MessageBoxIcon.Exclamation);

enter image description here

kyrylomyr
  • 12,192
  • 8
  • 52
  • 79

5 Answers5

71

According to this page, WPF picks up the old styles for some of the controls.

To get rid of it, you have to create a custom app.manifest file (Add -> New item -> Application Manifest File) and paste the following code in it (right after the /trustInfo - Tag ):

<!-- Activate Windows Common Controls v6 usage (XP and Vista): -->
<dependency>
  <dependentAssembly>
    <assemblyIdentity
      type="win32"
      name="Microsoft.Windows.Common-Controls"
      version="6.0.0.0"
      processorArchitecture="*"
      publicKeyToken="6595b64144ccf1df"
      language="*"/>
  </dependentAssembly>
</dependency>

Then you have to compile your solution with this app.manifest (set it in the project properties -> Application -> Point to the new manifest in "Icons and manifest").

If you start your application now it should look like the WinForms- MessageBox.

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
Gimno
  • 6,506
  • 3
  • 39
  • 39
  • 1
    thank you. Works perfect! I was wonder that this code is already exists in manifest but commented by default. By the way, it works without enabling manifest in project properties. – kyrylomyr Mar 13 '11 at 13:09
  • 7
    When I uncomment this on Windows 7 with VS 2010, I get the error "Could not find file 'Microsoft.Windows.Common-Controls, Version=6.0.0.0, Culture=*, PublicKeyToken=6595b64144ccf1df, ProcessorArchitecture=*, Type=win32'.", it seems most search results reveal people are deleting the manifest to solve their problems (after a VS2005 upgrade). – Brett Ryan Apr 22 '11 at 20:01
  • @BrettRyan this is the reason: [Mike Taulty Explanation](http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2011/02/11/clickonce-taskdialogs-common-controls-and-wpf-applications.aspx) – Ade A Jan 22 '13 at 17:44
  • I tried this solution on Windows 8 and it does not work - it still shows me old style message box :( – Peter Sivák Jun 12 '13 at 19:29
  • I've tested in Windows 8 - everything is fine. Message boxes have correct style if project has manifest with uncommented lines related to style (see above). – kyrylomyr Oct 06 '13 at 09:43
  • 4
    @PeterSivák It will not work in debug mode,so try to start without debugging(CTRL + F5) – WAKU Nov 04 '13 at 03:16
  • 1
    Thanks for the answer. It is astonishing that I have to undergo so much effort for something so basic that should work out-of-the-box in WPF. On a loosely related note, we had much problems with the tray icon as well that we eventually just used the WinForms one with its system menus - it was the only one that worked, on all Windows-es, RemoteApps and Citrix. WPF made a lot of complicated stuff simple but made some of the most basic stuff very complicated. – bokibeg Dec 21 '18 at 11:49
7

as how i triggered it, "redirecting" the usual references to the Forms ones (they work the same, but are named differently):

using MessageBox = System.Windows.Forms.MessageBox;
using MessageBoxImage = System.Windows.Forms.MessageBoxIcon;
using MessageBoxButton = System.Windows.Forms.MessageBoxButtons;
using MessageBoxResult = System.Windows.Forms.DialogResult;

namespace ... class ...

    public MainWindow()
    {
        InitializeComponent();

        System.Windows.Forms.Application.EnableVisualStyles();
    }

    public void do()
    {
        // updated style, but good syntax for a later solution
        MessageBox.Show("Some Message", "DEBUG", MessageBoxButton.OK, MessageBoxImage.Question);
    }

... the manifest solution did not work for me.

BananaAcid
  • 3,221
  • 35
  • 38
  • 1
    +1 very nice approach. Due to issues with ClickOnce deployment I couldn't use the manifest approach, but this one actually did help. – Thomas Flinkow Jun 15 '18 at 07:16
6

The reason that the WinForms one works the way that it does is because visual styles are turned on (i.e. using Common Controls v6) in its Main function. If you remove the call to System.Windows.Forms.Application.EnableVisualStyles(), then the WinForms Message Box will look just like the WPF one.

This doesn't happen for a WPF app, possibly because all of the WPF controls are rendered so there is no need to use the new version of Common Controls.

You might try calling EnableVisualStyles() somewhere in the start up of your WPF application. I don't know if it will work or not, but it's worth a try. This will require a reference to System.Windows.Forms, though.

Andy
  • 30,088
  • 6
  • 78
  • 89
  • 1
    works if only to use everything from WinForms (include reference to System.Windows.Forms and use that version of MessageBox), but its not good way. – kyrylomyr Mar 13 '11 at 13:10
5

Also, for WPF I would recommmend using the Extended WPF Toolkit which has a WPF messagebox

gcores
  • 12,376
  • 2
  • 49
  • 45
1

Create a new manifest and paste this:

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel level="asInvoker" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>
  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
    </application>
  </compatibility>
  <dependency>
    <dependentAssembly>
      <assemblyIdentity
      type="win32"
      name="Microsoft.Windows.Common-Controls"
      version="6.0.0.0"
      processorArchitecture="*"
      publicKeyToken="6595b64144ccf1df"
      language="*"
    />
    </dependentAssembly>
  </dependency>
</assembly>
Pang
  • 9,564
  • 146
  • 81
  • 122
Marco Concas
  • 1,665
  • 20
  • 25