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
        }
    }
}
相关推荐
勘察加熊人2 小时前
forms实现俄罗斯方块
c#
艾妮艾妮5 小时前
C语言常见3种排序
java·c语言·开发语言·c++·算法·c#·排序算法
小码编匠6 小时前
.NET 验证码生成神器基于 SkiaSharp 的高性能方案
后端·c#·.net
专注VB编程开发20年6 小时前
Aspose.words,Aspose.cells,vb.net,c#加载许可证,生成操作选择:嵌入的资源
c#·word·.net·vb.net
andy55206 小时前
.NET 使用 WMQ 连接Queue 发送 message 实例
xml·c#·wmq·c# 连接wmq·发送消息到wmq
破罐子不摔6 小时前
【C#使用S7.NET库读取和写入西门子PLC变量】
java·c#·.net
杰尼杰尼丶6 小时前
Winform MQTT客户端连接方式
c#·winform
weixin_307779138 小时前
C#实现HiveQL建表语句中特殊数据类型的包裹
开发语言·数据仓库·hive·c#
lixy5798 小时前
C# WPF 命令机制(关闭CanExecute自动触发,改手动)
c#·wpf
天地长久.8 小时前
C# N层架构和解耦
c#·解耦·多层架构