ソフトウェア開発 Win32プログラミング

壁紙を設定する

戻る


Windowsの壁紙を設定するコードは次の通り。

#define _WIN32_IE 0x0400
#include <windows.h>
#include <wininet.h>
#include <shlobj.h>

BOOL SetWallpaper(LPCTSTR pszBmpFile, BOOL fTile)
{
    HKEY hSubKey;
    TCHAR szValue[32];
    static const TCHAR szSubKey[] = TEXT("Control Panel\\Desktop");
    static const TCHAR tile[] = TEXT("TileWallpaper");
    static const TCHAR format[] = TEXT("%u");

    if (pszBmpFile[0] != 0 && GetFileAttributes(pszBmpFile) == 0xFFFFFFFF)
        return FALSE;

    if (SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (PVOID)pszBmpFile, 
                                SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE))
    {
        wsprintf(szValue, format, fTile ? 1 : 0);
        if (RegOpenKeyEx(HKEY_CURRENT_USER, szSubKey, 0, KEY_SET_VALUE, 
                         &hSubKey) == ERROR_SUCCESS)
        {
            RegSetValueEx(hSubKey, tile, 0, REG_SZ, (BYTE *)szValue, 
                          (lstrlen(szValue) + 1) * sizeof(TCHAR));
            RegCloseKey(hSubKey);
        }
        SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (PVOID)pszBmpFile, 
                                SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
        return TRUE;
    }
    return FALSE;
}

BOOL SetWallpaper2(LPCTSTR pszPath, DWORD dwWPSTYLE_)
{
    BOOL f;
    HRESULT hr;
    LPACTIVEDESKTOP pAD;
    COMPONENTSOPT co;
    WALLPAPEROPT wpo;
#ifndef UNICODE
    WCHAR wszPath[MAX_PATH];
#endif

    if (pszPath[0] != 0 && GetFileAttributes(pszPath) == 0xFFFFFFFF)
        return FALSE;

    if (dwWPSTYLE_ == WPSTYLE_TILE)
        f = SetWallpaper(pszPath, TRUE);
    else
        f = SetWallpaper(pszPath, FALSE);

    if (f)
        return TRUE;

    CoInitialize(NULL);
    hr = CoCreateInstance(&CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER,
                          &IID_IActiveDesktop, (void **)&pAD);
    if (SUCCEEDED(hr))
    {
        co.dwSize = sizeof(COMPONENTSOPT);
        pAD->lpVtbl->GetDesktopItemOptions(pAD, &co, 0);
        if (co.fActiveDesktop == TRUE || MessageBox(NULL, TEXT(
            "この壁紙を表示にするには Active Desktop を有効にする必要があります。\n"
            "有効にしますか"), TEXT("壁紙の設定"), MB_YESNO) == IDYES)
        {
            co.fEnableComponents = TRUE;
            co.fActiveDesktop = TRUE;
            hr = pAD->lpVtbl->SetDesktopItemOptions(pAD, &co, 0);
            if (SUCCEEDED(hr))
            {
#ifdef UNICODE
                hr = pAD->lpVtbl->SetWallpaper(pAD, pszPath, 0);
#else
                wszPath[0] = 0;
                MultiByteToWideChar(CP_ACP, 0, pszPath, -1, wszPath, MAX_PATH);
                hr = pAD->lpVtbl->SetWallpaper(pAD, wszPath, 0);
#endif
                if (SUCCEEDED(hr))
                {
                    wpo.dwSize = sizeof(WALLPAPEROPT);
                    wpo.dwStyle = dwWPSTYLE_;
                    hr = pAD->lpVtbl->SetWallpaperOptions(pAD, &wpo, 0);
                    if (SUCCEEDED(hr))
                    {
                        hr = pAD->lpVtbl->ApplyChanges(pAD, AD_APPLY_ALL);
                    }
                }
            }
        }
        else
            hr = E_FAIL;
        pAD->lpVtbl->Release(pAD);
    }
    CoUninitialize();

    return SUCCEEDED(hr);
}

#ifdef UNITTEST
int main(void)
{
    return SetWallpaper2(TEXT("C:\\a.gif"), 0);
}
#endif

ソース: wall.zip


戻る

©片山博文MZ
katayama.hirofumi.mz@gmail.com

inserted by FC2 system