C#,获取与设置Windows背景图片的源代码

C#,获取与设置Windows背景图片的源代码_第1张图片

为了满足孩子们个性化桌面的需求。

这里发布获取与设置Windows背景图片的源代码。

1 文本格式

using System;
using System.IO;
using System.Data;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Legalsoft.Truffer.Application
{
    public partial class Form1 : Form
    {
#if __STRING_BUILDER__
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern int SystemParametersInfo(int uAction, int uParam, StringBuilder lpvParam, int fuWinIni);
#else
        [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
        public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
#endif
        private const int SPI_GETDESKWALLPAPER = 0x0073;

        ///


        /// 获取Windows背景图片(地址)
        ///

        ///
        public string GetWindowsBackgroundImage()
        {
#if __STRING_BUILDER__
            StringBuilder sb = new StringBuilder(300);
            SystemParametersInfo(SPI_GETDESKWALLPAPER, 300, sb, 0);
            return sb.ToString();
#else
            char[] sc = new char[300];
            string ss = new string(sc);
            SystemParametersInfo(SPI_GETDESKWALLPAPER, 300, ss, 0);
            return ss;
#endif
        }

        ///


        /// 设置Windows背景图片
        ///

        ///
        public void SetWindowsBackgroundImage(string imgFile)
        {
            Image image = Image.FromFile(imgFile);
            FileInfo fileInfo = new FileInfo(imgFile);
            string backFile = Path.Combine(fileInfo.DirectoryName, fileInfo.Name, @".bmp");
            image.Save(backFile, System.Drawing.Imaging.ImageFormat.Bmp);
#if __STRING_BUILDER__
            StringBuilder sb = new StringBuilder();
            sb.Append(backFile);
            SystemParametersInfo(20, 0, sb, 0x2);
#else
            SystemParametersInfo(20, 0, backFile, 0x2);
#endif
        }
    }
}
 

 ——————————————————————

POWER BY 315SOFT.COM &
TRUFFER.CN

2 代码格式

using System;
using System.IO;
using System.Data;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Legalsoft.Truffer.Application
{
    public partial class Form1 : Form
    {
#if __STRING_BUILDER__
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern int SystemParametersInfo(int uAction, int uParam, StringBuilder lpvParam, int fuWinIni);
#else
        [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
        public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
#endif
        private const int SPI_GETDESKWALLPAPER = 0x0073;

        /// 
        /// 获取Windows背景图片(地址)
        /// 
        /// 
        public string GetWindowsBackgroundImage()
        {
#if __STRING_BUILDER__
            StringBuilder sb = new StringBuilder(300);
            SystemParametersInfo(SPI_GETDESKWALLPAPER, 300, sb, 0);
            return sb.ToString();
#else
            char[] sc = new char[300];
            string ss = new string(sc);
            SystemParametersInfo(SPI_GETDESKWALLPAPER, 300, ss, 0);
            return ss;
#endif
        }

        /// 
        /// 设置Windows背景图片
        /// 
        /// 
        public void SetWindowsBackgroundImage(string imgFile)
        {
            Image image = Image.FromFile(imgFile);
            FileInfo fileInfo = new FileInfo(imgFile);
            string backFile = Path.Combine(fileInfo.DirectoryName, fileInfo.Name, @".bmp");
            image.Save(backFile, System.Drawing.Imaging.ImageFormat.Bmp);
#if __STRING_BUILDER__
            StringBuilder sb = new StringBuilder();
            sb.Append(backFile);
            SystemParametersInfo(20, 0, sb, 0x2);
#else
            SystemParametersInfo(20, 0, backFile, 0x2);
#endif
        }
    }
}

 

你可能感兴趣的:(C#实用代码,Coding,Recipes,c#,背景图片,Windows桌面)