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 142 143 144 145 146 147 148 149 150 151 152 153
|
Public Class TransparentControl
Inherits Control
Private borderColor As Color = Color.Black
'Public drag As Boolean = False
Public enab As Boolean = False
Private alpha As Integer
Private mdrag As Boolean = False
Public Property Drag() As Boolean
Get
Return mdrag
End Get
Set(ByVal value As Boolean)
mdrag = value
If (Me.Parent IsNot Nothing) Then
Me.Parent.Invalidate(Me.Bounds, True)
End If
End Set
End Property
Private mfillColor As Color = Color.White
Public Property FillColor() As Color
Get
Return mfillColor
End Get
Set(ByVal value As Color)
mfillColor = value
If (Me.Parent IsNot Nothing) Then
Me.Parent.Invalidate(Me.Bounds, True)
End If
End Set
End Property
Private mopacity As Integer = 100
Public Property Opacity() As Integer
Get
If (mopacity > 100) Then
mopacity = 100
ElseIf mopacity < 1 Then
mopacity = 1
End If
Return mopacity
End Get
Set(ByVal value As Integer)
mopacity = value
Me.Invalidate(Me.Bounds, True)
End Set
End Property
Private mborderstyle As BorderStyle
Public Property BorderStyle() As BorderStyle
Get
Return mborderstyle
End Get
Set(ByVal value As BorderStyle)
mborderstyle = value
If (Me.Parent IsNot Nothing) Then
Me.Parent.Invalidate(Me.Bounds, True)
End If
End Set
End Property
Public Sub New()
' Cet appel est requis par le concepteur.
InitializeComponent()
SetStyle(ControlStyles.SupportsTransparentBackColor, True)
SetStyle(ControlStyles.Opaque, True)
Me.BackColor = Color.Transparent
Me.BorderStyle = BorderStyle.FixedSingle
End Sub
Protected Overrides ReadOnly Property CreateParams As System.Windows.Forms.CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or &H20
Return cp
End Get
End Property
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim gr As Graphics = e.Graphics
Dim bounds As Rectangle = New Rectangle(0, 0, Me.Width - 1, Me.Height - 1)
If Me.BorderStyle = BorderStyle.FixedSingle Then
Using p As New Pen(borderColor)
gr.DrawRectangle(p, New Rectangle(0, 0, Width - 1, Height - 1))
End Using
End If
Dim frmColor As Color = Me.Parent.BackColor
Dim brushColor As Brush
Dim bckColor As Brush
alpha = CInt((Opacity * 255) / 100)
If Drag Then
Dim dragFillColor As Color
Dim dragBckColor As Color
If (BackColor <> color.Transparent) Then
Dim Rb As Integer = BackColor.R * alpha / 255 + frmColor.R * (255 - alpha) / 255
Dim Gb As Integer = BackColor.G * alpha / 255 + frmColor.G * (255 - alpha) / 255
Dim Bb As Integer = BackColor.B * alpha / 255 + frmColor.B * (255 - alpha) / 255
dragBckColor = color.FromArgb(Rb, Gb, Bb)
Else
dragBckColor = frmColor
End If
If (FillColor <> color.Transparent) Then
Dim Rf As Integer = FillColor.R * alpha / 255 + frmColor.R * (255 - alpha) / 255
Dim Gf As Integer = FillColor.G * alpha / 255 + frmColor.G * (255 - alpha) / 255
Dim Bf As Integer = FillColor.B * alpha / 255 + frmColor.B * (255 - alpha) / 255
dragFillColor = color.FromArgb(Rf, Gf, Bf)
Else
dragFillColor = dragBckColor
End If
alpha = 255
brushColor = New SolidBrush(Color.FromArgb(alpha, dragFillColor))
bckColor = New SolidBrush(Color.FromArgb(alpha, dragBckColor))
Else
Dim color As Color = FillColor
brushColor = New SolidBrush(color.FromArgb(alpha, color))
bckColor = New SolidBrush(color.FromArgb(alpha, Me.BackColor))
End If
Dim pen As New Pen(Me.ForeColor)
If (Me.BackColor <> Color.Transparent Or Drag) Then 'remplit le fond avec backolor opacity du control
gr.FillRectangle(bckColor, bounds)
ElseIf (FillColor <> Color.Transparent Or Drag) Then 'remplit le fond avec FillColor opacity du control
gr.FillRectangle(brushColor, bounds)
Else
gr.FillRectangle(New SolidBrush(Color.FromArgb(1, Color.White)), bounds)
End If
'gr.DrawEllipse(pen, bounds)
pen.Dispose()
brushColor.Dispose()
bckColor.Dispose()
gr.Dispose()
MyBase.OnPaint(e)
End Sub
Protected Overrides Sub OnBackColorChanged(e As System.EventArgs)
If Me.Parent IsNot Nothing Then Parent.Invalidate(Me.Bounds, True)
MyBase.OnBackColorChanged(e)
End Sub
Protected Overrides Sub OnParentBackColorChanged(e As System.EventArgs)
Me.Invalidate()
MyBase.OnParentBackColorChanged(e)
End Sub
End Class |
Partager