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 :

position drawline dans un rectangle


Sujet :

VB.NET

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éprouvé
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2018
    Messages
    323
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2018
    Messages : 323
    Par défaut position drawline dans un rectangle
    Bonsoir, je me suis crée une class trackbar mais j'ai un petit souci avec mon code Drawline.

    Nom : Capture d’écran 2021-12-17 172106.png
Affichages : 246
Taille : 1,6 Ko

    j'arrive pas positionner la valeur maximum jusqu'à la fin drawline comme sur la flèche rouge.

    mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Private Sub DrawLine(g As Graphics, Rect As Rectangle)
            g.DrawLine(New Pen(Brushes.Black, 1), New Point(CInt(Rect.X + 8), Rect.Height / 2), New Point(CInt(Rect.Width - 10), Rect.Height / 2))
            For i = Minimum To Maximum
                If i Mod 20 = 0 Then
                    g.DrawLine((New Pen(Brushes.Black, 1)), CInt((Rect.X + 8) + i), CInt(Rect.Height / 2), CInt((Rect.X + 8) + i), CInt(Rect.Height / 1.5))
                    g.DrawString(i.ToString, Me.Font, Brushes.Black, New Point(CInt((Rect.X + 8) + i), Rect.Height / 1.5), New StringFormat With {.Alignment = StringAlignment.Center})
                End If
            Next
        End Sub
    avez-vous une autre solution pour positionner la valeur maximum jusqu'à la fin drawline (flèche rouge comme l'image). merci d'avance

  2. #2
    Membre Expert Avatar de Phil Rob
    Homme Profil pro
    Retraité
    Inscrit en
    Novembre 2013
    Messages
    1 613
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Novembre 2013
    Messages : 1 613
    Par défaut
    Bonjour,
    Suis pas sûr de comprendre ...
    Mais la distance d entre chaque graduation égale la longueur de la ligne divisée par le nombre de graduations moins une unité. Une fois la grad. 0 placée, la graduation 1 se trouve d unités à droite, et ainsi de suite jusqu'à la valeur maximale.
    Selon ton illustration, tu places les valeurs de 0 à 100, au pas de 10, sur une ligne de 120 unités (par ex.). Tu places donc les valeurs 0, 10, ... 100, soit 11 valeurs.
    La distance d = 120 / (11 - 1), soit 12 unités.
    La grad 0 est à l'origine de la ligne, celle du 10 est 12 unités à droite, celle du 20 encore 12 unités de plus à droite, ... et ainsi de suite jusqu'à la grad 100 qui devrait tomber a l extrémité de la ligne.
    J'écris ceci en vitesse depuis un smartphone, je veux réviser si j'ai commis un bug ...
    J espère que ceci t'aidera
    ... )))

  3. #3
    Membre éprouvé
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2018
    Messages
    323
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2018
    Messages : 323
    Par défaut
    Bonjour, j'ai trouver la solution a mon problème ce matin. merci Phil Rob pour votre réponse.

    Nom : Capture d’écran 2021-12-18 100503.png
Affichages : 211
Taille : 2,0 Ko

    si vous-voulez mon code dit le moi. je laisse active pendant 1 semaine après je ferme comme résolu.

  4. #4
    Expert confirmé
    Avatar de ProgElecT
    Homme Profil pro
    Retraité
    Inscrit en
    Décembre 2004
    Messages
    6 132
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 69
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Retraité
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Décembre 2004
    Messages : 6 132
    Par défaut
    Salut
    Citation Envoyé par Miska59 Voir le message
    Bonjour, j'ai trouver la solution a mon problème ce matin.....
    si vous-voulez mon code dit le moi.....
    DVP est un site d'entraide et de partage, donc tu peux partager ton code

    Citation Envoyé par Miska59 Voir le message
    .... je laisse active pendant 1 semaine après je ferme comme résolu.
    Marquer une discussion comme résolue permet de faire une recherche sur des discussions résolues, quasi toutes les discussions sur DVP restent actives, résolues ou non, on peut même faire passer une discussion résolue en non résolue.
    :whistle:pourquoi pas, pour remercier, un :plusser: pour celui/ceux qui vous ont dépannés.
    saut de ligne
    OOOOOOOOO👉 → → Ma page perso sur DVP ← ← 👈

  5. #5
    Membre éprouvé
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2018
    Messages
    323
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2018
    Messages : 323
    Par défaut
    dans ma classe trackbar, vous pouvez choisir soit rectangle,cercle,customthumb.

    la classe trackbar :
    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
    Public Class Trackbar1 : Inherits Control
     
        Private _ThumbTwoColor As Boolean
        Public Property ThumbTwoColor As Boolean
            Get
                Return _ThumbTwoColor
            End Get
            Set(value As Boolean)
                _ThumbTwoColor = value
                Me.Invalidate()
            End Set
        End Property
     
        Private _TickFrequency As Integer
        Public Property TickFrequency As Integer
            Get
                Return _TickFrequency
            End Get
            Set(value As Integer)
                _TickFrequency = value
                Me.Invalidate()
            End Set
        End Property
     
        Private _minimum As Integer
        Public Property Minimum() As Integer
            Get
                Return _minimum
            End Get
            Set(ByVal value As Integer)
                _minimum = value
     
                If value > _maximum Then _maximum = value
                If value > _value Then _value = value
     
                Invalidate()
            End Set
        End Property
     
        Private _maximum As Integer
        Public Property Maximum() As Integer
            Get
                Return _maximum
            End Get
            Set(ByVal value As Integer)
                _maximum = value
     
                If value < _minimum Then _minimum = value
                If value < _value Then _value = value
     
                Invalidate()
            End Set
        End Property
     
        Event ValueChanged()
        Private _value As Integer
        Public Property Value() As Integer
            Get
                Return _value
            End Get
            Set(ByVal value As Integer)
                If value < _minimum Then
                    _value = _minimum
                ElseIf value > _maximum Then
                    _value = _maximum
                Else
                    _value = value
                End If
     
                Invalidate()
                RaiseEvent ValueChanged()
            End Set
        End Property
     
        Private _trackbar As TypeTrackBar
        Public Property MyCuseur As TypeTrackBar
            Get
                Return _trackbar
            End Get
            Set(value As TypeTrackBar)
                _trackbar = value
                Me.Invalidate()
            End Set
        End Property
     
        Private _FillColor As Color = Color.White
        Public Property FillColor As Color
            Get
                Return _FillColor
            End Get
            Set(value As Color)
                If value = Color.Transparent Then
                    Return
                Else
                    _FillColor = value
                    Me.Invalidate()
                End If
            End Set
        End Property
     
        Private _LineColor As Color = Color.Gray
        Public Property LineColor As Color
            Get
                Return _LineColor
            End Get
            Set(value As Color)
                If value = Color.Transparent Then
                    Return
                Else
                    _LineColor = value
                    Me.Invalidate()
                End If
            End Set
        End Property
     
        Private _CuseurColor2 As Color = Color.White
        Public Property CuseurColor2 As Color
            Get
                Return _CuseurColor2
            End Get
            Set(value As Color)
                _CuseurColor2 = value
                Me.Invalidate()
            End Set
        End Property
     
        Private _CuseurColor As Color = Color.DarkBlue
        Public Property CuseurColor As Color
            Get
                Return _CuseurColor
            End Get
            Set(value As Color)
                If value = Color.Transparent Then
                    Return
                Else
                    _CuseurColor = value
                    Me.Invalidate()
                End If
            End Set
        End Property
     
        Private Rect As New Rectangle
        Private Moving As Boolean = False
     
        Public Sub New()
            Me.SetStyle(ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
            Me.Size = New Size(210, 50)
            Me._trackbar = TypeTrackBar.Rectangle
            Me._maximum = 100
            Me._minimum = 0
            Me._TickFrequency = 1
            Me._ThumbTwoColor = True
        End Sub
     
        Protected Overrides Sub OnResize(ByVal e As EventArgs)
            MyBase.OnResize(e)
            If Me.Height <> 50 Then
                Me.Height = 50
            ElseIf Me.Width < 200 Then
                Me.Width = 200
            End If
        End Sub
     
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            MyBase.OnPaint(e)
            e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
            e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
            Rect = New Rectangle(0, 0, Me.Width, Me.Height)
            e.Graphics.FillRectangle(New SolidBrush(_FillColor), New Rectangle(0, 0, Rect.Width - 1, Rect.Height - 1))
            e.Graphics.DrawLine(New Pen(New SolidBrush(_LineColor), 5), New Point(5, 10), New Point(Rect.Width - 5, 10))
            Select Case _trackbar
                Case TypeTrackBar.Rectangle
                    e.Graphics.FillRectangle(New SolidBrush(_CuseurColor), New Rectangle(Rect.X + CInt(((Rect.Width - 20) / (Maximum - Minimum)) * Value), 0, 15, 25))
                Case TypeTrackBar.Circle
                    e.Graphics.FillEllipse(New SolidBrush(_CuseurColor), (Rect.X - 1) + CInt(((Rect.Width - 20) / (Maximum - Minimum)) * Value), 0, 20, 20)
                Case TypeTrackBar.CustomThumb
                    Dim size As New Size((Rect.X + 15) + CInt(((Rect.Width - 20) / (Maximum - Minimum)) * Value), Rect.Height - 20)
                    e.Graphics.FillPolygon(New SolidBrush(_CuseurColor), New Point() {New Point(size.Width - 15, 1), New Point(size.Width, 1), New Point(size.Width, size.Height - 12), New Point((size.Width - 7), size.Height - 6), New Point(size.Width - 15, size.Height - 12)})
                    If _ThumbTwoColor Then
                        e.Graphics.FillPolygon(New SolidBrush(_CuseurColor2), New Point() {New Point(size.Width - 15, 4), New Point(size.Width, 4), New Point(size.Width, size.Height - 16), New Point((size.Width - 7), size.Height - 7), New Point(size.Width - 15, size.Height - 16)})
                    End If
            End Select
            e.Graphics.DrawLine(New Pen(New SolidBrush(_CuseurColor), 5), New Point(0, 10), New Point(CInt(((Rect.Width - 20) / (Maximum - Minimum)) * Value), 10))
            DrawLine(e.Graphics, Rect)
        End Sub
     
        Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
            If Rect.Contains(e.Location) Then
                Dim mp = New Rectangle(New Point(e.Location.X, e.Location.Y), New Size(1, 1))
                If New Rectangle(New Point(Rect.X + CInt(((Rect.Width - 20) / (Maximum - Minimum)) * Value), 0), New Size(Rect.Width, Height)).IntersectsWith(mp) Then
                    Moving = True
                End If
            End If
        End Sub
        Protected Overrides Sub OnMouseup(ByVal e As System.Windows.Forms.MouseEventArgs)
            MyBase.OnMouseUp(e)
            Moving = False
        End Sub
        Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
            If Moving Then
                Dim mp As Point = New Point(e.X, e.Y)
                Value = CInt(Maximum * ((mp.X - Rect.X) / Rect.Width))
            End If
        End Sub
     
        Public Enum TypeTrackBar
            Rectangle = 0
            Circle = 1
            CustomThumb = 2
        End Enum
     
        Private Sub DrawLine(g As Graphics, Rect As Rectangle)
            Dim tickFrequencySize As Double = ((Size.Width - 20) * Me.TickFrequency / (Me.Maximum - Me.Minimum))
            Dim tickCount As Integer = CInt(((Me.Maximum - Me.Minimum) / Me.TickFrequency)) + 1
            g.DrawLine(New Pen(Brushes.Black, 1), New Point(CInt(Rect.X + 8), Rect.Height / 2), New Point(CInt(Rect.Width - 12), Rect.Height / 2))
            For i = 0 To tickCount
                If i Mod 10 = 0 Then
                    g.DrawLine((New Pen(Brushes.Black, 1)), CInt((Rect.X + 8) + tickFrequencySize * i), CInt(Rect.Height / 2), CInt((Rect.X + 8) + tickFrequencySize * i), CInt(Rect.Height / 1.5))
                    g.DrawString(i.ToString, Me.Font, Brushes.Black, New Point(CInt((Rect.X + 8) + tickFrequencySize * i), Rect.Height / 1.5), New StringFormat With {.Alignment = StringAlignment.Center})
                End If
            Next
        End Sub
     
    End Class

  6. #6
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    bonjour

    La variation va de mini a maxi ce qui fixe les graduations
    quant au positions ,elle dependent de P1 et P2 c.à.de l' ecart DX qui est egal à : (p2.X - p1.X) / (Maximum - Minimum).
    cet ecart doit etre "injecté" dans le calcul des coordonnées X .
    Code .vb revu :
    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
     
      Private Sub DrawLine(ByVal gr As Graphics, ByVal Rect As RectangleF)
            Dim p1 As Point = New Point(CInt(Rect.X + 8), Rect.Height / 2)
            Dim p2 As Point = New Point(CInt(Rect.Width - 10), Rect.Height / 2)
     
       Dim dx As Single = (p2.X - p1.X) / (Maximum - Minimum)
     
            gr.DrawLine(New Pen(Brushes.Black, 1), p1, p2)
     
            For i As Integer = Minimum To Maximum
                If i Mod 20 = 0 Then
                    gr.DrawLine((New Pen(Brushes.Black, 1)), CInt((Rect.X + 8) + i * dx), CInt(Rect.Height / 2), CInt((Rect.X + 8) + i * dx), CInt(Rect.Height / 1.5))
                    gr.DrawString(i.ToString, Me.Font, Brushes.Black, New Point(CInt((Rect.X + 8) + i * dx), Rect.Height / 1.5), New StringFormat With {.Alignment = StringAlignment.Center})
                End If
            Next
     
            'For i As Integer = Minimum To Maximum
            '    If i Mod 20 = 0 Then
            '        gr.DrawLine((New Pen(Brushes.Black, 1)), CInt((Rect.X + 8) + i), CInt(Rect.Height / 2), CInt((Rect.X + 8) + i), CInt(Rect.Height / 1.5))
            '        gr.DrawString(i.ToString, Me.Font, Brushes.Black, New Point(CInt((Rect.X + 8) + i), Rect.Height / 1.5), New StringFormat With {.Alignment = StringAlignment.Center})
            '    End If
            'Next
        End Sub
    bon code....

  7. #7
    Membre Expert Avatar de Phil Rob
    Homme Profil pro
    Retraité
    Inscrit en
    Novembre 2013
    Messages
    1 613
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Novembre 2013
    Messages : 1 613
    Par défaut
    Bravo
    Très bon week-end !

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

Discussions similaires

  1. [Opera 7 & 8] Position absolue dans conteneur relatif
    Par Sub0 dans le forum Balisage (X)HTML et validation W3C
    Réponses: 16
    Dernier message: 16/08/2005, 23h16
  2. [AS2] Position clip dans attachMovie
    Par ooyeah dans le forum ActionScript 1 & ActionScript 2
    Réponses: 2
    Dernier message: 07/07/2005, 11h10
  3. Dessin de la partie d'une ligne passant dans un rectangle
    Par Invité dans le forum Algorithmes et structures de données
    Réponses: 8
    Dernier message: 13/06/2005, 09h35
  4. [MFC]Ecrire du texte dans un rectangle
    Par zaz16 dans le forum MFC
    Réponses: 8
    Dernier message: 29/07/2003, 10h31
  5. Comment centrer un Texte dans un rectangle ...
    Par Djedjeridoo dans le forum Composants VCL
    Réponses: 3
    Dernier message: 16/06/2003, 21h56

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