2014-09-10 95 views
0

我正在運行一個PowerShell腳本來將背景更改爲某一組顏色。我想在不重新啓動的情況下執行此操作,但不幸的是,無法立即在Windows 7/8平臺上使更改立即生效。我在網上發現了很多解決方案,但我找不到適合自己的解決方案。我想這可能與設置SystemParametersInfo有關,但我不確定。我已經看到了一些解決方案併爲自己嘗試過,但是我也無法讓它們工作。註冊表項更新只是查找,但更改不會生效,直到我重新啓動後。以下是我迄今爲止,如果有人看到任何我可以做不同的事情,我會感謝幫助!Powershell - 使桌面背景更改立即生效

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() 
+0

您可以在**更新註冊表項之前而不是之前嘗試將'SystemParametersInfo'調用移動到**,因爲這可能會導致系統更新其內部狀態。 – 2014-09-10 22:13:41

+0

@JonathanPotter嗨喬納森,謝謝你的回覆。我發現我不需要使用SystemParametersInfo。我需要弄清楚如何使用SetSysColors。現在編輯我的問題。 – wheatfairies 2014-09-10 22:22:11

+0

如果你想以純色結束,你仍然需要'SystemParametersInfo'清除任何壁紙圖像。 – 2014-09-10 23:21:11

回答

4

的記錄方式來改變系統的顏色是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); 
    } 
} 
+0

是的,我一直在研究這個。是否需要安裝外部庫以導入System.Drawing.Color?我不斷收到無法找到的錯誤。 – wheatfairies 2014-09-11 15:26:59

+0

@wheatfairies我添加了一些代碼。它成功更新桌面。 – arx 2014-09-11 16:35:19

+0

謝謝Arx!這肯定有幫助。我其實只是想通了,所以我會發布我的解決方案:)大多數功勞去鏈接這裏http://gallery.technet.microsoft.com/scriptcenter/Change-window-borderdesktop-609a6fb2 – wheatfairies 2014-09-11 16:43:41

3

昨天是我第一次參加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