2009-07-18 8 views
0

le code suivant est supposé remplacer la couleur du système utilisée pour les arrière-plans de fenêtre. Vous le modifieriez en appelant la fonction API SetSysColor(). J'utilise le code suivant qui m'a été suggéré mais malheureusement quand je clique sur le bouton rien ne se passe!remplacer la couleur du système dans les arrière-plans de fenêtres

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 

Répondre

2

Cela fonctionne. La couleur que vous modifiez est la couleur d'arrière-plan utilisée dans les zones de liste, par exemple. Donc, ajoutez une zone de liste au formulaire, cliquez sur le bouton et observez la magie: o) Si vous voulez définir la couleur de l'arrière-plan du formulaire, vous devrez utiliser une autre valeur d'élément. J'ai trouvé 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 

Cependant, cela change la couleur de fond de tous les programmes en cours d'exécution aussi bien (aussi longtemps que votre programme est en cours d'exécution). Cela ne m'a pas frappé comme une très belle chose à faire ...

Mise à jour

En ce qui concerne le lien avec l'exemple: c'est ce que votre code a fait à mon instance de Word alors que l'application a été en cours d'exécution:

alt text

Mise à jour 2

code complet qui fonctionne pour moi:

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

je veux que cela fonctionne comme ici: http://www.thomson-software-solutions.com/html/screen_tinter.html –

+0

je vous remercie très très très beaucoup. tout le meilleur pour vous –

+0

comment avez-vous supprimé vos commentaires? –

Questions connexes