Ara*_*ind 38 .net c# background titlebar winforms
是否可以在C#中更改WinForm标题栏的颜色?
__________________________
[Form1_______________-|[]|X] <- I want to change the color of this
| |
| |
| |
|__________________________|
Run Code Online (Sandbox Code Playgroud)
Ara*_*ind 19
我解决了这个问题.这是代码:
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("User32.dll")]
private static extern IntPtr GetWindowDC(IntPtr hWnd);
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
const int WM_NCPAINT = 0x85;
if (m.Msg == WM_NCPAINT)
{
IntPtr hdc = GetWindowDC(m.HWnd);
if ((int)hdc != 0)
{
Graphics g = Graphics.FromHdc(hdc);
g.FillRectangle(Brushes.Green, new Rectangle(0, 0, 4800, 23));
g.Flush();
ReleaseDC(m.HWnd, hdc);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Asi*_*taq 10
你可以做的是使用GDI 将FormBorderStyle属性设置为None并使用表单执行任何操作.
我使用@Jonas Kohls 的答案创建了一个 C# 类
该类的工作方式就像一个魅力,只需调用DarkTitleBarClass.UseImmersiveDarkMode(Handle, true);您的加载方法即可轻松处理多种表单。
我在升级一些旧的 WinForms 应用程序时使用了它,因此它对 WinForm 友好,但仅在 win 8,10 和 11 上进行了测试
Winforms .NetCore 6.0 下面的示例图片
using System.Runtime.InteropServices;
namespace Myapp.Classes
{
internal class DarkTitleBarClass
{
[DllImport("dwmapi.dll")]
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr,
ref int attrValue, int attrSize);
private const int DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19;
private const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
internal static bool UseImmersiveDarkMode(IntPtr handle, bool enabled)
{
if (IsWindows10OrGreater(17763))
{
var attribute = DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1;
if (IsWindows10OrGreater(18985))
{
attribute = DWMWA_USE_IMMERSIVE_DARK_MODE;
}
int useImmersiveDarkMode = enabled ? 1 : 0;
return DwmSetWindowAttribute(handle, attribute, ref useImmersiveDarkMode, sizeof(int)) == 0;
}
return false;
}
private static bool IsWindows10OrGreater(int build = -1)
{
return Environment.OSVersion.Version.Major >= 10 && Environment.OSVersion.Version.Build >= build;
}
}
}
Run Code Online (Sandbox Code Playgroud)
[编辑] 不要忘记添加app.manifest并取消注释适当的支持的操作系统。