2010-07-26 23 views
6

He intentado escribir una aplicación, usando Qt y mingw32, para descargar imágenes y configurarlas como Fondo de pantalla. He leído varios artículos en línea sobre cómo hacer esto, en VB y C#, y en cierta medida cómo hacerlo en C++. Actualmente estoy llamando al SystemParametersInfo con lo que parecen ser todos los argumentos correctos (sin errores del compilador) y falla. No se produjo un gran choque de platillos, solo un 0 devuelto. GetLastError() devuelve un igualmente esclarecedor 0.Cambiar el fondo de pantalla mediante programación usando C++ y Windows api

A continuación se muestra el código que estoy usando (en una forma ligeramente modificada, para que no tenga que ver las partes internas del objeto).

#include <windows.h> 
#include <iostream> 
#include <QString> 

void setWall() 
{ 
    QString filepath = "C:\\Documents and Settings\\Owner\\My Documents\\Wallpapers\\wallpaper.png"; 
    char path[150]; 
    strcpy(path, currentFilePath.toStdString().c_str()); 
    char *pathp; 
    pathp = path; 

    cout << path; 

    int result; 
    result = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, pathp, SPIF_UPDATEINIFILE); 

    if (result) 
    { 
     cout << "Wallpaper set"; 
    } 
    else 
    { 
     cout << "Wallpaper not set"; 
     cout << "SPI returned" << result; 
    } 
} 
+0

¿Has probado con un archivo de mapa de bits y no png/jpg? –

+0

Probado con png, jpeg, bmp. –

Respuesta

10

Podría ser que SystemParametersInfo espera un LPWSTR (un puntero a wchar_t).

Prueba esto:

LPWSTR test = L"C:\\Documents and Settings\\Owner\\My Documents\\Wallpapers\\wallpaper.png"; 

result = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, test, SPIF_UPDATEINIFILE); 

Si esto funciona (pruebe con un par de archivos diferentes para asegurarse), que necesita para convertir su char * a un LPWSTR. No estoy seguro de si Qt ofrece estos servicios, pero una función que puede ayudar es MultiByteToWideChar.

+1

Sí, eso funciona, acabo de intentar ... – sukru

2
"C:\Documents and Settings\Owner\My Documents\Wallpapers\wallpaper.png"; 

no debe ser esto:

"C:\\Documents and Settings\\Owner\\My Documents\\Wallpapers\\wallpaper.png"; 
+0

Oh cierto. Pero ese no es el error. En el programa real, el QString está debidamente ocupado por una función diferente :) Pero felicitaciones por detectar mi error :) –

0

Usted puede usar SetTimer para activar un cambio.

#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; 
} 
Cuestiones relacionadas