WPF 窗口标题栏
我在这篇文章中查看了有关在 winforms 中获取“深色模式”标题栏的信息 Windows 10 上的 WinForms 深色标题栏
很明显,您可以获取该窗口像这样处理(在WPF中)而不是使用this.Handle
IntPtr hWnd = new WindowInteropHelper(GetWindow(this)).EnsureHandle();
所以它可以工作
但我想知道是否可以用任何颜色来做到这一点。Windows
10 和 11 有一个设置可以在设置中打开任何标题栏颜色,但我想知道是否可以获得 hWnd 和每个应用程序都自己执行此操作,因为我可以将其变成黑色,为什么不能使用其他颜色呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
人们一直想看看我是如何让它工作
将其放在你的类下
的任何函数(主要?)
中获取窗口句柄或 hwnd 像这样,
颜色
然后定义0x 字符串的 ,其格式如下这
0xRRGGBB
将字母替换为相应的值,
然后使其发生
注意:这仅适用于 Windows 11
如果您很懒,这里是完整版本
哦是的并导入这些
编辑:颜色采用 BGR 格式,因此请确保它变为蓝色、绿色,红与非红,绿与蓝
People have been wanting to see how I got this to work
Put this under your class
in whatever function (main?)
get the window handle or hwnd like this
then define the color
the 0x string thing is formatted like this
0xRRGGBB
replace the letters to their corresponding values
then make it happen
note: this only works in windows 11
here is the complete version if your lazy
oh yes and import these
Edit: the color goes in the BGR format, so make sure it goes blue, green, red and not red, green blue
您可以采取三种方法来解决这个问题...
1:困难的一个,您要做的是修改Windows控制的非客户区。
缺点是这只能是可能的通过 Windows 的内核方法,您使用的是 .NET,而不是 C/C++。然而,我们可以使用
P/Invoke
。事实上,整个 Windows 窗体 UI 和控制台应用程序 I/O 方法都作为包装器提供,在幕后进行系统调用。因此,正如 MSDN 中记录的那样,完全可以使用P/Invoke
来访问设置非客户区所需的那些方法。如前所述,这是一个“比当前必要的更难”的解决方案。
2:更简单,使用 XAML 制作自己的标题栏
幸运的是,从 .NET 4.5 开始,您可以使用
WindowChrome
类来稍微调整非客户区域根据您的需要,您可以将 WindowStyle 设置为 none,并且可以向您的应用程序添加自定义标题栏,该标题栏可以具有您喜欢的任何颜色,并且可以看起来像 Windows 或其他操作系统的标题栏。您自己开始使用
WindowChrome
您可以转到以下文章 WindowChrome 类 和 WindowChrome 实验。
下方代码的基础并确保添加
WindowStyle="None"
以删除标题栏及其组件< strong>3:最简单的一种,使用第三方库。
您还可以使用第三方组件系统,例如 MahApps.Metro 为WPF。据我所知,您应该能够自定义标题栏颜色。
最终结果可能如下所示:
You can take three approaches to this problem...
1: The hard one, what you are trying to do is modify the non-client area which is controlled by Windows.
The downside is this is possible only through the Windows' kernel methods, and you're in .NET, not C/C++. However, there is
P/Invoke
at our disposal. Indeed, the whole of the Windows Form UI and console application I/O methods are offered as wrappers that make system calls under the hood. Hence, as documented in MSDN, it is completely possible to useP/Invoke
to access those methods that are needed to set up the non–client area.As said prior, this is a "harder than currently necessary" solution.
2: The easier one, making your own title bar with XAML
Luckily, as of .NET 4.5, you can use the
WindowChrome
class to adjust the non-client area somewhat to your needs, you can set theWindowStyle
to none and can add a custom title bar to your application that can have any color you like, and can look like the title bar from Windows or other OS'To get yourself started on
WindowChrome
you can go to these following articles WindowChrome Class and Experiments with WindowChrome.The basis of your code right below the
<Window [...]>
And make sure you add
WindowStyle="None"
to remove the titlebar and its components3: The easiest one, using a third-party library.
You can also use a third-party components system like MahApps.Metro for WPF. You should be able to Customize the title bar color as far as I am aware.
The end result can look something like this: