LoginSignup
3
2

More than 5 years have passed since last update.

Windowsスポットライトで表示される画像をデスクトップ背景にする

Last updated at Posted at 2016-12-24

Windowsスポットライトは、Windows起動時にきれいな写真を表示する機能。
この写真を取得し起動時にデスクトップの壁紙として貼り付けるプログラムを作ろう。

スポットライトの画像は「C:\Users(ユーザ名)\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets」に拡張子が消えた状態で置かれる。この中にどういう決まりで画像が追加されているのかは調査中なので、今回は一番サイズの大きい画像を取り出して使う。

VisualStudio2015でC#で作る。C#のWindowsFormApplicationのプロジェクトを作る。

program.csに書き込むコードは以下。

Program.cs
using System;
using System.Text;

namespace WindowsFormsApplication2
{
    static class Program
    {
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern bool SystemParametersInfo(uint uiAction, uint uiParam, StringBuilder pvParam, uint fWinIni);
        const uint SPI_SETDESKWALLPAPER = 20;
        const uint SPIF_UPDATEINIFILE = 1;
        const uint SPIF_SENDWININICHANGE = 2;
        const string WIN_LIVEPHOTOPATH = @"C:\Users\(ユーザ名)\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets";

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            string[] subFolders = System.IO.Directory.GetFiles(WIN_LIVEPHOTOPATH, "*");
            long max = 0;
            string fileName = "";
            foreach (string value in subFolders) {
                System.IO.FileInfo fi = new System.IO.FileInfo(value);
                if (max < fi.Length) {
                    max = fi.Length;
                    fileName = value;
                }
            }
            StringBuilder sb = new StringBuilder(fileName);
            SystemParametersInfo(SPI_SETDESKWALLPAPER, (uint)sb.Length, sb, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
        }
    }
}

SystemParamertersInfoがミソ。StringBuilderで指定したパスの画像をデスクトップの背景にする。

ビルドすると,VisualStudioのプロジェクトの「/Debug/bin」内にexeファイルができる。
これを「C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp」に置く。
これでWindows起動時に実行される。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2