Powershell - 使桌面背景更改立即生效

whe*_*ies 0 windows powershell winapi

我正在运行一个PowerShell脚本来将背景更改为某组颜色.我想这样做而不重新启动,但遗憾的是无法让更改立即在Windows 7/8平台上生效.我在网上找到了很多解决方案,但我找不到适合我的解决方案.我认为它可能与设置SystemParametersInfo有关,但我不确定.我已经看到了一些解决方案,并为自己尝试了,但我也无法让它们工作.注册表项更新只是找到,但更改只有在我重新启动后才会生效.以下是我到目前为止,如果有人看到任何我可以做的不同,我会很感激帮助!

backgroundtest.ps1

Add-Type @"
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;


namespace Background 
{
    public class Setter {
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern int SystemParametersInfo(int uAction, int uParm, string lpvParam, int fuWinIni);
        public const int UpdateIniFile = 0x01;
        public const int SendWinIniChange = 0x02;
        public const int SetDesktopBackground = 20; <# following examples online to set parameters #>

        public static void SetBackground() {
            SystemParametersInfo(SetDesktopBackground, 0, "", UpdateIniFile | SendWinIniChange);
            RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
            key.SetValue(@"WallPaper", 0); <#remove wallpaper#>
            RegistryKey key2 = Registry.CurrentUser.OpenSubKey("Control Panel\\Colors", true);
            key2.SetValue(@"Background", "0 118 163"); <#set background to new color>
            key.Close();
            key2.Close();
         }

    }


}

"@

[Background.Setter]::SetBackground() 
Run Code Online (Sandbox Code Playgroud)

whe*_*ies 7

昨天是我的第一次 Powershell 体验,我对我需要做的事情感到非常迷茫。要将桌面背景改为纯色,首先需要去除墙纸,然后可以使用 SetSysColors 功能立即更改桌面背景。这个链接极大地帮助了我。http://gallery.technet.microsoft.com/scriptcenter/Change-window-borderdesktop-609a6fb2

希望这能像帮助我一样帮助别人。

更新代码

$code = @'
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using Microsoft.Win32;


namespace Background 
{
    public class Setter {
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern int SystemParametersInfo(int uAction, int uParm, string lpvParam, int fuWinIni);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError =true)]
        private static extern int SetSysColors(int cElements, int[] lpaElements, int[] lpRgbValues);
        public const int UpdateIniFile = 0x01;
        public const int SendWinIniChange = 0x02;
        public const int SetDesktopBackground = 0x0014;
        public const int COLOR_DESKTOP = 1;
        public int[] first = {COLOR_DESKTOP};


        public static void RemoveWallPaper() {
        SystemParametersInfo( SetDesktopBackground, 0, "", SendWinIniChange | UpdateIniFile );
        RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
        key.SetValue(@"WallPaper", 0);
        key.Close();
        }

        public static void SetBackground(byte r, byte g, byte b) {
            RemoveWallPaper();
            System.Drawing.Color color= System.Drawing.Color.FromArgb(r,g,b);
            int[] elements = {COLOR_DESKTOP};
            int[] colors = { System.Drawing.ColorTranslator.ToWin32(color) }; 
            SetSysColors(elements.Length, elements, colors);
            RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Colors", true);
            key.SetValue(@"Background", string.Format("{0} {1} {2}", color.R, color.G, color.B));
            key.Close();

        }

    }


}

'@
Add-Type -TypeDefinition $code -ReferencedAssemblies System.Drawing.dll -PassThru
Function Set-OSCDesktopColor
{
    <# Powershell function to remove desktop background and set background to colors we want #>
    Process
    {
        [Background.Setter]::SetBackground(0,118,163)
        return
    }
}

Set-OSCDesktopColor
Run Code Online (Sandbox Code Playgroud)


arx*_*arx 6

记录的更改系统颜色的方法是SetSysColors功能.

这会将WM_SYSCOLORCHANGE消息发送到所有顶级窗口,以通知他们更改.

我已更新您的课程以清除背景并将颜色设置为紫色.它需要复制到PowerShell的东西.请注意,我声明SetSysColors你的方式一次只能改变一种颜色.

public class Setter {
    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern int SystemParametersInfo(int uAction, int uParm, string lpvParam, int fuWinIni);
    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern int SetSysColors(int count, [In] ref int index, [In] ref int colour);

    public const int UpdateIniFile = 0x01;
    public const int SendWinIniChange = 0x02;
    public const int SetDesktopBackground = 20;
    public const int COLOR_BACKGROUND = 1;

    public static void SetBackground() {
        SystemParametersInfo(SetDesktopBackground, 0, "", UpdateIniFile | SendWinIniChange);
        int index = COLOR_BACKGROUND;
        int colour = 0xFF00FF;
        SetSysColors(1, ref index, ref colour);
    }
}
Run Code Online (Sandbox Code Playgroud)