Draw on the windows 11 wallpaper in C++/Qt

I’m trying to use Qt and C++ to draw a window on the windows desktop, between the icons and the wallpaper, to make that window into a wallpaper.

This simplified version of my program makes a window that’s inactive, always in the background, and that does not catch inputs, at the size and position of the first screen it finds:

#include <QApplication>
#include <QScreen>
#include <QWindow>
#include <Windows.h>
#include <QPainter>
#include <QMainWindow>

class MainWindow : public QMainWindow {
        Q_OBJECT
    public:
        MainWindow(QRect geometry, QWidget* parent = nullptr): QMainWindow(parent) {
            setGeometry(geometry);
            setWindowFlags(
                Qt::FramelessWindowHint |
                Qt::WindowTransparentForInput |
                Qt::WindowStaysOnBottomHint
            );
            HWND hWnd = (HWND)winId();
            LONG_PTR exStyle = GetWindowLongPtr(hWnd, GWL_EXSTYLE);
            SetWindowLongPtr(hWnd, GWL_EXSTYLE, exStyle | WS_EX_NOACTIVATE);
        }
        ~MainWindow(){}

    protected:
        void paintEvent(QPaintEvent*) {
            QPainter painter(this);
            painter.fillRect(geometry(), QColor(0, 50, 120));
        }
};

int main(int argc, char* argv[]) {
    QApplication a(argc, argv);
    MainWindow w(a.screens().at(0)->geometry());
    w.show();
    return a.exec();
}

The issue here is that despite being under all windows and letting inputs go through it (including right clicks on the desktop), it’s still above the desktop icons.

My main sources for this were:

  • Draw on Windows 10 wallpaper in C++
  • How to create a QWidget with a HWND as parent?
  • QWidget::createWindowContainer
  • QWindow::fromWinId

The first shows how to get a HWND to the wallpaper.

The second shows how to use a HWND as a parent for a QWidget, but it’s for Qt <5, and I’m using Qt 6.6.1.

It used QWidget::create, which is now deprecated and the Qt docs say it should be replaced with QWidget::createWindowContainer and QWindow::fromWinId. However I don’t really understand the ins and outs of Qt, and I’m really out of my depth when it comes to the Windows API. I barely understand what a HWND actually is.

With the help of a friendly robot, I was able to come up with this:

// Both functions are from the first link
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
    HWND p = FindWindowEx(hwnd, NULL, L"SHELLDLL_DefView", NULL);
    HWND* ret = (HWND*)lParam;

    if (p)
    {
        // Gets the WorkerW Window after the current one.
        *ret = FindWindowEx(NULL, hwnd, L"WorkerW", NULL);
    }
    return true;
}

HWND get_wallpaper_window()
{
    // Fetch the Progman window
    HWND progman = FindWindow(L"ProgMan", NULL);
    // progman = FindWindow(L"MozillaWindowClass", 0);
    // Send 0x052C to Progman. This message directs Progman to spawn a
    // WorkerW behind the desktop icons. If it is already there, nothing
    // happens.
    SendMessageTimeout(progman, 0x052C, 0, 0, SMTO_NORMAL, 1000, nullptr);
    // We enumerate all Windows, until we find one that has the SHELLDLL_DefView
    // as a child.
    // If we found that window, we take its next sibling and assign it to workerw.
    HWND wallpaper_hwnd = nullptr;
    EnumWindows(EnumWindowsProc, (LPARAM)&wallpaper_hwnd);
    // Return the handle you're looking for.
    return wallpaper_hwnd;
}

// My code
int main(int argc, char *argv[]) {
    QApplication a(argc, argv);

    HWND wallpaper_hwnd = get_wallpaper_window();
    QWindow* wallpaper_window = QWindow::fromWinId((WId)wallpaper_hwnd);
    QWidget* wallpaper_widget = QWidget::createWindowContainer(wallpaper_window);

    MainWindow w(a.screens().at(0)->geometry(), wallpaper_widget);
    w.show();
    return a.exec();
}

It’s unclear what’s actually happening. Visually, nothing. The window just doesn’t show up anymore, but it looks like the program doesn’t crash.

I have high suspicions that I’m just using the HWND from get_wallpaper_window wrong.

Additional info:

  • wallpaper_window‘s geometry (x, y, width, height) is 1200, 720, 160, 160
  • wallpaper_widget‘s geometry is 0, 0, 640, 480
  • My display’s geometry is 0, 0, 1600, 2560
  • In the first version of the code, without the wallpaper as a parent, sometimes when I click on the window, the desktop icons are briefly visible over the window.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật