C#用winapi改变桌面壁纸

时间:2016-01-23 09:46:41

标签: c# wallpaper

我想更改桌面壁纸。为此,我已经编写了以下代码,但它不起作用:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Win32;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Background bg = new Background();
        bg.DoSomething();  
    }
}

class Background
{
    [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool SystemParametersInfo(uint uiAction, uint uiParam,
        string pvParam, uint fWinIni);

    private const uint SPI_SETDESKWALLPAPER = 0x0014;
    private const uint SPIF_UPDATEINIFILE = 0x01;
    private const uint SPIF_SENDWININICHANGE = 0x02;

    private string pathtofiles;
    private string[] filename;
    private int count, o, ui;
    private Object thisLock = new Object();

    public Background()
    {
        pathtofiles = @"C:\Users\Mine\Desktop\Test\";
        filename = new string[150];
        count = 0;
        o = 0;
        ui = 0;
    }

    public void DoSomething()
    {
        System.IO.DirectoryInfo ParentDirectory = new System.IO.DirectoryInfo(@"C:\Users\Mine\Desktop\Test\");
        foreach (System.IO.FileInfo f in ParentDirectory.GetFiles())
        {

            if (f.Name.EndsWith(".jpg"))
            {
                filename[count] = f.Name;
                count++;
            }
            if (f.Name.EndsWith(".png"))
            {
                filename[count] = f.Name;
                count++;
            }
            if (f.Name.EndsWith(".bmp"))
            {
                filename[count] = f.Name;
                count++;
            }
        }

        count = 0;

        foreach (string s in filename)
        {
            if (System.IO.File.Exists(pathtofiles + s))
            {
                System.IO.Stream BitmapStream = System.IO.File.Open(pathtofiles + s, System.IO.FileMode.Open);

                System.Drawing.Image img = System.Drawing.Image.FromStream(BitmapStream);
                System.Drawing.Bitmap mBitmap = new System.Drawing.Bitmap(img);
                mBitmap.Save(@"C:\Users\Mine\Desktop\BmpSpeichern\00" + o + ".bmp");
                o++;


            }
        }
            System.IO.DirectoryInfo ParentDirectory2 = new System.IO.DirectoryInfo(@"C:\Users\Mine\Desktop\BmpSpeichern\");
            foreach (System.IO.FileInfo f in ParentDirectory2.GetFiles())
            {

                if (f.Name.EndsWith(".bmp"))
                {
                    filename[count] = f.Name;
                    count++;
                }
            }

            if (!SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, @"C:\Users\Mine\Desktop\BmpSpeichern\" + filename[ui],
                        SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE))
            {
                throw new Win32Exception();
            }
        }
    }   
}

首先,我将图像文件复制到另一个目录,然后将它们转换为.bmp数据类型。完美工作,但调用WINAPI函数SystemParametersInfo无法更改壁纸。 我做错了什么?

0 个答案:

没有答案