簡體   English   中英

WPF Window 標題欄

[英]WPF Window Titlebar

我看過這篇關於在 winforms 中獲取“暗模式”標題欄的文章WinForms Dark title bar on Windows 10

很明顯,您可以獲得 window 這樣的句柄(在 WPF 中),而不是使用 this.Handle

IntPtr hWnd = new WindowInteropHelper(GetWindow(this)).EnsureHandle();

所以它有效

單擊此處查看圖像(我還沒有代表)

但我想知道我是否可以用任何顏色來做這個……

Windows 10 和 11 有一個設置可以在設置中打開任何標題欄顏色,但我想知道我是否可以獲取 hWnd 並在每個應用程序中自己執行此操作,因為我可以將它變成黑色,為什么不使用任何其他顏色?

人們一直想看看我是如何讓它發揮作用的

把這個放在你的 class 下

[DllImport("DwmApi")] 
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, int[] attrValue, int attrSize);
const int DWWMA_CAPTION_COLOR = 35;


在任何 function(主要?)

像這樣獲取 window 句柄或 hwnd

IntPtr hWnd = new WindowInteropHelper(this).EnsureHandle();

然后定義顏色

int[] colorstr = new int[]{0xFF00FF};

0x 字符串的格式如下 0xRRGGBB 將字母替換為相應的值

然后讓它發生

DwmSetWindowAttribute(hWnd, DWWMA_CAPTION_COLOR, colorstr, 4);

注意:這僅適用於 windows 11


如果你懶惰,這里是完整版本

class MainWindow : Window
{

     [DllImport("DwmApi")] 
     private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, int[] attrValue, int attrSize);
     const int DWWMA_CAPTION_COLOR = 35;

     public MainWindow()
     {
          IntPtr hWnd = new WindowInteropHelper(this).EnsureHandle();
          int[] colorstr = new int[]{0xFF00FF};
          DwmSetWindowAttribute(hWnd, DWWMA_CAPTION_COLOR, colorstr, 4);
     }

}

哦,是的,導入這些

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;

編輯:顏色采用 BGR 格式,因此請確保顏色為藍色、綠色、紅色而不是紅色、綠色和藍色

你可以采取三種方法來解決這個問題......

1:困難的,你要做的是修改由Windows控制的非客戶區。

缺點是這只能通過 Windows 的 kernel 方法實現,而您使用的是 .NET,而不是 C/C++。 但是,我們可以P/Invoke 事實上,整個 Windows 表單 UI 和控制台應用程序 I/O 方法都作為包裝器提供,這些包裝器在后台進行系統調用。 因此,如 MSDN 中所述,完全可以使用P/Invoke訪問設置非客戶區所需的那些方法。

如前所述,這是一個“比目前需要的更難”的解決方案。


2:更簡單的,用XAML制作自己的標題欄

幸運的是,從 .NET 4.5 開始,您可以使用WindowChrome class 來根據您的需要調整非客戶區,您可以將WindowStyle設置為 none 並可以向您的應用程序添加自定義標題欄,它可以具有您喜歡的任何顏色,並且可以看起來像 Windows 或其他操作系統的標題欄

要開始使用WindowChrome ,您可以閱讀以下文章WindowChrome ClassExperiments with WindowChrome go

<Window [...]>下方代碼的基礎

<WindowChrome.WindowChrome>
        <WindowChrome 
        CaptionHeight="0"
        ResizeBorderThickness="5" />
    </WindowChrome.WindowChrome>

並確保添加WindowStyle="None"以刪除標題欄及其組件


3:最簡單的,使用第三方庫。

您還可以使用第三方組件系統,例如MahApps.Metro for WPF。據我所知,您應該能夠自定義標題欄顏色。

最終結果可能如下所示:

MahApps.Metro

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM