5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C#でWin10の壁紙を変更する方法

Last updated at Posted at 2020-04-27

概要

以前、デスクトップの背景画像をC#から変えたくなったことがあるので備忘録としてメモ

検証環境

  • Windows 10 Pro
  • Visual Studio Enterprise 2019
  • .NET Core 3.1

ソースコード

初めにWin32 APIを読み込むためにDLLImportuser32.dllを呼びましょう

Program.cs

using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace TestProject
{
    class Program
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool SystemParametersInfo(uint uAction, uint uParam, string lpvParam, uint fuWinIni);

        private const uint SPI_SETDESKWALLPAPER = 0x0014;
        private const uint SPIF_UPDATEINIFILE = 1;
        private const uint SPIF_SENDWININICHANGE = 2;

        // 壁紙にしたい画像のパス
        private readonly static string exefolder = Directory.GetCurrentDirectory();
        private static readonly StringBuilder img = new StringBuilder(@$"{exefolder}\test.png");

        static void Main(string[] args)
        {
            SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, img.ToString(), SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
        }
    }
}

SystemParametersInfoの引数の詳細についてはSystemParametersInfoA functionに記載されています

Note Windows 8 以降のOSは画像を指定する際に絶対パスを指定しなければいけないらしい(すいません確認とれてません)

参考

SystemParametersInfoA function

5
3
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
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?