WPF 消息框窗口样式

发布于 2024-10-31 07:47:23 字数 536 浏览 2 评论 0原文

如何将默认的Windows样式应用到WPF中的标准MessageBox

例如,当我执行下一个代码时:

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

我收到消息框:

在此处输入图像描述

但在 WinForms 中一切正常样式:

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

在此处输入图像描述

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

感情废物 2024-11-07 07:47:23

根据 页,WPF 采用了某些控件的旧样式。

要摆脱它,您必须创建一个自定义 app.manifest 文件(添加 -> 新项目 -> 应用程序清单文件)并将以下代码粘贴到其中(就在 /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>

:使用此 app.manifest 编译您的解决方案(在项目属性 -> 应用程序 -> 指向“图标和清单”中的新清单中设置)。

如果您现在启动应用程序,它应该看起来像 WinForms-MessageBox。

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.

迷雾森÷林ヴ 2024-11-07 07:47:23

至于我如何触发它,“重定向”对表单引用的通常引用(它们的工作原理相同,但命名不同):

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);
    }

...清单解决方案对我不起作用。

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.

陌伤浅笑 2024-11-07 07:47:23

WinForms 之所以如此工作,是因为在其 Main 函数中打开了视觉样式(即使用 Common Controls v6)。如果删除对 System.Windows.Forms 的调用.Application.EnableVisualStyles(),那么 WinForms 消息框将看起来像 WPF 消息框。

对于 WPF 应用程序不会发生这种情况,可能是因为所有 WPF 控件都已呈现,因此无需使用新版本的通用控件。

您可以尝试在 WPF 应用程序启动时的某个位置调用 EnableVisualStyles() 。我不知道它是否有效,但值得一试。不过,这需要引用 System.Windows.Forms。

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.

水染的天色ゝ 2024-11-07 07:47:23

另外,对于 WPF,我建议使用 扩展 WPF 工具包,它有一个 WPF 消息框

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

呆头 2024-11-07 07:47:23

创建一个新清单并粘贴以下内容:

<?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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文