2010-07-26 7 views
6

Ho cercato di scrivere un'applicazione, utilizzando Qt e mingw32, per scaricare immagini e impostarle come sfondo. Ho letto diversi articoli online su come farlo, in VB e C#, e in una certa misura come farlo in C++. Attualmente sto chiamando il SystemParametersInfo con quello che sembra essere tutti gli argomenti corretti (senza errori del compilatore) e fallisce. Nessun grande crash di piatti, solo un 0 restituito. GetLastError() restituisce un ugualmente illuminante 0.Cambia lo sfondo in modo programmatico utilizzando C++ e windows api

Di seguito è riportato il codice che sto utilizzando (in una forma leggermente modificata, quindi non è necessario visualizzare gli interni dell'oggetto).

#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

hai provato con un file bitmap e non png/jpg? –

+0

Provato con png, jpeg, bmp. –

risposta

10

Potrebbe essere che SystemParametersInfo si aspetta un LPWSTR (un puntatore a wchar_t).

Prova questo:

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

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

Se funziona (provare con un paio di file diversi solo per assicurarsi), è necessario convertire il char * ad un LPWSTR. Non sono sicuro che Qt offra questi servizi, ma una funzione che può essere d'aiuto è MultiByteToWideChar.

+1

Sì, quello funziona - Ho appena provato ... – sukru

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

non dovrebbe essere questo:

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

Oh vero. Ma questo non è l'errore. Nel programma attuale, QString è correttamente popolato da una funzione diversa :) Ma complimenti per aver individuato il mio errore :) –

0

È possibile utilizzare SetTimer per attivare una modifica.

#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; 
} 
Problemi correlati