替换windows背景中的系统颜色

问题描述:

以下代码应该替换用于窗口背景的系统颜色。您可以通过P /调用SetSysColor()API函数来更改它。我正在使用以下代码建议给我,但不幸的是,当我点击按钮没有任何反应!替换windows背景中的系统颜色

Imports System 
Imports System.Drawing 
Imports System.Windows.Forms 
Imports System.Runtime.InteropServices 

Namespace WindowsFormsApplication1 
    Partial Public Class Form1 
     Inherits Form 
     Private oldcolor As Integer 
     Public Sub New() 
      InitializeComponent() 
      oldcolor = GetSysColor(COLOR_WINDOW) 
      AddHandler Me.FormClosed, AddressOf Form1_FormClosed 
      AddHandler Me.button1.Click, AddressOf button1_Click 
     End Sub 

     Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs) Handles MyBase.FormClosed 
      Dim element As Integer = COLOR_WINDOW 
      SetSysColors(1, element, oldcolor) 
     End Sub 

     Private Function Color2COLORREF(ByVal color As Color) As Integer 
      Return color.R Or (color.G << 8) Or (color.B << &H10) 
     End Function 

     Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click 
      Dim element As Integer = COLOR_WINDOW 
      Dim colorref As Integer = Color2COLORREF(Color.NavajoWhite) 
      SetSysColors(1, element, colorref) 
     End Sub 

     Private Const COLOR_WINDOW As Integer = 5 
     <DllImport("user32.dll")> _ 
     Private Shared Function SetSysColors(ByVal one As Integer, ByRef element As Integer, ByRef color As Integer) As Boolean 
     End Function 
     <DllImport("user32.dll")> _ 
     Private Shared Function GetSysColor(ByVal element As Integer) As Integer 
     End Function 

     Friend WithEvents Button1 As System.Windows.Forms.Button 
     Private Sub InitializeComponent() 
      Me.Button1 = New System.Windows.Forms.Button 
      Me.SuspendLayout() 
      ' 
      'Button1 
      ' 
      Me.Button1.Location = New System.Drawing.Point(71, 61) 
      Me.Button1.Name = "Button1" 
      Me.Button1.Size = New System.Drawing.Size(153, 126) 
      Me.Button1.TabIndex = 0 
      Me.Button1.Text = "Button1" 
      Me.Button1.UseVisualStyleBackColor = True 
      ' 
      'Form1 
      ' 
      Me.ClientSize = New System.Drawing.Size(284, 264) 
      Me.Controls.Add(Me.Button1) 
      Me.Name = "Form1" 
      Me.ResumeLayout(False) 

     End Sub 




    End Class 
End Namespace 

它的工作原理。您正在更改的颜色是例如列表框中使用的背景颜色。所以,在表单中添加一个列表框,点击按钮,然后观看魔术:o)如果要为表单背景设置颜色,则需要使用另一个元素值。我发现a post with an enumeration of valid values

Public Enum SystemColor As Integer 
    ScrollBar = 0 'The Scrollbar colour ' 
    BackGround = 1 'Colour of the background with no wallpaper ' 
    ActiveCaption = 2 'Caption of Active Window ' 
    InactiveCaption = 3 'Caption of Inactive window ' 
    Menu = 4 'Menu ' 
    Window = 5 'Windows background ' 
    WindowFrame = 6 'Window frame ' 
    MenuText = 7 'Window Text ' 
    WindowText = 8 '3D dark shadow (Win95) ' 
    CaptionText = 9 'Text in window caption ' 
    ActiveBorder = 10 'Border of active window ' 
    InactiveBorder = 11 'Border of inactive window ' 
    AppWorkspace = 12 'Background of MDI desktop ' 
    Highlight = 13 'Selected item background ' 
    HighlightText = 14 'Selected menu item ' 
    BtnFace = 15 'Button ' 
    BtnShadow = 16 '3D shading of button ' 
    GrayText = 17 'Grey text, of zero if dithering is used. ' 
    BtnText = 18 'Button text ' 
    InactiveCaptionText = 19 'Text of inactive window ' 
    BtnHightList = 20 '3D highlight of button ' 
    SecondActiveCatpion = 27 'Win98 only: 2nd active window color ' 
    SecondInactiveCaption = 28 'Win98 only: 2nd inactive window color ' 
End Enum 

但是,这改变了所有正在运行的程序,以及背景颜色(只要你的程序正在运行)。这不打我作为一个非常好的事情要做......

更新

关于以实例链接:这是你的代码做我的Word实例,而应用程序正在运行:

alt text

更新2

完整的代码,对我的作品:

Imports System 
Imports System.Drawing 
Imports System.Windows.Forms 
Imports System.Runtime.InteropServices 

Public Class Form1 
    Inherits Form 
    Private oldcolor As Integer 
    Public Sub New() 
     InitializeComponent() 
     oldcolor = GetSysColor(SystemColor.Window) 
     AddHandler Me.FormClosed, AddressOf Form1_FormClosed 
     AddHandler Me.Button1.Click, AddressOf button1_Click 
    End Sub 

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs) Handles MyBase.FormClosed 
     SetSysColors(1, SystemColor.Window, oldcolor) 
    End Sub 

    Private Function Color2COLORREF(ByVal color As Color) As Integer 
     Return color.R Or (color.G << 8) Or (color.B << &H10) 
    End Function 

    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click 
     SetSysColors(1, SystemColor.Window, Color2COLORREF(Color.NavajoWhite)) 
    End Sub 

    <DllImport("user32.dll")> _ 
    Private Shared Function SetSysColors(ByVal one As Integer, ByRef element As Integer, ByRef color As Integer) As Boolean 
    End Function 
    <DllImport("user32.dll")> _ 
    Private Shared Function GetSysColor(ByVal element As Integer) As Integer 
    End Function 

    Friend WithEvents Button1 As System.Windows.Forms.Button 
    Private Sub InitializeComponent() 
     Me.Button1 = New System.Windows.Forms.Button 
     Me.SuspendLayout() 
     ' 
     'Button1 
     ' 
     Me.Button1.Location = New System.Drawing.Point(21, 50) 
     Me.Button1.Name = "Button1" 
     Me.Button1.Size = New System.Drawing.Size(153, 126) 
     Me.Button1.TabIndex = 0 
     Me.Button1.Text = "Button1" 
     Me.Button1.UseVisualStyleBackColor = True 
     ' 
     'Form1 
     ' 
     Me.BackColor = System.Drawing.SystemColors.Window 
     Me.ClientSize = New System.Drawing.Size(284, 264) 
     Me.Controls.Add(Me.Button1) 
     Me.Name = "Form1" 
     Me.ResumeLayout(False) 
     Me.PerformLayout() 
    End Sub 
End Class 

Public Enum SystemColor As Integer 
    ScrollBar = 0 'The Scrollbar colour ' 
    BackGround = 1 'Colour of the background with no wallpaper ' 
    ActiveCaption = 2 'Caption of Active Window ' 
    InactiveCaption = 3 'Caption of Inactive window ' 
    Menu = 4 'Menu ' 
    Window = 5 'Windows background ' 
    WindowFrame = 6 'Window frame ' 
    MenuText = 7 'Window Text ' 
    WindowText = 8 '3D dark shadow (Win95) ' 
    CaptionText = 9 'Text in window caption ' 
    ActiveBorder = 10 'Border of active window ' 
    InactiveBorder = 11 'Border of inactive window ' 
    AppWorkspace = 12 'Background of MDI desktop ' 
    Highlight = 13 'Selected item background ' 
    HighlightText = 14 'Selected menu item ' 
    BtnFace = 15 'Button ' 
    BtnShadow = 16 '3D shading of button ' 
    GrayText = 17 'Grey text, of zero if dithering is used. ' 
    BtnText = 18 'Button text ' 
    InactiveCaptionText = 19 'Text of inactive window ' 
    BtnHightList = 20 '3D highlight of button ' 
    SecondActiveCatpion = 27 'Win98 only: 2nd active window color ' 
    SecondInactiveCaption = 28 'Win98 only: 2nd inactive window color ' 
End Enum 
+0

我希望它能像这样工作:http://www.thomson-software-solutions.com/html/screen_tinter.html – 2009-07-18 19:46:37