The windows title bar has gone through various changes throughout the years - from the old title bars with user customisable colours to the newer title bars with visual styling support introduced in Windows XP to the DWM (Desktop Window Manager) rendered title bars introduced in Windows Vista. One thing about these changes is that they often explored the concepts the relationship of the title bar to the client area of Windows and who is in control of what the title bar looks like. For example, in Windows XP, the user could only choose between specific title bar styles unless you used the Windows classic theme, in which case you could choose your own title bar colours or even use gradients if you wanted. With the addition of the DWM in Windows Vista, Microsoft explored the idea of extending the title bar into the non client area of Windows - think File Explorer and Microsoft Word for example.
Where we are now
In modern versions of Windows, the title bar only uses the DWM version and allows the user to customise the colours - gone are the glass title bars of Windows Vista and Windows 7, though that being said, Microsoft has been slowly brining back the concept of transparent title bars with features such as acrylic and mica. A question that has very often been asked by developers is the question of how they can control the colours of the title bars in their own applications. This may be so that the title bar can match the look and feel of its window's content or even to add dark mode support - though, if dark mode support is what you're after, this can be easily enabled in Windows 11 and newer versions of Windows 10 in your WPF and Windows Forms applications by following the instructions
here.
How to change title bar colours in Windows Forms and WPF
In Windows 10, Microsoft introduced the ability to change the title bar colours of UWP windows, but no other types of windows. In Windows 11, Microsoft finally introduced the ability to change the colours of normal Win32 title bars, which includes Windows Forms and WPF title bars. The function we'll be using is
DwmSetWindowAttribute from dwmapi.dll. We'll need to use platform invoke to do this. You can easily define the function in
C# like so:
using System.Runtime.InteropServices;
[DllImport("dwmapi.dll", PreserveSig = true)]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
And in VB like this:
Imports System.Runtime.InteropServices
<DllImport("dwmapi.dll", PreserveSig:=True)>
Public Shared Function DwmSetWindowAttribute(hwnd As IntPtr, attr As Integer, ByRef attrValue As Integer, attrSize As Integer) As Integer
End Function
Remember to put the using/imports statements at the top of the file. Once you've defined the function, you can call it from your code to change various DWMWINDOWATTRIBUTEs. Specifically, we want to change '
DWMWA_CAPTION_COLOR' for the background colour, which has a value of 35 or
DWMWA_TEXT_COLOR, which has a value of 36 for the text colour. As for the colour itself, we'll use hexadecimal format for that. If you want a colour picker, Google has one
here. Here is an example of how to give the title bar a red background. Replace ff0000 with the colour you want.
Note: 'System.Windows.Interop.WindowInteropHelper(Me).Handle' assumes that this code is being called from a WPF Window class, if being called from a Windows Forms Form class, you can just use Me.Handle.
C#:
int TitlebarLightColour = 0xFF0000;
DwmSetWindowAttribute(new System.Windows.Interop.WindowInteropHelper(this).Handle, 35, ref TitlebarDarkColour, System.Runtime.InteropServices.Marshal.SizeOf(TitlebarDarkColour));
VB:
Dim TitlebarDarkColour As Integer = &HFF0000
DwmSetWindowAttribute(New Interop.WindowInteropHelper(Me).Handle, 35, TitlebarDarkColour, Runtime.InteropServices.Marshal.SizeOf(TitlebarDarkColour))
If you want to change the text colour rather than the background colour, simply replace 35 with 36 above. This code will change the appropriate colour instantly and is currently only supported on Windows 11.
Tip: Replace 35 with 34 in the code above to change the border colour of the window!
This snippet is available in Codly. Click the appropriate link below to download the snippet. If you don't have Codly, it is available here in the Microsoft Store. Example
Here is an example of our app
QPad, which uses this function to change its title bar colour. A run box is open for comparison - the run box's title bar is using the colour set in Windows colour settings whereas
QPad's title bar is using a custom colour to make the title bar seamlessly fit in with the content of the window:
Comments
Post a Comment