Change wallpaper programmatically using c++ and windows api

后端 未结 3 1096
灰色年华
灰色年华 2021-02-03 13:40

I\'ve been trying to write an application, using Qt and mingw32, to download images and set them as the background Wallpaper. I have read several articles online about how to do

相关标签:
3条回答
  • 2021-02-03 14:18

    You cn use SetTimer to trigger a change.

    #define STRICT 1 
    #include <windows.h>
    #include <iostream.h>
    
    VOID CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime) 
    {
    
      LPWSTR wallpaper_file = L"C:\\Wallpapers\\wallpaper.png";
      int return_value = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, wallpaper_file, SPIF_UPDATEINIFILE);
    
    
      cout << "Programmatically change the desktop wallpaper periodically: " << dwTime << '\n';
      cout.flush();
    }
    
    int main(int argc, char *argv[], char *envp[]) 
    {
        int Counter=0;
        MSG Msg;
    
        UINT TimerId = SetTimer(NULL, 0, 2000, &TimerProc); //2000 milliseconds
    
        cout << "TimerId: " << TimerId << '\n';
       if (!TimerId)
        return 16;
    
       while (GetMessage(&Msg, NULL, 0, 0)) 
       {
            ++Counter;
            if (Msg.message == WM_TIMER)
            cout << "Counter: " << Counter << "; timer message\n";
            else
            cout << "Counter: " << Counter << "; message: " << Msg.message << '\n';
            DispatchMessage(&Msg);
        }
    
       KillTimer(NULL, TimerId);
    return 0;
    }
    
    0 讨论(0)
  • 2021-02-03 14:22
    "C:\Documents and Settings\Owner\My Documents\Wallpapers\wallpaper.png";
    

    shouldn't this be:

    "C:\\Documents and Settings\\Owner\\My Documents\\Wallpapers\\wallpaper.png";
    
    0 讨论(0)
  • 2021-02-03 14:41

    It could be that SystemParametersInfo is expecting an LPWSTR (a pointer to wchar_t).

    Try this:

    LPWSTR test = L"C:\\Documents and Settings\\Owner\\My Documents\\Wallpapers\\wallpaper.png";
    
    result = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, test, SPIF_UPDATEINIFILE);
    

    If this works (try it with a few different files just to make sure), you'll need to convert your char * to a LPWSTR. I'm not sure if Qt offers these services, but one function that may help is MultiByteToWideChar.

    0 讨论(0)
提交回复
热议问题