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, 160wallpaper_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