Because it is based on Win32 API, I pointed out in the post that you don’t need to migrate to WindowsAppSDK or XAML island, you only need
-
This method is exactly for Win32 applications
-
This method is fully compatible with earlier versions of Windows (although you claim to have ended support for it. But even so, for Windows 8, or earlier versions of Windows, the API will not work without any compatibility issues, This change will be ignored.
-
This method also does not require complex customization of the window border, and only needs to add a few lines of code to run perfectly.
-
This approach also eliminates the need to migrate to the Windows App SDK or use a XAML island, which is currently the cheapest approach.
-
This is Microsoft’s official method for Win32 applications to adapt to dark title bars.
To enable the dark title bar, call a Desktop Windows Manager (DWM) function called DwmSetWindowAttribute on your top-level window, using the window attribute DWMWA_USE_IMMERSIVE_DARK_MODE. (DWM renders attributes for a window.)
The following examples assume you have a window with with a standard title bar, like the one created by this code.
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, 0,
CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
First, you need to import the DWM API, like this.
#include <dwmapi.h>
After passing hWnd (the handle to the window you want to change) as your first parameter, you need to pass in DWMA_USE_IMMERSIVE_DARK_MODE as the dwAttribute parameter. This is a constant in the DWM API that lets the Windows frame be drawn in Dark mode colors when the Dark mode system setting is enabled. If you switch to Light mode, you will have to change DWMA_USE_IMMERSIVE_DARK_MODE from 20 to 0 for the title bar to be drawn in light mode colors.
The pvAttribute parameter points to a value of type BOOL (which is why you made the BOOL value earlier). You need pvAttribute to be TRUE to honor Dark mode for the window. If pvAttribute is FALSE, the window will use Light Mode.
#ifndef DWMWA_USE_IMMERSIVE_DARK_MODE
#define DWMWA_USE_IMMERSIVE_DARK_MODE 20
#endif
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
BOOL value = TRUE;
::smile:wmSetWindowAttribute(hWnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &value, sizeof(value));
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
Lastly, cbAttribute needs to have the size of the attribute being set in pvAttribute. To do easily do this, we pass in sizeof(value).
Your code to draw a dark windows title bar should look like this.
More Document:
https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/apply-windows-themes
