我正在运行一个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() 发布于 2014-09-10 22:50:40
记录在案的改变系统颜色的方法是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);
}
}发布于 2014-09-11 16:47:55
昨天是我的第一次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发布于 2017-01-27 16:21:58
我抓起了惠特仙子的代码,对其进行了更新,并在PowerShell画廊(有属性)发布在:https://www.powershellgallery.com/packages/Set-DesktopBackGround/1.0.0.0/DisplayScript
所以现在每个人都可以输入:Install Set-DesktopBackG圆桌会议
为了得到它。
干杯!
https://stackoverflow.com/questions/25774494
复制相似问题