C#使用API设置桌面背景

该文章介绍了如何在C#Windows应用程序中使用API函数SystemParametersInfo来设置桌面背景。用户可以选择图片,程序会判断图片类型,如果不是BMP格式,会自动转换为BMP然后设置为桌面背景。
摘要由CSDN通过智能技术生成

使用API函数SystemParametersInfo,可以将指定的图片设为电脑桌面背景,具体操作如下:

  1. 新建一个Windows应用程序,任意命名即可,默认窗体为Form1。

  1. Form1窗体主要用到的控件及说明如表1所示。

控件类型

控件名称

属性设置

说 明

toolStrip

toolStrip1

程序工具栏

listView

listView1

View属性设为Details

显示选择的图片

openFileDialog

openFileDialog1

Multiselect属性设为true

选择图片

contextMenuStrip

contextMenuStrip1

右键菜单

  1. 实现代码如下:

[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设置桌面背景的教程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值