弹来弹去跑马灯!

c# 设置桌面壁纸: 只在win10 上测试了,不知道其它系统如何

c# 设置桌面壁纸:

只在win10 上测试了,不知道其它系统如何。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
using Microsoft.Win32;

namespace Wallpaper
{
    static class Window
    {
        [DllImport("user32.dll", EntryPoint = "SetParent")]
        private static extern int SetParent(int hWndChild,int hWndNewParent);
        [DllImport("user32.dll", EntryPoint = "FindWindowA")]
        private static extern IntPtr FindWindowA(string lpClassName, string lpWindowName);
        [DllImport("user32.dll", EntryPoint = "FindWindowExA")]
        private static extern IntPtr FindWindowExA(IntPtr hWndParent, IntPtr hWndChildAfter, string lpszClass, string lpszWindow);
        [DllImport("user32.dll", EntryPoint = "GetClassNameA")]
        private static extern IntPtr GetClassNameA(IntPtr hWnd, IntPtr lpClassName, int nMaxCount);
        [DllImport("user32.dll", EntryPoint = "GetParent")]
        private static extern IntPtr GetParent(IntPtr hWnd);

        public static void SetFather(Form form)
        {
            SetParent((int)form.Handle, GetBackground());
        }

        private static int GetBackground()
        {
            unsafe
            {
                IntPtr background = IntPtr.Zero;
                IntPtr father = FindWindowA("progman", "Program Manager");
                IntPtr workerW = IntPtr.Zero;
                do
                {
                    workerW = FindWindowExA(IntPtr.Zero, workerW, "workerW", null);
                    if (workerW != IntPtr.Zero)
                    {
                        char[] buff = new char[200];
                        IntPtr b = Marshal.UnsafeAddrOfPinnedArrayElement(buff, 0);
                        int ret = (int)GetClassNameA(workerW, b, 400);
                        if (ret == 0) throw new Exception("出错");
                    }
                    if (GetParent(workerW) == father)
                    {
                        background = workerW;
                    }
                } while (workerW != IntPtr.Zero);
                return (int)background;
            }
        }

        [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
        public static extern int SystemParametersInfo(int uAction, int uParam, StringBuilder lpvParam, int fuWinIni);

        public static bool Refresh()
        {
            StringBuilder wallpaper = new StringBuilder(200);
            SystemParametersInfo(0x73, 200, wallpaper, 0);
            int ret = SystemParametersInfo(20, 1, wallpaper, 3);
            if(ret != 0)
            {
                RegistryKey hk = Registry.CurrentUser;
                RegistryKey run = hk.CreateSubKey(@"Control Panel\Desktop\");
                run.SetValue("Wallpaper", wallpaper.ToString());
                return true;
            }
            return false;
        }

    }
}

  

posted @ 2022-10-04 09:21  wgscd  阅读(162)  评论(0)    收藏  举报