7

Qtとmingw32を使用して、画像をダウンロードして背景の壁紙として設定するアプリケーションを作成しようとしています。VB と C# でこれを行う方法と、C++ である程度行う方法について、オンラインでいくつかの記事を読みました。SystemParametersInfo現在、すべて正しい引数 (コンパイラ エラーなし) と思われるものを使用して を呼び出していますが、失敗します。シンバルの大きなクラッシュはなく、ただ0戻ってきただけです。GetLastError()同様に啓発的な を返します0

以下は私が使用しているコードです (わずかに変更された形式であるため、オブジェクトの内部を表示する必要はありません)。

#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;
    }
}
4

3 に答える 3

9

が(へのポインタ) をSystemParametersInfo期待している可能性があります。LPWSTRwchar_t

これを試して:

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.

于 2010-07-26T01:20:14.200 に答える
2
"C:\Documents and Settings\Owner\My Documents\Wallpapers\wallpaper.png";

これはすべきではありません:

"C:\\Documents and Settings\\Owner\\My Documents\\Wallpapers\\wallpaper.png";
于 2010-07-26T01:08:34.973 に答える
-1

SetTimer変更をトリガーするために使用できます。

#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;
}
于 2012-08-31T09:15:55.503 に答える