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
        }
    }
}
相关推荐
bicijinlian1 小时前
.Net HttpClient 使用代理功能
c#·.net·httpclient·.net httpclient·httpclient 代理
敲代码的 蜡笔小新6 小时前
【行为型之中介者模式】游戏开发实战——Unity复杂系统协调与通信架构的核心秘诀
unity·设计模式·c#·中介者模式
程序猿多布6 小时前
使用Visual Studio将C#程序发布为.exe文件
c#·visual studio
老衲有点帅7 小时前
C#多线程Thread
开发语言·c#
PascalMing9 小时前
C# 通过脚本实现接口
c#·codeanalysis·接口派生
敲代码的 蜡笔小新12 小时前
【行为型之观察者模式】游戏开发实战——Unity事件驱动架构的核心实现策略
观察者模式·unity·设计模式·c#
向宇it12 小时前
【unity游戏开发——编辑器扩展】使用EditorGUI的EditorGUILayout绘制工具类在自定义编辑器窗口绘制各种UI控件
开发语言·ui·unity·c#·编辑器·游戏引擎
FAREWELL0007518 小时前
Unity基础学习(九)输入系统全解析:鼠标、键盘与轴控制
学习·unity·c#·游戏引擎
码观天工20 小时前
【.NET必读】RabbitMQ 4.0+重大变更!C#开发者必须掌握的6大升级要点
c#·rabbitmq·.net·mq
绿龙术士21 小时前
构建现代化WPF应用:数据驱动开发与高级特性解析
c#·wpf