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

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

这里发布获取与设置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;

/// <summary>

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

/// </summary>

/// <returns></returns>

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

}

/// <summary>

/// 设置Windows背景图片

/// </summary>

/// <param name="imgFile"></param>

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 代码格式

cs 复制代码
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;

        /// <summary>
        /// 获取Windows背景图片(地址)
        /// </summary>
        /// <returns></returns>
        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
        }

        /// <summary>
        /// 设置Windows背景图片
        /// </summary>
        /// <param name="imgFile"></param>
        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
        }
    }
}
相关推荐
Java Fans1 小时前
C# 中串口读取问题及解决方案
开发语言·c#
盛派网络小助手1 小时前
微信 SDK 更新 Sample,NCF 文档和模板更新,更多更新日志,欢迎解锁
开发语言·人工智能·后端·架构·c#
码农君莫笑1 小时前
信管通低代码信息管理系统应用平台
linux·数据库·windows·低代码·c#·.net·visual studio
鲤籽鲲2 小时前
C# Random 随机数 全面解析
android·java·c#
fkdw5 小时前
C# Newtonsoft.Json 反序列化派生类数据丢失问题
c#·json
浅尝辄止;7 小时前
C# 异步编程
c#
ou.cs11 小时前
c# 实现一个简单的异常日志记录(异常迭代+分片+定时清理)+AOP Rougamo全局注入
c#
一只小小汤圆13 小时前
编译笔记:vs 中 正在从以下位置***加载符号 C# 中捕获C/C++抛出的异常
c++·c#