sachsenhessi
Goto Top

Anpassen der Titelzeile einer WinForms-Form

Hallo @all,

ich suche eine Funktionalität zum Anpassen der Titelzeile einer (Win11-)WinForms-Form.
Konkret soll die Hintergrundfarbe geändert werden.
Bisheriger Ansatz klappt leider nur in Win10. Ich such etwas ähnliches für Win11.
(In VB.Net oder C#)
    
Imports System.Runtime.InteropServices
Public Class FormTest
                    Inherits Form

<DllImport("user32.dll", SetLastError:=True)>  
    Private Shared Function SetWindowCompositionAttribute(hWnd As IntPtr, ByRef data As WindowCompositionAttributeData) As Integer
    End Function

    <StructLayout(LayoutKind.Sequential)>
    Private Structure AccentPolicy
        Public nAccentState As Integer
        Public nFlags As Integer
        Public nColor As Integer
        Public nAnimationId As Integer
    End Structure

    <StructLayout(LayoutKind.Sequential)>
    Private Structure WindowCompositionAttributeData
        Public Attribute As Integer
        Public Data As IntPtr
        Public SizeOfData As Integer
    End Structure

    Private Const WCA_ACCENT_POLICY As Integer = 19
    Private Const ACCENT_ENABLE_BLURBEHIND As Integer = 3

    Public Sub New()
        InitializeComponent()
        SetTitleBarColor(Color.FromArgb(1, 93, 109)) ' Aufruf der Funktion, um die Farbe der Titelleiste zu ändern  
    End Sub

    Private Sub SetTitleBarColor(color As Color)
        Dim accent As New AccentPolicy()
        Dim accentStructSize As Integer = Marshal.SizeOf(accent)
        accent.nAccentState = ACCENT_ENABLE_BLURBEHIND
        accent.nFlags = 2
        accent.nColor = ColorToABGR(color)

        Dim accentPtr As IntPtr = Marshal.AllocHGlobal(accentStructSize)
        Marshal.StructureToPtr(accent, accentPtr, False)

        Dim data As New WindowCompositionAttributeData()
        data.Attribute = WCA_ACCENT_POLICY
        data.SizeOfData = accentStructSize
        data.Data = accentPtr

        Dim hWnd As IntPtr = Me.Handle
        SetWindowCompositionAttribute(hWnd, data)

        Marshal.FreeHGlobal(accentPtr)
    End Sub

    Private Function ColorToABGR(color As Color) As Integer
        Return (CInt(color.A) << 24) Or (CInt(color.B) << 16) Or (CInt(color.G) << 8) Or CInt(color.R)
    End Function

End Class
Leider habe ich nix brauchbares gefunden.
Danke für Eure Hilfe.
SH

Content-Key: 22839481907

Url: https://administrator.de/contentid/22839481907

Printed on: April 27, 2024 at 10:04 o'clock

Mitglied: 11078840001
11078840001 Feb 21, 2024 updated at 13:21:49 (UTC)
Goto Top
Beispiel mit Powershell um z.B. den Titel des PS Fensters gelb zu machen , kannst du dir ja selbst auf C# ummünzen (klappt hier unter Windows 11)
Add-Type -MemberDefinition '[DllImport("dwmapi.dll")] public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);'-Namespace my -Name tools  
$color = 0x0050ffff
$handle = (Get-Process -id $PID).MainWindowHandle
[void][my.tools]::DwmSetWindowAttribute($handle,35,[ref]$color,[System.Runtime.InteropServices.Marshal]::SizeOf($color))
pause
Doku
https://learn.microsoft.com/de-de/windows/win32/api/dwmapi/nf-dwmapi-dwm ...
https://learn.microsoft.com/de-de/windows/win32/api/dwmapi/ne-dwmapi-dwm ...
https://stackoverflow.com/questions/39261826/change-the-color-of-the-tit ...


screenshot

Beispiel für Notepad Fenster
Add-Type -MemberDefinition '[DllImport("dwmapi.dll")] public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);'-Namespace my -Name tools  
$color = 0x0050ffff
$handle = (Get-Process -name Notepad).MainWindowHandle
[void][my.tools]::DwmSetWindowAttribute($handle,35,[ref]$color,[System.Runtime.InteropServices.Marshal]::SizeOf($color))

screenshot
Member: SachsenHessi
SachsenHessi Feb 22, 2024 at 08:46:15 (UTC)
Goto Top
Danke, schaue ich mir Montag an face-smile
LG
SH