Drop Shadow On A Borderless WinForm

后端 未结 5 1949
暖寄归人
暖寄归人 2020-12-08 05:38

I\'m trying to drop a shadow around the whole form just like the first picture, except that that is a WPF, not a WinForm. now I want to drop the same shadow on a winform.

5条回答
  •  -上瘾入骨i
    2020-12-08 06:05

    Humm ,,, Just past the code and you will get the windows 7 Drop Shadow like this >>> http://marcin.floryan.pl/wp-content/uploads/2010/08/WPF-Window-native-shadow.png

    Imports System.Runtime.InteropServices
    
    Public Class IMSS_SplashScreen
        Private aeroEnabled As Boolean
        Protected Overrides ReadOnly Property CreateParams() As CreateParams
            Get
                CheckAeroEnabled()
                Dim cp As CreateParams = MyBase.CreateParams
                If Not aeroEnabled Then
                    cp.ClassStyle = cp.ClassStyle Or NativeConstants.CS_DROPSHADOW
                    Return cp
                Else
                    Return cp
                End If
            End Get
        End Property
        Protected Overrides Sub WndProc(ByRef m As Message)
            Select Case m.Msg
                Case NativeConstants.WM_NCPAINT
                    Dim val = 2
                    If aeroEnabled Then
                        NativeMethods.DwmSetWindowAttribute(Handle, 2, val, 4)
                        Dim bla As New NativeStructs.MARGINS()
                        With bla
                            .bottomHeight = 1
                            .leftWidth = 1
                            .rightWidth = 1
                            .topHeight = 1
                        End With
                        NativeMethods.DwmExtendFrameIntoClientArea(Handle, bla)
                    End If
                    Exit Select
            End Select
            MyBase.WndProc(m)
        End Sub
        Private Sub CheckAeroEnabled()
            If Environment.OSVersion.Version.Major >= 6 Then
                Dim enabled As Integer = 0
                Dim response As Integer = NativeMethods.DwmIsCompositionEnabled(enabled)
                aeroEnabled = (enabled = 1)
            Else
                aeroEnabled = False
            End If
        End Sub
    End Class
    Public Class NativeStructs
        Public Structure MARGINS
            Public leftWidth As Integer
            Public rightWidth As Integer
            Public topHeight As Integer
            Public bottomHeight As Integer
        End Structure
    End Class
    Public Class NativeMethods
         _
        Public Shared Function DwmExtendFrameIntoClientArea(ByVal hWnd As IntPtr, ByRef pMarInset As NativeStructs.MARGINS) As Integer
        End Function
         _
        Friend Shared Function DwmSetWindowAttribute(ByVal hwnd As IntPtr, ByVal attr As Integer, ByRef attrValue As Integer, ByVal attrSize As Integer) As Integer
        End Function
         _
        Public Shared Function DwmIsCompositionEnabled(ByRef pfEnabled As Integer) As Integer
        End Function
    End Class
    Public Class NativeConstants
        Public Const CS_DROPSHADOW As Integer = &H20000
        Public Const WM_NCPAINT As Integer = &H85
    End Class
    

提交回复
热议问题