Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dark title bar when dark themed #761

Open
astrohart opened this issue Jan 13, 2025 · 0 comments
Open

Dark title bar when dark themed #761

astrohart opened this issue Jan 13, 2025 · 0 comments

Comments

@astrohart
Copy link

image
Figure 1. White title bar in dark mode.

It would be great if the titlebars of the windows on Windows 10+ could be black as well, when the app is dark themed. Dark theme looks great but I have these white titlebars sticking out everywhere which is garish, IMHO.

On Windows 10 build 1809+ and Windows 11,, the system provides an immersive dark mode feature, but Qt does not natively enable dark title bars. You can use the Windows API to force dark mode.

Use Windows API Functions
Leverage the following APIs:

  • DwmSetWindowAttribute: Sets the dark mode attribute for the window.
  • SetWindowTheme: Ensures the window uses a specific theme like Explorer Dark Mode.

Implementation

Create a helper function in your Qt application that uses these APIs.

#include <QMainWindow>
#include <QWindow>
#include <QDebug>
#include <windows.h>
#include <dwmapi.h>

#pragma comment(lib, "Dwmapi.lib")

namespace DarkModeHelper {

    enum DWMWINDOWATTRIBUTE {
        DWMWA_USE_IMMERSIVE_DARK_MODE = 20
    };

    bool enableDarkTitleBar(HWND hwnd, bool enabled = true) {
        if (!hwnd) {
            qDebug() << "Invalid window handle.";
            return false;
        }

        BOOL darkMode = enabled ? TRUE : FALSE;

        // Set immersive dark mode
        HRESULT hr = DwmSetWindowAttribute(
            hwnd,
            DWMWA_USE_IMMERSIVE_DARK_MODE,
            &darkMode,
            sizeof(darkMode)
        );

        if (FAILED(hr)) {
            qDebug() << "Failed to set dark mode. Error code:" << hr;
            return false;
        }

        return true;
    }
}

Step 2: Apply to Qt Windows
You can apply the enableDarkTitleBar function to your Qt windows by passing their native HWND handle. Use QWindow::winId() to get the native window handle.

Example Usage
Override the showEvent of your QMainWindow or use a helper function to apply dark mode whenever a window is shown.

#include "DarkModeHelper.h"

class DarkMainWindow : public QMainWindow {
    Q_OBJECT

public:
    DarkMainWindow(QWidget *parent = nullptr) : QMainWindow(parent) {
        // Your initialization code
    }

protected:
    void showEvent(QShowEvent *event) override {
        QMainWindow::showEvent(event);

        // Apply dark title bar
        HWND hwnd = reinterpret_cast<HWND>(this->windowHandle()->winId());
        if (!DarkModeHelper::enableDarkTitleBar(hwnd)) {
            qDebug() << "Failed to enable dark title bar.";
        }
    }
};

Considerations

  1. Compatibility:
  • The DwmSetWindowAttribute function is supported on Windows 10 (1809+) or later.
  • Ensure your application handles older Windows versions gracefully.
  1. High DPI Awareness:
  • Add DPI-awareness settings to your manifest file to ensure proper scaling.
  1. Performance:
  • Applying dark mode dynamically might slightly affect performance. You can enable it for the best results once the window is created.
  1. Consistent Styling:
  • Ensure widget themes and title bars match for a seamless user experience.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant