заменить системный цвет в фонах окон - PullRequest
0 голосов
/ 18 июля 2009

следующий код должен заменить системный цвет, используемый для фона окна. Вы изменили бы это с помощью P / Invoking API-функции SetSysColor (). Я использую следующий код, который мне предложили, но, к сожалению, когда я нажимаю кнопку, ничего не происходит!

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

1 Ответ

2 голосов
/ 18 июля 2009

Работает. Изменяемый цвет - это цвет фона, который используется, например, в списках. Итак, добавьте в форму список со списком, нажмите кнопку и наблюдайте за магией: o) Если вы хотите установить цвет фона формы, вам нужно будет использовать другое значение элемента. Я нашел пост с перечислением допустимых значений :

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...