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
        }
    }
}
相关推荐
△曉風殘月〆5 小时前
WPF MVVM入门系列教程(二、依赖属性)
c#·wpf·mvvm
逐·風7 小时前
unity关于自定义渲染、内存管理、性能调优、复杂物理模拟、并行计算以及插件开发
前端·unity·c#
m0_6569747410 小时前
C#中的集合类及其使用
开发语言·c#
九鼎科技-Leo10 小时前
了解 .NET 运行时与 .NET 框架:基础概念与相互关系
windows·c#·.net
九鼎科技-Leo12 小时前
什么是 ASP.NET Core?与 ASP.NET MVC 有什么区别?
windows·后端·c#·asp.net·mvc·.net
.net开发12 小时前
WPF怎么通过RestSharp向后端发请求
前端·c#·.net·wpf
小乖兽技术12 小时前
C#与C++交互开发系列(二十):跨进程通信之共享内存(Shared Memory)
c++·c#·交互·ipc
幼儿园园霸柒柒13 小时前
第七章: 7.3求一个3*3的整型矩阵对角线元素之和
c语言·c++·算法·矩阵·c#·1024程序员节
平凡シンプル15 小时前
C# EF 使用
c#
丁德双16 小时前
winform 加载 office excel 插入QRCode图片如何设定位置
c#·excel