Bonjour,

Je souhaite dessiner un contour dégradé bleu autour de mon form principal à l'aide d'un form secondaire et de la fonction UpdateLayeredWindow.
Je me suis inspiré de l'exemple à cette adresse : https://code.msdn.microsoft.com/CSWi...indow-23cdc375

Mon problème est que le form secondaire reste invisible à l'écran alors qu'il est bien visible dans la prévisualisation de la barre des tâches :
Nom : Sans titre.png
Affichages : 332
Taille : 25,8 Ko

Voici le code du form1 :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Public Class Form1
 
 
    Public Form2 As New Form2
    Public BlurWidth As Integer = 5
    Public ColorHighlight As Color = SystemColors.Highlight
 
 
    Public Sub UpdateBorder()
        Form2.Location = New Point(Location.X - BlurWidth, Location.Y - BlurWidth)
        Form2.Size = New Size(Width + 2 * BlurWidth, Height + 2 * BlurWidth)
        Dim bmp As Bitmap = New Bitmap(Form2.Width, Form2.Height, Imaging.PixelFormat.Format32bppArgb)
        Dim g As Graphics = Graphics.FromImage(bmp)
        g.Clear(Color.FromArgb(0))
        For i = 0 To BlurWidth
            g.DrawRectangle(New Pen(Color.FromArgb(255 * i / 5, ColorHighlight), 1), New Rectangle(i, i, Form2.Width - 2 * i, Form2.Height - 2 * i))
        Next
        Form2.SelectBitmap(bmp)
    End Sub
 
 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Form2.Show()
    End Sub
 
 
    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        UpdateBorder()
    End Sub
 
 
End Class
et celui du form2 :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
Imports System.Runtime.InteropServices
 
 
Public Class Form2
    Public Sub New()
        InitializeComponent()
    End Sub
 
 
    Public Const WS_EX_LAYERED As Integer = &H80000
    Public Const AC_SRC_OVER As Integer = &H0
    Public Const AC_SRC_ALPHA As Integer = &H1
    Public Const ULW_ALPHA As Integer = &H2
 
 
    <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Public Shared Function UpdateLayeredWindow(ByVal hwnd As IntPtr, ByVal hdcDst As IntPtr, ByRef pptDst As Point, ByRef psize As Size, ByVal hdcSrc As IntPtr, ByRef pprSrc As Point, ByVal crKey As Integer, ByRef pblend As BLENDFUNCTION, ByVal dwFlags As Integer) As Boolean
    End Function
    <DllImport("gdi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Public Shared Function CreateCompatibleDC(ByVal hDC As IntPtr) As IntPtr
    End Function
    <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Public Shared Function GetDC(ByVal hWnd As IntPtr) As IntPtr
    End Function
    <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Public Shared Function ReleaseDC(ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As Integer
    End Function
    <DllImport("gdi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Public Shared Function DeleteDC(ByVal hdc As IntPtr) As Boolean
    End Function
    <DllImport("gdi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Public Shared Function SelectObject(ByVal hDC As IntPtr, ByVal hObject As IntPtr) As IntPtr
    End Function
    <DllImport("gdi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Public Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
    End Function
 
 
 
 
    <StructLayout(LayoutKind.Sequential)>
    Public Structure BLENDFUNCTION
        Private Shared _BlendOp As Byte
        Private Shared _BlendFlags As Byte
        Private Shared _SourceConstantAlpha As Byte
        Private Shared _AlphaFormat As Byte
 
        Shared Sub New()
            _BlendOp = 0
            _BlendFlags = 0
            _SourceConstantAlpha = 0
            _AlphaFormat = 0
        End Sub
 
        Public Property BlendOp As Byte
            Get
                Return _BlendOp
            End Get
            Set(ByVal value As Byte)
                _BlendOp = value
            End Set
        End Property
 
        Public Property BlendFlags As Byte
            Get
                Return _BlendFlags
            End Get
            Set(ByVal value As Byte)
                _BlendFlags = value
            End Set
        End Property
 
        Public Property SourceConstantAlpha As Byte
            Get
                Return _SourceConstantAlpha
            End Get
            Set(ByVal value As Byte)
                _SourceConstantAlpha = value
            End Set
        End Property
 
        Public Property AlphaFormat As Byte
            Get
                Return _AlphaFormat
            End Get
            Set(ByVal value As Byte)
                _AlphaFormat = value
            End Set
        End Property
    End Structure
 
    Protected Overrides ReadOnly Property CreateParams As CreateParams
        Get
            Dim CP As CreateParams = MyBase.CreateParams
            CP.ExStyle = CP.ExStyle Or WS_EX_LAYERED
            Return CP
        End Get
    End Property
 
    Public Sub SelectBitmap(ByVal Bmp As Bitmap)
        If Bmp.PixelFormat <> Imaging.PixelFormat.Format32bppArgb Then
            Throw New ApplicationException("The bitmap must be 32bpp with alpha-channel.")
        End If
        Dim screenDc As IntPtr = GetDC(IntPtr.Zero)
        Dim memDc As IntPtr = CreateCompatibleDC(screenDc)
        Dim hBitmap As IntPtr = IntPtr.Zero
        Dim hOldBitmap As IntPtr = IntPtr.Zero
        hBitmap = Bmp.GetHbitmap(Color.FromArgb(0))
        hOldBitmap = SelectObject(memDc, hBitmap)
        Dim newSize As New Size(Bmp.Width, Bmp.Height)
        Dim sourceLocation As New Point(0, 0)
        Dim newLocation As New Point(Me.Left, Me.Top)
        Dim blend As New BLENDFUNCTION()
        blend.BlendOp = AC_SRC_OVER
        blend.BlendFlags = 0
        blend.SourceConstantAlpha = 255
        blend.AlphaFormat = AC_SRC_ALPHA
        UpdateLayeredWindow(Me.Handle, screenDc, newLocation, newSize, memDc, sourceLocation, 0, blend, ULW_ALPHA)
        ReleaseDC(IntPtr.Zero, screenDc)
        If hBitmap <> IntPtr.Zero Then
            SelectObject(memDc, hOldBitmap)
            DeleteObject(hBitmap)
        End If
        DeleteDC(memDc)
        'Renvoi 0
        MsgBox(Marshal.GetLastWin32Error)
    End Sub
 
    Private Sub InitializeComponent()
        Me.SuspendLayout()
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(284, 261)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
        Me.Name = "Form2"
        Me.Text = "Form2"
        Me.TopMost = True
        Me.ResumeLayout(False)
    End Sub
 
End Class
Je ne parviens pas à comprendre d'où viens le problème. Ce post semble traiter d'un problème semblable, mais il ne m'a pas davantage aider à le résoudre.
Merci d'avance !