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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
Imports System.ComponentModel
Imports System.Drawing.Drawing2D
Public Class MyButton
Inherits Button
Private m_borderRadius As Single = 20
<Browsable(True)>
Public Property BorderRadius As Single
Get
Return m_borderRadius
End Get
Set(value As Single)
If value <> m_borderRadius Then
m_borderRadius = value
Me.Invalidate()
End If
End Set
End Property
Private m_color1 As Color = Color.Red
<Browsable(True)>
Public Property Color1() As Color
Get
Return m_color1
End Get
Set(ByVal value As Color)
m_color1 = value
Me.Invalidate()
End Set
End Property
Private m_Color2 As Color = Color.Blue
<Browsable(True)>
Public Property Color2() As Color
Get
Return m_Color2
End Get
Set(ByVal value As Color)
m_Color2 = value
Me.Invalidate()
End Set
End Property
Private m_borderColor As Color = Color.LimeGreen
<Browsable(True)>
Public Property BorderColor() As Color
Get
Return m_borderColor
End Get
Set(ByVal value As Color)
m_borderColor = value
Me.Invalidate()
End Set
End Property
Private opacity As Integer = 0
Private isPressed As Boolean
Private bGotKeyDown As Boolean
Public Sub New()
' Cet appel est requis par le concepteur.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
End Sub
Protected Overrides Sub OnKeyDown(kevent As System.Windows.Forms.KeyEventArgs)
bGotKeyDown = True
Select Case kevent.KeyCode
Case Keys.Space, Keys.Enter
isPressed = True
Case Keys.Up, Keys.Left
Me.Parent.SelectNextControl(Me, False, False, True, True)
Case Keys.Down, Keys.Right
Me.Parent.SelectNextControl(Me, True, False, True, True)
Case Else
bGotKeyDown = False
MyBase.OnKeyDown(kevent)
End Select
End Sub
Protected Overrides Sub OnKeyUp(kevent As System.Windows.Forms.KeyEventArgs)
Select Case kevent.KeyCode
Case Keys.Space, Keys.Enter
If bGotKeyDown Then
isPressed = False
Me.Invalidate()
OnClick(EventArgs.Empty)
bGotKeyDown = False
End If
Case Else
MyBase.OnKeyUp(kevent)
End Select
End Sub
Protected Overrides Sub OnMouseDown(mevent As System.Windows.Forms.MouseEventArgs)
If mevent.Button = System.Windows.Forms.MouseButtons.Left Then
Me.Capture = True
Me.Focus()
isPressed = True
Me.Invalidate()
End If
MyBase.OnMouseDown(mevent)
End Sub
Protected Overrides Sub OnMouseUp(mevent As System.Windows.Forms.MouseEventArgs)
Me.Capture = False
isPressed = False
Me.Invalidate()
MyBase.OnMouseUp(mevent)
End Sub
Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
Dim gr As Graphics = e.Graphics
gr.SmoothingMode = SmoothingMode.AntiAlias
DrawButton(gr, isPressed, BorderRadius)
End Sub
Protected Overrides Sub OnResize(e As System.EventArgs)
Me.Invalidate()
MyBase.OnResize(e)
End Sub
Private Sub DrawButton(ByVal gr As Graphics, ByVal pressed As Boolean, rad As Single)
Dim rc As Rectangle = Me.ClientRectangle
rc.Inflate(-1, -1)
' Draw fill and border.
DrawFillAndBorder(gr, rc, rad)
' Draw the text.
Dim sf As New StringFormat()
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
gr.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), Me.ClientRectangle, sf)
End Sub
Public Sub DrawFillAndBorder(
ByVal grfx As Graphics, rc As RectangleF,
ByVal rad As Single)
Dim corner1 As PointF =
New PointF(rc.Left, rc.Top)
Dim corner2 As PointF =
New PointF(rc.Right - rad, rc.Top)
Dim corner3 As PointF =
New PointF(rc.Right - rad, rc.Bottom - rad)
Dim corner4 As PointF =
New PointF(rc.Left, rc.Bottom - rad)
Dim rcLeftTop As New Rectangle(corner1.X, corner1.Y, rad, rad)
Dim rcRightTop As New Rectangle(corner2.X, corner2.Y, rad, rad)
Dim rcRightBottom As New Rectangle(corner3.X, corner3.Y, rad, rad)
Dim rcLeftBottom As New Rectangle(corner4.X, corner4.Y, rad, rad)
' Create a GraphicsPath object.
Using controlPath As New GraphicsPath
' Set up and call AddArc, and close the figure.
controlPath.StartFigure()
controlPath.AddArc(rcLeftTop, 180, 90)
controlPath.AddLine(
New PointF(rc.Left + rad / 2, rc.Top),
New PointF(rc.Right - rad / 2, rc.Top))
controlPath.AddArc(rcRightTop, 270, 90)
controlPath.AddLine(
New PointF(rc.Right, rc.Top + rad / 2),
New PointF(rc.Right, rc.Bottom - rad / 2))
'controlPath.StartFigure()
controlPath.AddArc(rcRightBottom, 0, 90)
controlPath.AddLine(
New PointF(rc.Right - rad / 2, rc.Bottom),
New PointF(rc.Left + rad / 2, rc.Bottom))
controlPath.AddArc(rcLeftBottom, 90, 90)
controlPath.AddLine(
New PointF(rc.Left, rc.Bottom - rad / 2),
New PointF(rc.Left, rc.Top + rad / 2))
controlPath.CloseFigure()
' fill
If isPressed Then
opacity = 100
Color1 = Color.FromArgb(opacity, Color1)
Color2 = Color.FromArgb(opacity, Color2)
Using br As New LinearGradientBrush(
rc, Color1, Color2,
LinearGradientMode.Horizontal, True)
grfx.FillPath(br, controlPath)
End Using
Else
opacity = 255
Color1 = Color.FromArgb(opacity, Color1)
Color2 = Color.FromArgb(opacity, Color2)
Using br As New LinearGradientBrush(
rc, Color1, Color2,
LinearGradientMode.Horizontal, True)
grfx.FillPath(br, controlPath)
End Using
End If
' border
'grfx.DrawPath(New Pen(Color.Red, 1.0), controlPath)
grfx.DrawPath(New Pen(BorderColor, 2.0), controlPath)
End Using
End Sub
End Class |
Partager