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
|
Imports System.ComponentModel
Imports System.Drawing.Drawing2D
Public Class XpButton
Inherits System.Windows.Forms.Control
Private buffer As BufferedGraphics
Public Enum ButtonState
Normal
Pressed
End Enum
Private HasFocus As Boolean
Private IsMouseHover As Boolean
Private mState As ButtonState
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
'Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
'Me.SetStyle(ControlStyles.DoubleBuffer, True)
'Me.SetStyle(ControlStyles.UserPaint, True)
'Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
'Me.SetStyle(ControlStyles.ResizeRedraw, True)
Me.SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer, False)
Me.SetStyle(System.Windows.Forms.ControlStyles.ResizeRedraw, False)
Me.SetStyle(System.Windows.Forms.ControlStyles.UserPaint, True)
Me.SetStyle(ControlStyles.Opaque, True)
Me.UpdateStyles()
End Sub
Protected Overrides Sub OnPaintBackground(ByVal p As System.Windows.Forms.PaintEventArgs)
End Sub
<Category("Appearance"), Browsable(True), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
Public Overrides Property Text() As String
Get
Return MyBase.Text
End Get
Set(ByVal Value As String)
If MyBase.Text <> Value Then
MyBase.Text = Value
Call DrawControl()
End If
End Set
End Property
Private Sub XpButton_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
If Enabled Then
HasFocus = True
Call DrawControl()
End If
End Sub
Private Sub XpButton_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LostFocus
If Enabled Then
HasFocus = False
Call DrawControl()
End If
End Sub
Private Sub XpButton_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
If Enabled Then
IsMouseHover = True
Call DrawControl()
End If
End Sub
Private Sub XpButton_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave
If Enabled Then
IsMouseHover = False
Call DrawControl()
End If
End Sub
Private Sub DrawControl()
Dim r As New Rectangle(0, 0, Me.Width, Me.Height)
Dim ColorNormal1 As Color = Color.FromArgb(251, 248, 231)
Dim ColorNormal2 As Color = Color.FromArgb(221, 218, 201)
Dim ColorPressed1 As Color = Color.FromArgb(236, 233, 216)
Dim ColorPressed2 As Color = Color.FromArgb(255, 255, 246)
Dim Color1 As Color
Dim Color2 As Color
Dim brush1 As LinearGradientBrush
Dim brush2 As LinearGradientBrush
buffer.Graphics.Clear(Color.Black)
buffer.Graphics.PageUnit = GraphicsUnit.Pixel
If mState = ButtonState.Normal Then
Color1 = ColorNormal1
Color2 = ColorNormal2
brush1 = New LinearGradientBrush(r, Color1, Color2, LinearGradientMode.Vertical)
buffer.Graphics.FillRectangle(brush1, r)
Else
Color1 = ColorPressed1
Color2 = ColorPressed2
brush2 = New LinearGradientBrush(r, Color1, Color2, LinearGradientMode.Vertical)
buffer.Graphics.FillRectangle(brush2, r)
End If
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
buffer.Render(e.Graphics)
End Sub
Private Sub XpButton_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Créer le buffer
Dim Context As BufferedGraphicsContext = BufferedGraphicsManager.Current
Context.MaximumBuffer = Me.ClientSize
buffer = Context.Allocate(Me.CreateGraphics, Me.ClientRectangle)
End Sub
Private Sub XpButton_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If Enabled Then
mState = ButtonState.Pressed
Call DrawControl()
End If
End Sub
Private Sub XpButton_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
If Enabled Then
mState = ButtonState.Normal
Call DrawControl()
End If
End Sub
End Class |
Partager