1、设计代码
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(270, 228);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "确认";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(179, 228);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "下一张";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(80, 228);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 2;
this.button3.Text = "上一张";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(38, 12);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(393, 177);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 3;
this.pictureBox1.TabStop = false;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(470, 276);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "壁纸切换";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
2、事件函数
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using Microsoft.Win32;
namespace WallpaperToggle
{
public partial class Form1 : Form
{
List<string> imagePaths = GetImagePaths("C:\\Users\\Administrator\\Pictures\\Saved Pictures");
int num = 0;
static List<string> GetImagePaths(string folderPath)
{
List<string> imagePaths = new List<string>();
string[] imageExtensions = { ".jpg", ".jpeg", ".png", ".gif", ".bmp" }; // 定义常见的图片文件扩展名
try
{
foreach (string file in Directory.GetFiles(folderPath))
{
string extension = Path.GetExtension(file).ToLower();
if (Array.IndexOf(imageExtensions, extension) >= 0)
{
imagePaths.Add(file);
}
}
}
catch (Exception e)
{
Console.WriteLine($"Error occurred: {e.Message}");
}
return imagePaths;
}
public Form1()
{
InitializeComponent();
pictureBox1.Image = Image.FromFile(imagePaths[num]);
}
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
/// <summary>
/// 更换壁纸
/// </summary>
/// <param name="fileName">壁纸文件的路径</param>
/// <returns>操作结果:true为更换成功,false为更换失败</returns>
public static bool ChangeWallPaper(string fileName)
{
if (string.IsNullOrEmpty(fileName))
{
return false;
}
if (File.Exists(fileName) == false)
{
return false;
}
fileName = Path.GetFullPath(fileName);
var nResult = SystemParametersInfo(20, 1, fileName, 0x1 | 0x2); //更换壁纸
if (nResult == 0)
{
return false;
}
else
{
RegistryKey hk = Registry.CurrentUser;
RegistryKey run = hk.CreateSubKey(@"Control Panel\Desktop\");
run.SetValue("Wallpaper", fileName);
return true;
}
}
private void button1_Click(object sender, EventArgs e)
{
ChangeWallPaper(imagePaths[num]);
}
private void button3_Click(object sender, EventArgs e)
{
if (num > 0)
{
num--;
}
else {
num = imagePaths.Count - 1;
}
pictureBox1.Image = Image.FromFile(imagePaths[num]);
}
private void button2_Click(object sender, EventArgs e)
{
if (num < imagePaths.Count - 1) {
num++;
}
else
{
num = 0;
}
pictureBox1.Image = Image.FromFile(imagePaths[num]);
}
}
}
3、界面展示
