Change Desktop Wallpaper
-
@mrjj I can manual change the wallpaper. That's no problem. They removed the clock because the Microsoft right handle doesn't work for them, so everyone who can see the clock and change it, too. Some students did that and that way the whole account synchronisation was broken. Since that the clock was removed.
I tried to write the clock on the image at my home computer, but i get some errors for this step, too:
QPixmap: Must construct a QGuiApplication before a QPixmap -
@QT-static-prgm said:
Must construct a QGuiApplication
If you do have
QApplication app( argc, argv );then you must likely have constructed the Pixmap as global before main.
and hence its created before "app"
-
@mrjj It works now :D (on my home pc)
#pragma warning(disable: 4100) #include <QGuiApplication> #include <QString> #include <QDebug> #include <QPixmap> #include <QTime> #include <QPainter> #include <Windows.h> #include "shobjidl.h" inline int setwallpaper(QString filePath) { return SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (PVOID)filePath.utf16(), SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE); } void setWallpaperAdvanced(QString filePath) { HRESULT hr = CoInitialize(NULL); IDesktopWallpaper *pDesktopWallpaper = NULL; hr = CoCreateInstance(__uuidof(DesktopWallpaper), NULL, CLSCTX_ALL, IID_PPV_ARGS(&pDesktopWallpaper)); if (FAILED(hr)) { qDebug() << "error"; } else { pDesktopWallpaper->SetWallpaper(NULL, filePath.toStdWString().c_str()); } } void drawTime(QPixmap &image, int posX, int posY, QColor color) { QPainter painter(&image); painter.setPen(color); QFont font; font.setBold(true); painter.setFont(font); QTime time = QTime::currentTime(); QDate date = QDate::currentDate(); QString timeText = time.toString("hh:mm"); QString dateText = date.toString(Qt::DefaultLocaleShortDate); painter.drawText(posX+15,posY,timeText); painter.drawText(posX,posY+15,dateText); } int main(int argc, char **argv) { QGuiApplication a(argc, argv); QString originalFilePath("D:\\workspaces\\Qt_default\\WallpaperApp\\wallpaper.jpg"); QString manipulatedFilePath("D:\\workspaces\\Qt_default\\WallpaperApp\\wallpaper_temp.jpg"); QTime time; while(true) { time = QTime::currentTime(); if(time.second() == 0) { QPixmap image(originalFilePath); drawTime(image, image.width()-75, image.height()-150, Qt::white); image.save(manipulatedFilePath); setWallpaperAdvanced(manipulatedFilePath); qDebug() << "changed"; } Sleep(800); } return 0; }
I was just wondering if there is a good way to stop the program. Maybe a tray icon that has an close option. Any ideas??
-
@QT-static-prgm I have tried your code but I got error.
error: C2065: 'IDesktopWallpaper' : undeclared identifier
error: C2065: 'DesktopWallpaper' : undeclared identifier
error: C2065: 'pDesktopWallpaper' : undeclared identifier
error: C2660: 'CoCreateInstance' : function does not take 3 argumentsbeside these I have a question.
What is the file shobjidl.h that you have included? -
@mKing142 said in Change Desktop Wallpaper:
shobjidl.h
This a native win SDK include
https://msdn.microsoft.com/en-us/library/windows/desktop/dd391719(v=vs.85).aspx
Its needed to know some of the types.Note that with the mingw compiler it would not compile but with visual studio it did.
Note2:
You might need
LIBS += -lOle32
LIBS += User32.lib
LIBS += Ole32.lib
in the .pro file. -
@mrjj Thanks for your quick reply.
Now am able to understand about shobjidl.h file.I am using MSVC 2013 compiler.
I have added
LIBS += Ole32.lib
in .pro file but still I am getting the error. But if I comment this by pragma i.e. #pragma comment( lib, "Ole32.lib" ) then its compiling. So I just want to know that do I need to add this file somewhere in project settings also. -
@mKing142 said in Change Desktop Wallpaper:
#pragma comment( lib, "Ole32.lib" )
No that should be enough.
as far as i know its the same as
Configuration Properties -> Linker -> Input -> Additional Dependencies.
(and add the dll/lib there)Basically we the linker where all the code that goes with the shobjidl.h are.
Normally you would have a cpp file ( the h file just declare ) but in this case its
inside a windows DLL ( ole32 it seems) so if it links, then nothing more is needed. -
Thanks @hskoglund
I always fiddle around with the syntax. :) -
@mrjj Thanks for your comment. I have added the Olib32 in Lib section and tried it. Now the code is building.
But am getting error in CoCreateInstance. The hex code returned by CoCreateInstance is 0x80040154. I have googled this and came to know that the error is "Class not registered". Now am confused.
Do you have any idea regarding this error?