DWMWA_USE_IMMERSIVE_DARK_MODE won't update

StewartBW 1,175 Reputation points
2025-03-05T19:37:40.9233333+00:00

Hi

Using this code to switch to dark and light mode for my .Net Framework 4.8 app only, but they won't apply until the form is resized, I can do:

Me.Width = Me.Width + 1

Me.Width = Me.Width - 1

But that's not the way to go, the code, Windows 10 21H2, any help is appreciated :)

Imports System.Runtime.InteropServices
Public Class Form1
    <DllImport("dwmapi.dll", CharSet:=CharSet.Unicode, PreserveSig:=True)>
    Public Shared Function DwmSetWindowAttribute(hwnd As IntPtr, attr As Integer, ByRef attrValue As Boolean, attrSize As Integer) As Integer
    End Function
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        DwmSetWindowAttribute(Me.Handle, 20, False, Runtime.InteropServices.Marshal.SizeOf(True))
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        DwmSetWindowAttribute(Me.Handle, 20, True, Runtime.InteropServices.Marshal.SizeOf(True))
    End Sub
End Class
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,793 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 87,706 Reputation points
    2025-03-05T21:56:41.7366667+00:00

    A way is to force activation of caption, like :

    SendMessage(Me.Handle, WM_NCACTIVATE, CType(False, IntPtr), IntPtr.Zero)
    SendMessage(Me.Handle, WM_NCACTIVATE, CType(True, IntPtr), IntPtr.Zero)
    
    

    with :

        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function SendMessage(hWnd As IntPtr, Msg As Integer, wParam As IntPtr, lParam As IntPtr) As Integer
        End Function
        Public Const WM_NCACTIVATE = &H86
    
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.