如何在 WPF 中设置窗口的边框和标题栏的样式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9978444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:21:06  来源:igfitidea点击:

How can I style the border and title bar of a window in WPF?

wpfxamlstyling

提问by Marcel Gosselin

We are developing a WPF application which uses Telerik's suite of controls and everything works and looks fine. Unfortunately, we recently needed to replace the base class of all our dialogs, changing RadWindow by the standard WPF window (reason is irrelevant to this discussion). In doing so, we ended up having an application which still looked pretty on all developer's computers (Windows 7 with Aero enabled) but was ugly when used in our client's environment (Terminal Services under Windows Server 2008 R2).

我们正在开发一个 WPF 应用程序,它使用 Telerik 的控件套件,一切正常并且看起来很好。不幸的是,我们最近需要替换所有对话框的基类,将 RadWindow 更改为标准 WPF 窗口(原因与本讨论无关)。通过这样做,我们最终得到了一个应用程序,它在所有开发人员的计算机(启用 Aero 的 Windows 7)上看起来仍然很漂亮,但在我们的客户端环境(Windows Server 2008 R2 下的终端服务)中使用时却很丑陋。

Telerik's RadWindow is a standard user control that mimicks a dialog's behaviour so styling it was not an issue. With WPF's Window though, I have a hard time changing its "border". What I mean by "border" here is both the title bar with the icon and the 3 standard buttons (Minimize, Maximize/Restore, Close) and the resize grip around the window.

Telerik 的 RadWindow 是一个标准的用户控件,它模仿对话框的行为,因此样式设置不是问题。但是,使用 WPF 的 Window,我很难改变它的“边框”。我在这里所说的“边框”是指带有图标的标题栏和 3 个标准按钮(最小化、最大化/恢复、关闭)以及窗口周围的调整大小手柄。

How can I change the looks of these items:

如何更改这些项目的外观:

  • Title bar color
  • 3 standard buttons
  • Window's real border color
  • 标题栏颜色
  • 3个标准按钮
  • 窗口的真实边框颜色

With round corners if possible.

如果可能的话,用圆角。

采纳答案by Muad'Dib

Those are "non-client" areas and are controlled by Windows. Here is the MSDN docs on the subject(the pertinent info is at the top).

这些是“非客户端”区域,由 Windows 控制。这是有关该主题的 MSDN 文档(相关信息位于顶部)。

Basically, you set your Window's WindowStyle="None", then build your own window interface. (similar question on SO)

基本上,您设置 Window 的 WindowStyle="None",然后构建您自己的窗口界面。(关于SO的类似问题

回答by Lescai Ionel

You need to set

你需要设置

WindowStyle="None", AllowsTransparency="True"and optionally ResizeMode="NoResize"
and then set the Styleproperty of the window to your custom window style, where you design the appearance of the window (title bar, buttons, border) to anything you want and display the window contents in a ContentPresenter.

WindowStyle="None"AllowsTransparency="True"然后可以选择将窗口ResizeMode="NoResize"
Style属性设置为您的自定义窗口样式,您可以在其中将窗口的外观(标题栏、按钮、边框)设计为您想要的任何内容,并以ContentPresenter.

Thisseems to be a good article on how you can achieve this, but there are many other articles on the internet.

似乎是一篇关于如何实现这一目标的好文章,但互联网上还有许多其他文章。

回答by Anas

I found a more straight forward solution from @DK comment in this question, the solution is written by Alex and described herewith source, To make customized window:

我在这个问题的@DK 评论中找到了一个更直接的解决方案,该解决方案由 Alex 编写,并在此处使用源代码进行描述,要制作自定义窗口:

  1. download the sample project here
  2. edit the generic.xaml file to customize the layout.
  3. enjoy :).
  1. 此处下载示例项目
  2. 编辑 generic.xaml 文件以自定义布局。
  3. 请享用 :)。

回答by Davide Cannizzo

If someone says you can't because only Windows can control the non-client area, they're wrong!

如果有人说你不能因为只有 Windows 可以控制非客户区,那他们就错了!

That's just a half-truth because Windows lets you specify the dimensions of the non-client area. The fact is, this is possible only throughout the Windows' kernel methods, and you're in .NET, not C/C++. Anyway, don't worry! P/Invoke was meant just for such things! Indeed, the whole of the Windows Form UI and Console application Std-I/O methods are offered using system calls. Hence, you'd have only to perform the right system calls to set the non-client area up, as documented in MSDN.

这只是半真半假,因为 Windows 允许您指定非客户区的尺寸。事实是,这仅在整个 Windows 内核方法中是可能的,并且您使用的是 .NET,而不是 C/C++。总之,别担心!P/Invoke 就是为了这样的事情!实际上,整个 Windows 窗体 UI 和控制台应用程序 Std-I/O 方法都是使用系统调用提供的。因此,您只需执行正确的系统调用即可设置非客户区,如 MSDN 中所述。

However, this is a really hard solution I came up with a lot of time ago. Luckily, as of .NET 4.5, you can use the WindowChromeclass to adjust the non-client area like you want. Hereyou can get to start with.

然而,这是我很久以前想出的一个非常困难的解决方案。幸运的是,从 .NET 4.5 开始,您可以根据需要使用WindowChrome该类来调整非客户区。在这里你可以开始。

In order to make things simpler and cleaner, I'll redirect you here, a guide to change the window border dimensions to whatever you want. By setting it to 0, you'll be able to implement your custom window border in place of the system's one.

为了使事情更简单和更清晰,我将把您重定向到这里,这是一个将窗口边框尺寸更改为您想要的任何尺寸的指南。通过将其设置为 0,您将能够实现您的自定义窗口边框来代替系统的边框。

I'm sorry for not posting a clear example, but later I will for sure.

我很抱歉没有发布一个明确的例子,但稍后我会肯定的。

回答by Belahcene Benzara Tahar

I suggest you to start from an existing solution and customize it to fit your needs, that's better than starting from scratch!

我建议您从现有的解决方案开始,并根据您的需要对其进行自定义,这比从头开始要好!

I was looking for the same thing and I fall on thisopen source solution, I hope it will help.

我一直在寻找同样的东西,我喜欢这个开源解决方案,我希望它会有所帮助。