我想禁用窗口的窗体淡入效果。我想我找到了正确的函数
<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并且该标志应该是
DWMWA_TRANSITIONS_FORCEDISABLED但我不知道如何在VB.NET中调用它。这是我到目前为止所拥有的:
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非常感谢你的帮助!
发布于 2012-12-22 19:13:13
DWMWA_TRANSITIONS_FORCEDISABLED的文档如下:
与DwmSetWindowAttribute一起使用
。启用或强制禁用DWM过渡。pvAttribute参数指向的值为TRUE以禁用转换,或FALSE以启用转换。
宏TRUE和FALSE声明为:
#define FALSE 0
#define TRUE 1因此,您需要为attrValue参数传递1。
Windows本机使用的布尔类型是BOOL。这是这样声明的:
typedef int BOOL;因为sizeof(int)是4,所以您需要传递的attrSize是4。
发布于 2012-12-22 17:45:38
对我来说起作用的是
If Environment.OSVersion.Version.Major >= 6 Then
DwmSetWindowAttribute(Me.Handle, DWMWINDOWATTRIBUTE.DWMWA_TRANSITIONS_FORCEDISABLED, 1, 4)
End If但这只是一种猜测。我不确定"1“是否代表YES,我也不确定长度4是否在所有条件下都是正确的。如果能确认这一点就太好了。谢谢!
发布于 2012-12-22 20:26:25
由于属性值参数类型的原因,该函数很笨拙。它使用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然后,您需要更改调用此函数的位置,一个窗口可以多次创建,但Load事件只运行一次。您需要在窗口创建后立即调用它。添加错误处理,以便可以诊断运行时问题:
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当我测试它时,它工作得很好。
https://stackoverflow.com/questions/14001694
复制相似问题