0

I'm trying to change the solid desktop background color, and i'm using a RegistryKey to do that like so :

Dim CD As New ColorDialog
If CD.ShowDialog = DialogResult.OK Then
    Dim RK As RegistryKey = Registry.CurrentUser.OpenSubKey("Control Panel\Colors", True)
    RK.SetValue("Background", CD.Color.R & " " & CD.Color.G & " " & CD.Color.B)
End If

I'v seen this question, but it didn't help in my case.

The code above is working because the value in the registry editor is changing, but the background color is not, if i edited the color from the control panel, it will change the same value in the registry editor and the background color will change, anyone have explaining for that, or is there any other way to do it?

Mousa Alfhaily
  • 1,260
  • 3
  • 20
  • 38
  • 1
    That question talks about READING the background color, changing it is a more bit complex: [Set Desktop Wallpaper to a Solid Color](https://stackoverflow.com/questions/7309943/c-set-desktop-wallpaper-to-a-solid-color) – Steve Sep 11 '17 at 10:07
  • 1
    @Steve , Thanks to you, i'v got it, and i made an answer about it. – Mousa Alfhaily Sep 11 '17 at 11:03

1 Answers1

2

I finally got it to work, it can't be done by only editing the registry key, in fact you need to use the user32.dll API to do so, and you can use the registry key to get the value of the color only, but not to set it, thanks to Steve (↑).

Here is what worked for me :

First > Declare this function to use the API :

 Private Declare Function SetSysColors Lib "user32.dll" (ByVal one As Integer, ByRef element As Integer, ByRef color As Integer) As Boolean

Second > Use this to call the function and change the color :

Dim CD As New ColorDialog
If CD.ShowDialog = DialogResult.OK Then
    Dim BackgroundColor As Integer = ColorTranslator.ToWin32(CD.Color)
    SetSysColors(1, 1, BackgroundColor)
End If

Hope that was useful to someone :)

Mousa Alfhaily
  • 1,260
  • 3
  • 20
  • 38