IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

Comment arrondir les bordures


Sujet :

VB.NET

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2017
    Messages
    24
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 23
    Localisation : Cameroun

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Service public

    Informations forums :
    Inscription : Mai 2017
    Messages : 24
    Points : 33
    Points
    33
    Par défaut Comment arrondir les bordures
    Salut à tous... Pour plus d'esthétique, j'aimerai bien savoir comment on fait pour arrondir les bordures d'un widget quelconque... Surtout les boutons et les textBox

  2. #2
    Expert éminent sénior Avatar de Pol63
    Homme Profil pro
    .NET / SQL SERVER
    Inscrit en
    Avril 2007
    Messages
    14 154
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : .NET / SQL SERVER

    Informations forums :
    Inscription : Avril 2007
    Messages : 14 154
    Points : 25 072
    Points
    25 072
    Par défaut
    winforms ? wpf ? asp.net ? autre ?
    Cours complets, tutos et autres FAQ ici : C# - VB.NET

  3. #3
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut
    bonjour

    Un control,un bouton avec bordures rondes /
    voici 2 codes exemples .vb qui feront peut être ton bonheur !
    le premier concerne un simple Control (héritant de Control)
    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
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
     
    Imports System.ComponentModel
    Imports System.Drawing.Drawing2D
     
    Public Class MyControl
        Inherits Control
     
        Private m_borderRadius As Single = 20
        <Browsable(True)>
        Public Property BorderRadius As Single
            Get
                Return m_borderRadius
            End Get
            Set(value As Single)
                m_borderRadius = value
                Me.Invalidate()
            End Set
        End Property
        Private m_color1 As Color = Color.Magenta
        <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.Yellow
        <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
        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 OnPaint(e As System.Windows.Forms.PaintEventArgs)
            MyBase.OnPaint(e)
            Dim gr As Graphics = e.Graphics
            gr.SmoothingMode = SmoothingMode.AntiAlias
            DrawButton(gr, 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, 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
                Using br As New LinearGradientBrush(
                 rc, Color1, Color2,
                 LinearGradientMode.Vertical, True)
                    grfx.FillPath(br, controlPath)
                End Using
     
                '  border
                grfx.DrawPath(New Pen(BorderColor, 2.0), controlPath)
     
     
            End Using
     
        End Sub
    End Class
    le second concerne un Control (héritant de Button) est plus versatile car il doit gérer le Focus et l’état "pressé" ou" non pressé"
    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
    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
    Bon code...

  4. #4
    Nouveau membre du Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2017
    Messages
    24
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 23
    Localisation : Cameroun

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Service public

    Informations forums :
    Inscription : Mai 2017
    Messages : 24
    Points : 33
    Points
    33
    Par défaut Merci
    Merci bien. Très sympa ton code !!!!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Comment supprimer les bordures des TabPages
    Par Cedric33 dans le forum Windows Forms
    Réponses: 2
    Dernier message: 26/03/2009, 15h02
  2. Comment voir les bordures d'un tableau
    Par oriental dans le forum Balisage (X)HTML et validation W3C
    Réponses: 2
    Dernier message: 19/10/2008, 16h47
  3. [C#] Comment arrondir les angles d'un Label avec borderRadius ?
    Par Anto03 dans le forum Windows Presentation Foundation
    Réponses: 3
    Dernier message: 29/09/2008, 15h47
  4. Arrondir les bordures d'un tableau
    Par koKoTis dans le forum Mise en page CSS
    Réponses: 3
    Dernier message: 01/12/2007, 16h50
  5. [HTML] Arrondir les bordures d’un tableau
    Par Furius dans le forum Balisage (X)HTML et validation W3C
    Réponses: 12
    Dernier message: 11/01/2006, 08h41

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo