Un bouton pouvant contenir jusqu'à 4 Texts différents avec avec les propriétés TextColor, TextFont, TextMargin et TextAlign spécifiques à chacun des Texts.
Un module MyFunctions avec des fonctions qui vous seront, je l'espère, utiles.
Source / Exemple :
Option Strict On
Option Explicit On
Imports System.Drawing.Drawing2D
Imports System.ComponentModel
Public Class MyCBtn
Private MeForeColor As Color = MyBase.ForeColor 'Color.Silver
Private MeFont As Font = MyBase.Font ' New System.Drawing.Font("Microsoft Sans Serif", 9.25!)
Public Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
End Sub
Private Sub Me_BackColorChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.BackColorChanged
Me.Btn.Refresh()
End Sub
Private Sub Btn_Paint(ByVal sender As System.Object, ByVal e As PaintEventArgs) _
Handles Btn.Paint
Call BtnBrush(e)
For _i As Integer = 0 To _TextContents.Length - 1
Dim _Text As String = _TextContents(_i)
Dim _X As Single = 0 'Margins(0) ' _TextMargin1.Left
Dim _Y As Single = 0 'Margins(1) ' _TextMargin1.Top
Dim _TxtAlign As ContentAlignment = ContentAlignment.TopLeft
' ... Fonts ...
Dim _F As Font = MeFont
Try
_F = _Fonts(_i)
Catch ex As Exception
End Try
' ... End Fonts ...
' ... Colors ...
Dim _Col As Color = MeForeColor
Try
_Col = _TextColors(_i)
Catch ex As Exception
End Try
' ... End Colors ...
' ... Margin ...
Dim _mTop As Integer = 0
Try
_mTop = _TextMargTops(_i)
Catch ex As Exception
End Try
Dim _mBot As Integer = 0
Try
_mBot = _TextMargBots(_i)
Catch ex As Exception
End Try
Dim _mLeft As Integer = 0
Try
_mLeft = _TextMargLefts(_i)
Catch ex As Exception
End Try
Dim _mRight As Integer = 0
Try
_mRight = _TextMargRights(_i)
Catch ex As Exception
End Try
' ... End Margin ...
' ... AlignV ...
Dim _TxtAlignV As AlignV = AlignV.Top
Try
_TxtAlignV = _AlignVs(_i)
Catch ex As Exception
End Try
Dim _BtnHeight As Integer = Me.Btn.Height
Dim _StrHeight As Integer = StringSize_(_Text, _F).Height
Select Case _TxtAlignV
Case AlignV.Top
_Y = _mTop
Case AlignV.Middle
_Y = CSng((_BtnHeight - _StrHeight) / 2)
Case AlignV.Bottom
_Y = _BtnHeight - _StrHeight - _mBot
End Select
' ... End AlignV ...
' ... AlignH ...
Dim _TxtAlignH As AlignH = AlignH.Left
Try
_TxtAlignH = _AlignHs(_i)
Catch ex As Exception
End Try
Dim _BtnWidth As Integer = Me.Btn.Width
Dim _StrWidth As Integer = StringSize_(_Text, _F).Width
Select Case _TxtAlignH
Case AlignH.Left
_X = _mLeft
Case AlignH.Center
_X = CSng((_BtnWidth - _StrWidth) / 2)
Case AlignH.Right
_X = _BtnWidth - _StrWidth - _mRight
End Select
' ... End AlignH ...
e.Graphics.DrawString(_Text, _F, New SolidBrush(_Col), _X, _Y)
Next
End Sub
Private Sub BtnBrush(ByVal e As PaintEventArgs)
Dim _BrushColors As Color() = BrushColors()
Dim _colTop As Color = _BrushColors(0)
Dim _colBot As Color = _BrushColors(1)
Dim _Bounds As Rectangle = Me.Btn.Bounds
_Bounds.Width -= (Me.Btn.FlatAppearance.BorderSize * 2)
_Bounds.Height -= (Me.Btn.FlatAppearance.BorderSize * 2)
_Bounds.X += Me.Btn.FlatAppearance.BorderSize
_Bounds.Y += Me.Btn.FlatAppearance.BorderSize
Dim _Brush As Brush = New LinearGradientBrush(_Bounds, _colTop, _colBot, LinearGradientMode.Vertical)
e.Graphics.FillRegion(_Brush, New Region(_Bounds))
If _Brush IsNot Nothing Then _Brush.Dispose()
End Sub
Private Function BrushColors() As Color()
Dim _ColorLight As Color = Me.BackColor
Dim _BrushColors As Color() = New Color() {_ColorLight, _ColorLight}
Dim _A As Integer = 255
If _BtnStyle = BtnStyle.MouseOn Then
_A = 200
End If
Dim _Lr As Integer = _ColorLight.R
Dim _Lg As Integer = _ColorLight.G
Dim _Lb As Integer = _ColorLight.B
_ColorLight = Color.FromArgb(_A, _Lr, _Lg, _Lb)
Dim _Dr As Integer = ColorItem(_Lr - 180)
Dim _Dg As Integer = ColorItem(_Lg - 180)
Dim _Db As Integer = ColorItem(_Lb - 180)
Dim _ColorDark As Color = Color.FromArgb(_A, _Dr, _Dg, _Db)
Dim _ColorTop As Color = _ColorLight
Dim _ColorBot As Color = _ColorDark
If _BtnStyle = BtnStyle.MouseDown Then
_ColorTop = _ColorDark
_ColorBot = _ColorLight
End If
_BrushColors = New Color() {_ColorTop, _ColorBot}
Return _BrushColors
End Function
Private Function ColorItem(ByVal _i As Integer) As Integer
Dim _ColorItem As Integer = 0
Select Case _i
Case 0 To 255
_ColorItem = _i
Case Is < 0
_ColorItem = 0
Case Is > 255
_ColorItem = 255
End Select
Return _ColorItem
End Function
#Region " ... Textes Properties ... "
Private _TextContents As String() = New String() {"", "", "", ""}
<Description("Styles Textes"), Category("Textes "), DefaultValue("")> _
Public Property TextContent() As String()
Get
Me.Btn.Refresh()
Return _TextContents
End Get
Set(ByVal value As String())
_TextContents = value
End Set
End Property
Private _Fonts As Font() = New Font() {MeFont, MeFont, MeFont, MeFont}
<Description("Styles Textes"), Category("Textes "), DefaultValue("")> _
Public Property TextFont() As Font()
Get
Return _Fonts
Me.Btn.Refresh()
End Get
Set(ByVal value As Font())
_Fonts = value
End Set
End Property
Private _TextColors As Color() = New Color() {MeForeColor, MeForeColor, MeForeColor, MeForeColor, MeForeColor}
<Description("Styles Textes"), Category("Textes "), DefaultValue("")> _
Public Property TextColor() As Color()
Get
Me.Btn.Refresh()
Return _TextColors
End Get
Set(ByVal value As Color())
_TextColors = value
End Set
End Property
Private _TextMargTops As Integer() = New Integer() {0, 0, 0, 0}
<Description("Styles Textes"), Category("Textes "), DefaultValue("")> _
Public Property TextMarginTop() As Integer()
Get
Return _TextMargTops
Me.Btn.Refresh()
End Get
Set(ByVal value As Integer())
_TextMargTops = value
End Set
End Property
Private _TextMargBots As Integer() = New Integer() {0, 0, 0, 0}
<Description("Styles Textes"), Category("Textes "), DefaultValue("")> _
Public Property TextMarginBottom() As Integer()
Get
Return _TextMargBots
Me.Btn.Refresh()
End Get
Set(ByVal value As Integer())
_TextMargBots = value
End Set
End Property
Private _TextMargLefts As Integer() = New Integer() {0, 0, 0, 0}
<Description("Styles Textes"), Category("Textes "), DefaultValue("")> _
Public Property TextMarginLeft() As Integer()
Get
Return _TextMargLefts
Me.Btn.Refresh()
End Get
Set(ByVal value As Integer())
_TextMargLefts = value
End Set
End Property
Private _TextMargRights As Integer() = New Integer() {0, 0, 0, 0}
<Description("Styles Textes"), Category("Textes "), DefaultValue("")> _
Public Property TextMarginRight() As Integer()
Get
Return _TextMargRights
Me.Btn.Refresh()
End Get
Set(ByVal value As Integer())
_TextMargRights = value
End Set
End Property
Private _AlignV As AlignV = AlignV.Top
<Flags()> Public Enum AlignV
Top = 0
Middle = 1
Bottom = 2
End Enum
Private _AlignVs As AlignV() = New AlignV() {AlignV.Top, AlignV.Top, AlignV.Top, AlignV.Top}
<Description("Styles Textes"), Category("Textes "), DefaultValue("")> _
Public Property TextAlignV() As AlignV()
Get
Return _AlignVs
Me.Btn.Refresh()
End Get
Set(ByVal value As AlignV())
_AlignVs = value
End Set
End Property
Private _AlignH As AlignH = AlignH.Left
<Flags()> Public Enum AlignH
Left = 0
Center = 1
Right = 2
End Enum
Private _AlignHs As AlignH() = New AlignH() {AlignH.Left, AlignH.Left, AlignH.Left, AlignH.Left}
<Description("Styles Textes"), Category("Textes "), DefaultValue("")> _
Public Property TextAlignH() As AlignH()
Get
Return _AlignHs
Me.Btn.Refresh()
End Get
Set(ByVal value As AlignH())
_AlignHs = value
End Set
End Property
#End Region
#Region " ... Items Properties ... "
Public Property BorderSize() As Integer
Get
Return Me.Btn.FlatAppearance.BorderSize
End Get
Set(ByVal value As Integer)
Me.Btn.FlatAppearance.BorderSize = value
End Set
End Property
Public Property BorderColor() As Color
Get
Return Me.Btn.FlatAppearance.BorderColor
End Get
Set(ByVal value As Color)
Me.Btn.FlatAppearance.BorderColor = value
End Set
End Property
Public Property ButtonPadding() As Padding
Get
Return Me.Btn.Margin
End Get
Set(ByVal value As Padding)
Me.Btn.Margin = value
End Set
End Property
#End Region
#Region " ... Child Events ... "
Private _BtnStyle As BtnStyle
Enum BtnStyle
MouseOff
MouseOn
MouseDown
End Enum
Public Shadows Event MouseClick(ByVal sender As Object, ByVal e As MouseEventArgs)
Private Sub Child_MouseClick(ByVal sender As Object, ByVal e As MouseEventArgs) _
Handles Pnl.MouseClick, Btn.MouseClick
'Me.Btn.Refresh()
'Dim _Sender As Control = CType(sender, Control)
RaiseEvent MouseClick(Me, e)
End Sub
Public Shadows Event MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs)
Private Sub Child_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Btn.MouseEnter
'_Btn_Style = 1
_BtnStyle = BtnStyle.MouseOn
Dim _Sender As Control = CType(sender, Control)
RaiseEvent MouseEnter(Me, e)
End Sub
Public Shadows Event MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs)
Private Sub Child_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Btn.MouseLeave
'_Btn_Style = 0
_BtnStyle = BtnStyle.MouseOff
RaiseEvent MouseLeave(Me, e)
End Sub
Public Shadows Event MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
Private Sub Btn_MouseDown(ByVal sender As System.Object, ByVal e As MouseEventArgs) _
Handles Btn.MouseDown
'_Btn_Style = 2
_BtnStyle = BtnStyle.MouseDown
RaiseEvent MouseDown(Me, e)
End Sub
Public Shadows Event MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
Private Sub Btn_MouseUp(ByVal sender As System.Object, ByVal e As MouseEventArgs) _
Handles Btn.MouseUp
'_Btn_Style = 1
_BtnStyle = BtnStyle.MouseOn
RaiseEvent MouseUp(Me, e)
End Sub
#End Region
End Class
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.