VB.NET disables fading for forms

I want to disable the shape fading effect for the window. I think I found the correct function

<DllImport("dwmapi.dll", PreserveSig:=True)> _
Private Shared Function DwmSetWindowAttribute(ByVal hwnd As IntPtr, ByVal attr As Integer, ByRef attrValue As Integer, ByVal attrSize As Integer) As Integer
End Function

and the flag should be

DWMWA_TRANSITIONS_FORCEDISABLED

But I do not know what to call it with VB.NET. This is what I have so far:

Imports System.Runtime.InteropServices

Public Class Form1

Public Enum DWMWINDOWATTRIBUTE
    DWMWA_ALLOW_NCPAINT = 4
    DWMWA_CAPTION_BUTTON_BOUNDS = 5
    DWMWA_FLIP3D_POLICY = 8
    DWMWA_FORCE_ICONIC_REPRESENTATION = 7
    DWMWA_LAST = 9
    DWMWA_NCRENDERING_ENABLED = 1
    DWMWA_NCRENDERING_POLICY = 2
    DWMWA_NONCLIENT_RTL_LAYOUT = 6
    DWMWA_TRANSITIONS_FORCEDISABLED = 3
End Enum

<DllImport("dwmapi.dll", PreserveSig:=True)> _
Private Shared Function DwmSetWindowAttribute(ByVal hwnd As IntPtr, ByVal attr As Integer, ByRef attrValue As Integer, ByVal attrSize As Integer) As Integer
End Function

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Environment.OSVersion.Version.Major >= 6 Then
        DwmSetWindowAttribute Me.Handle, 'here I don't know how to go on...
    End If
End Sub
End Class

Thank you very much for your help!

0
source share
3 answers

The documentation for DWMWA_TRANSITIONS_FORCEDISABLEDreads:

Use with DwmSetWindowAttribute. Enables or enforces DWM transitions. The pvAttribute parameter indicates TRUE to disable transitions, or FALSE to enable transitions.

Macros TRUEand FALSEare declared as:

#define FALSE 0
#define TRUE  1

So you need to pass 1for the parameter attrValue.

, Windows , - BOOL. :

typedef int BOOL;

sizeof(int) - 4, attrSize 4.

+1

    If Environment.OSVersion.Version.Major >= 6 Then
        DwmSetWindowAttribute(Me.Handle, DWMWINDOWATTRIBUTE.DWMWA_TRANSITIONS_FORCEDISABLED, 1, 4)
    End If

. , "1" "", , 4 . , . !

+1

- . void *, C, . , VB.NET, . , , .

, , BOOL. , , BOOL :

<DllImport("dwmapi.dll")> _
Private Shared Function DwmSetWindowAttribute(ByVal hwnd As IntPtr, _
            ByVal attr As Integer, ByRef attrValue As Boolean, _
            ByVal attrSize As Integer) As Integer
End Function

, :

Private Const DWMWA_TRANSITIONS_FORCEDISABLED As Integer = 3

Then you need to change the place where you call this function, the window can be created several times, but the Load event is fired only once. You must call it immediately after creating the window. Adding error handling so you can diagnose runtime problems:

Protected Overrides Sub OnHandleCreated(ByVal e As System.EventArgs)
    MyBase.OnHandleCreated(e)
    If Environment.OSVersion.Version.Major >= 6 Then
        Dim hr = DwmSetWindowAttribute(Me.Handle, _
                    DWMWA_TRANSITIONS_FORCEDISABLED, True, 4)
        If hr < 0 Then Marshal.ThrowExceptionForHR(hr)
    End If
End Sub

Worked well when I tested it.

+1
source

Source: https://habr.com/ru/post/1731036/


All Articles