WPF通过WindowChrome实现对标题栏的设置(隐藏标题栏 - Win11)

1 篇文章 0 订阅

通过NuGet插件管理器下载WindowChrome包

<shell:WindowChrome ResizeBorderThickness="1"
					CaptionHeight="0"
					GlassFrameThickness="10"
					CornerRadius="10"/>

ResizeBorderThickness:指定应用程序窗口外部周围的不可见边框的厚度。这个边框允许用户单击并拖动以调整窗口的大小;

CaptionHeight:设置标题栏的高度,设置为0则完全隐藏标题栏。这个方法与WindowStyle="None"有本质的区别,用这个方法设置时,使用this.WindowState = WindowState.Minimized来最小化窗口是有动画过程的;

GlassFrameThickness:设置窗体外部阴影厚度;

CornerRadius:窗体圆角;

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在WPF应用程序中设置标题栏颜色,你可以使用以下代码: 1. 首先,添加以下命名空间: ```csharp using System.Windows.Interop; using System.Runtime.InteropServices; ``` 2. 接着,在你的窗口类中添加以下方法: ```csharp private void SetWindowTitleBarColor(Color color) { var hwnd = new WindowInteropHelper(this).Handle; var accent = new AccentPolicy(); accent.AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND; accent.AccentFlags = 0x20 | 0x40 | 0x80 | 0x100; accent.GradientColor = (color.R << 16) | (color.G << 8) | color.B; var accentStructSize = Marshal.SizeOf(accent); var accentPtr = Marshal.AllocHGlobal(accentStructSize); Marshal.StructureToPtr(accent, accentPtr, false); var data = new WindowCompositionAttributeData(); data.Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY; data.SizeOfData = accentStructSize; data.Data = accentPtr; SetWindowCompositionAttribute(hwnd, ref data); Marshal.FreeHGlobal(accentPtr); } [DllImport("user32.dll")] internal static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data); [StructLayout(LayoutKind.Sequential)] public struct AccentPolicy { public AccentState AccentState; public int AccentFlags; public int GradientColor; public int AnimationId; } public enum AccentState { ACCENT_DISABLED = 0, ACCENT_ENABLE_GRADIENT = 1, ACCENT_ENABLE_TRANSPARENTGRADIENT = 2, ACCENT_ENABLE_BLURBEHIND = 3, ACCENT_ENABLE_ACRYLICBLURBEHIND = 4, ACCENT_INVALID_STATE = 5 } public enum WindowCompositionAttribute { WCA_UNDEFINED = 0, WCA_NCRENDERING_ENABLED = 1, WCA_NCRENDERING_POLICY = 2, WCA_TRANSITIONS_FORCEDISABLED = 3, WCA_ALLOW_NCPAINT = 4, WCA_CAPTION_BUTTON_BOUNDS = 5, WCA_NONCLIENT_RTL_LAYOUT = 6, WCA_FORCE_ICONIC_REPRESENTATION = 7, WCA_EXTENDED_FRAME_BOUNDS = 8, WCA_HAS_ICONIC_BITMAP = 9, WCA_THEME_ATTRIBUTES = 10, WCA_NCRENDERING_EXILED = 11, WCA_NCADORNMENTINFO = 12, WCA_EXCLUDED_FROM_LIVEPREVIEW = 13, WCA_VIDEO_OVERLAY_ACTIVE = 14, WCA_FORCE_ACTIVEWINDOW_APPEARANCE = 15, WCA_DISALLOW_PEEK = 16, WCA_CLOAK = 17, WCA_CLOAKED = 18, WCA_ACCENT_POLICY = 19, WCA_FREEZE_REPRESENTATION = 20, WCA_EVER_UNCLOAKED = 21, WCA_VISUAL_OWNER = 22, WCA_LAST = 23 } [StructLayout(LayoutKind.Sequential)] public struct WindowCompositionAttributeData { public WindowCompositionAttribute Attribute; public IntPtr Data; public int SizeOfData; } ``` 3. 最后,在窗口的构造函数中调用 `SetWindowTitleBarColor` 方法,并传递一个 `Color` 类型的参数,如下所示: ```csharp public MainWindow() { InitializeComponent(); SetWindowTitleBarColor(Colors.Blue); } ``` 这将设置标题栏的颜色为蓝色。你可以使用任何有效的 `Color` 对象来设置颜色。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值