使用API函数SystemParametersInfo,可以将指定的图片设为电脑桌面背景,具体操作如下:
新建一个Windows应用程序,任意命名即可,默认窗体为Form1。
Form1窗体主要用到的控件及说明如表1所示。
控件类型 | 控件名称 | 属性设置 | 说 明 |
toolStrip | toolStrip1 | 无 | 程序工具栏 |
listView | listView1 | View属性设为Details | 显示选择的图片 |
openFileDialog | openFileDialog1 | Multiselect属性设为true | 选择图片 |
contextMenuStrip | contextMenuStrip1 | 无 | 右键菜单 |
实现代码如下:
[DllImport("user32.dll", EntryPoint = "SystemParametersInfoA")]
static extern Int32 SystemParametersInfo(Int32 uAction, Int32 uParam, string lpvparam, Int32 fuwinIni);
string FPath = listView1.SelectedItems[0].SubItems[1].Text;
//获取指定图片的扩展名
string SFileType = FPath.Substring(FPath.LastIndexOf(".") + 1, (FPath.Length - FPath.LastIndexOf(".") - 1));
//将扩展名转换成小写
SFileType = SFileType.ToLower();
//获取文件名
string SFileName = FPath.Substring(FPath.LastIndexOf("\\") + 1, (FPath.LastIndexOf(".") - FPath.LastIndexOf("\\") - 1));
//如果图片的类型是bmp则调用API中的方法将其设置为桌面背景
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, FPath, 1);
[DllImport("user32.dll", EntryPoint = "SystemParametersInfoA")]
static extern Int32 SystemParametersInfo(Int32 uAction, Int32 uParam, string lpvparam, Int32 fuwinIni);
private const int SPI_SETDESKWALLPAPER = 20;
private void toolStripButton2_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK) //如果选择了图片
{
listView1.Items.Clear(); //清空ListView控件
string[] files = openFileDialog1.FileNames; //获取选择的文件数组
string[] fileinfo = new string[3]; //用于存储文件信息
for (int i = 0; i < files.Length; i++) //遍历文件数组
{
string path = files[i].ToString(); //获取文件路径
//截取文件名
string fileName = path.Substring(path.LastIndexOf("\\") + 1, path.Length - 1 - path.LastIndexOf("\\"));
string filetype = fileName.Substring(fileName.LastIndexOf(".") + 1, fileName.Length - 1 - fileName.LastIndexOf(".")); //截取文件类型
fileinfo[0] = fileName; //文件名
fileinfo[1] = path; //文件路径
fileinfo[2] = filetype; //文件类型
ListViewItem lvi = new ListViewItem(fileinfo); //实例化ListViewItem对象
listView1.Items.Add(lvi); //添加到ListView控件中
}
}
}
private void 设为桌面背景ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
string FPath = listView1.SelectedItems[0].SubItems[1].Text; //单击某一项获取对应的文件路径
//获取指定图片的扩展名
string SFileType = FPath.Substring(FPath.LastIndexOf(".") + 1, (FPath.Length - FPath.LastIndexOf(".") - 1));
SFileType = SFileType.ToLower(); //将扩展名转换成小写
//获取文件名
string SFileName = FPath.Substring(FPath.LastIndexOf("\\") + 1, (FPath.LastIndexOf(".") - FPath.LastIndexOf("\\") - 1));
//如果图片的类型是bmp则调用API中的方法将其设置为桌面背景
if (SFileType == "bmp")
{
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, FPath, 1); //调用SystemParametersInfo函数设置背景
}
else
{
string SystemPath = Environment.SystemDirectory; //获取系统路径
string path = SystemPath + "\\" + SFileName + ".bmp"; //设置转换后的图片路径
FileInfo fi = new FileInfo(path); //创建FileInfo实例
if (fi.Exists) //如果转换后的文件存在
{
fi.Delete(); //将其删除
PictureBox pb = new PictureBox(); //创建PictureBox实例
pb.Image = Image.FromFile(FPath); //设置PictureBox对象的Image属性
//使用Save方法实现格式转换
pb.Image.Save(SystemPath + "\\" + SFileName + ".bmp", ImageFormat.Bmp);
}
else //如果不存在则直接进行格式转换
{
PictureBox pb = new PictureBox();
pb.Image = Image.FromFile(FPath);
pb.Image.Save(SystemPath + "\\" + SFileName + ".bmp", ImageFormat.Bmp);
}
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, path, 1); //调用SystemParametersInfo函数设置背景
}
}
}
以上为C#使用API设置桌面背景的教程