如何使System.Windows.MessageBox的按钮样式化?

4

我正在使用WPF应用程序中的System.Windows.MessageBox,但由于某种原因,它的按钮采用Windows 2000的样式 - 不是WinXP,不是Aero,也不是WPF默认样式。只是灰色的基本三维边框。

我该如何使它们以更现代的样式显示?(无论哪种)

1个回答

7
您可以通过使用清单文件来解决此问题。请参阅以下文章,了解逐步说明:为什么我在WPF中会得到旧样式的文件对话框和消息框
基本上,您需要将一个名为“清单”的XML文件添加到您的应用程序中。
更新:
实际上,在VS2008中执行此操作非常容易。转到“项目属性”->“应用程序”,然后单击“查看UAC设置”按钮。这将自动创建一个应用程序清单文件并打开它。按照以下方式编辑此文件:
在以下行之后:
</trustInfo>

请将以下依赖项粘贴到dependency部分:
  <!-- 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>

我的完整清单如下:

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <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">
        <!-- UAC Manifest Options
            If you want to change the Windows User Account Control level replace the 
            requestedExecutionLevel node with one of the following.

        <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
        <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />

            If you want to utilize File and Registry Virtualization for backward 
            compatibility then delete the requestedExecutionLevel node.
        -->
        <requestedExecutionLevel level="asInvoker" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>
  <!-- Activate Windows Common Controls v6 usage (XP, Vista, Win 7) to support themed dialogs: -->
  <dependency>
    <dependentAssembly>
      <assemblyIdentity
          type="win32"
          name="Microsoft.Windows.Common-Controls"
          version="6.0.0.0"
          processorArchitecture="*"
          publicKeyToken="6595b64144ccf1df"
          language="*"
        />
    </dependentAssembly>
  </dependency>
</asmv1:assembly>

完成这些步骤后,只需构建您的应用程序,运行即可,MessageBox对话框按钮将采用系统主题样式。

2
为了获得更多信息,现在使用Visual Studio 2010更加容易,只需向您的应用程序添加清单(添加,新项目...,应用程序清单文件),一切都已经准备就绪。取消底部的最后一组注释,这将生成与此帖子中显示的相同文件。 - RedGlyph

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接