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 小时前
内存优化有哪些方法?
c#
蓝胖子不会敲代码2 小时前
跟着AI学习C# Day14
开发语言·学习·c#
爱吃煎蛋的小新3 小时前
WPF入门 #1 WPF布局基础
笔记·学习·c#·wpf
夜空晚星灿烂15 小时前
C# 关于LINQ语法和类型的使用
windows·c#·linq
code bean15 小时前
【C#】 C#中 nameof 和 ToString () 的用法与区别详解
android·java·c#
普通网友20 小时前
C# 中委托和事件的深度剖析与应用场景
java·算法·c#
钢铁男儿1 天前
C#结构体性能暴击指南:从内存陷阱到零损耗实战
开发语言·c#
MZZ骏马1 天前
C#接受文件
开发语言·c#
ou.cs1 天前
wpf 控件开发中,OnApplyTemplate 和 OnContentRendered区别
c#·.net·wpf
忧郁的蛋~1 天前
.NET Core 实现缓存的预热的方式
缓存·c#·.net·.netcore