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

Windows Forms Discussion :

Panel avec un dégradé de couleur [Débutant]


Sujet :

Windows Forms

  1. #1
    Candidat au Club
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Novembre 2014
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux
    Secteur : Transports

    Informations forums :
    Inscription : Novembre 2014
    Messages : 9
    Points : 3
    Points
    3
    Par défaut Panel avec un dégradé de couleur
    Bonjour

    j'ai créé un usercontrol permettant d'avoir un espèce de panel avec un dégradé de couleur
    mon problème est que si je dépose un label dessus il prend la couleur dégradé de mon usercontrol et il perd son transparence
    voici le resultat Nom : dégradé.PNG
Affichages : 872
Taille : 2,8 Ko

    voici mon code merci de me dire ce qu'il ne va pas
    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
     
    Imports System.Drawing.Drawing2D
    Imports System.ComponentModel
    Imports System.ComponentModel.Design
     
    <Designer("System.Windows.Forms.Design.ParentControlDesigner,System.Design", GetType(IDesigner))> _
    Public Class PanelPerso2
        Inherits Control
     
     
        Private _borderColor As Color = Color.MidnightBlue
        Public Property BorderColor() As Color
            Get
                Return _borderColor
            End Get
            Set(ByVal Value As Color)
                _borderColor = Value
                Invalidate()
            End Set
        End Property
        Private Function GetRoundedRectPath(ByVal rect As Rectangle, ByVal radius As Integer) As GraphicsPath
            Dim diameter As Integer = 2 * radius
            Dim arcRect As New Rectangle(rect.Location, New Size(diameter, diameter))
            Dim path As New GraphicsPath
            path.AddArc(arcRect, 180, 90)
            arcRect.X = rect.Right - diameter
            path.AddArc(arcRect, 270, 90)
            arcRect.Y = rect.Bottom - diameter
            path.AddArc(arcRect, 0, 90)
            arcRect.X = rect.Left
            path.AddArc(arcRect, 90, 90)
            path.CloseFigure()
            Return path
        End Function
     
     
        Private Sub PanelPerso_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
            On Error Resume Next
            Dim gp As GraphicsPath
            Dim Monrec As Rectangle = ClientRectangle
            gp = GetRoundedRectPath(New Rectangle(Monrec.X + 2, Monrec.Y + 2, Monrec.Width - 4, Monrec.Height - 4), 20)
            Dim lgb As System.Drawing.Drawing2D.LinearGradientBrush = New System.Drawing.Drawing2D.LinearGradientBrush(e.ClipRectangle, Color.White, _borderColor, System.Drawing.Drawing2D.LinearGradientMode.Vertical)
            e.Graphics.FillPath(lgb, gp)
            e.Graphics.DrawPath(New Pen(_borderColor, 1), gp)
            ' e.Graphics.DrawPath(New Pen(Brushes.Gray, _BorderTaille), gp)
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
            lgb.Dispose()
     
            'ControlPaint.DrawBorder(e.Graphics, Monrec, Color.Red, ButtonBorderStyle.Dashed)
        End Sub

  2. #2
    Membre chevronné

    Homme Profil pro
    Appui fonctionnel senior
    Inscrit en
    Juin 2007
    Messages
    461
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Appui fonctionnel senior
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2007
    Messages : 461
    Points : 2 211
    Points
    2 211
    Par défaut
    Bonjour,

    Je suis un peu rouillé en UserControl WinForms, mais il me semble que la solution à ce problème est assez simple.

    Tout d'abord, commence par créer un nouveau UserControl qui hérite de Label. Ensuite, rajoute le code suivant dans cette classe :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    protected override CreateParams CreateParams
        {
          get
          {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x20;
            return cp;
          }
        }
    Je pense que tu n'auras pas trop de mal à convertir

    Et voilà ! Il ne te restera ensuite plus qu'à utiliser cette classe au lieu de la classe Label de base.
    Si tu aimes l'anglais, voici un exemple plus complet : http://www.doogal.co.uk/transparent.php

  3. #3
    Candidat au Club
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Novembre 2014
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux
    Secteur : Transports

    Informations forums :
    Inscription : Novembre 2014
    Messages : 9
    Points : 3
    Points
    3
    Par défaut
    en vb jai déjà testé ça donne
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
            Get
                Dim cp As CreateParams = MyBase.CreateParams
                cp.ExStyle = &H20
                Return cp
            End Get
        End Property

    mais ce n'est pas un label que je veux c un panel et le code ne sert a rien dans ce cas

    jai remarqué que si je dessine un simple rectangle le label est bien transparent
    Nom : Capture.JPG
Affichages : 756
Taille : 11,3 Ko

    mais si je dessine un GraphicsPath la transparence ne marche pas


  4. #4
    Expert éminent Avatar de Graffito
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    5 993
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 5 993
    Points : 7 903
    Points
    7 903
    Par défaut
    Sélectionner le Label en Design (concepteur de vues) et ...
    définir sa propriété BackColor en choisissant "Transparent" dans l'onglet "Web".
    " Le croquemitaine ! Aaaaaah ! Où ça ? " ©Homer Simpson

  5. #5
    Candidat au Club
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Novembre 2014
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux
    Secteur : Transports

    Informations forums :
    Inscription : Novembre 2014
    Messages : 9
    Points : 3
    Points
    3
    Par défaut
    jai deja testé marche po

  6. #6
    Expert éminent Avatar de Graffito
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    5 993
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 5 993
    Points : 7 903
    Points
    7 903
    Par défaut
    Pour ce type de panel, j'ai défini un UserControl qui hérite d'un Panel, ce qui donne en C# (j'ai extrait le code relatif au dégradé):
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
      internal class SxGradientPanel : Panel 
      { 
        internal Color BackColor2 = Color.FromArgb(240,240,240) ; 
     
        protected override void OnPaintBackground(PaintEventArgs e) 
        { 
          Brush TheBrush=new System.Drawing.Drawing2D.LinearGradientBrush(new Point(0,0), new Point (0,Height),BackColor, BackColor2) ;
          e.Graphics.FillRectangle(TheBrush,new Rectangle(0, 0, Width, Height));
        }
     }
    Et là, le BackColor "Transparent" marche.
    " Le croquemitaine ! Aaaaaah ! Où ça ? " ©Homer Simpson

  7. #7
    Candidat au Club
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Novembre 2014
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux
    Secteur : Transports

    Informations forums :
    Inscription : Novembre 2014
    Messages : 9
    Points : 3
    Points
    3
    Par défaut
    jai remarqué que si je dessine un simple rectangle le label est bien transparent*
    Pièce jointe 162475

    mais si je dessine un GraphicsPath la transparence ne marche pas

  8. #8
    Candidat au Club
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Novembre 2014
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux
    Secteur : Transports

    Informations forums :
    Inscription : Novembre 2014
    Messages : 9
    Points : 3
    Points
    3
    Par défaut
    bon j'ai enfin réussi
    je poste mon code sa peut servir c'est mon cadeau pr mon premier poste lol
    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
    Public Class Class1
        Inherits System.Windows.Forms.Panel
        Private _Couleur1 As Color = Color.Red
        <System.ComponentModel.DefaultValueAttribute(GetType(System.Drawing.Color), "Window"), _
            System.ComponentModel.CategoryAttribute("Appearance")> _
        Public Property Couleur1() As Color
            Get
                Return _Couleur1
            End Get
            Set(ByVal Value As Color)
                _Couleur1 = Value
                Invalidate()
            End Set
        End Property
     
     
        Private _Couleur2 As Color = Color.White
        <System.ComponentModel.DefaultValueAttribute(GetType(System.Drawing.Color), "Window"), _
            System.ComponentModel.CategoryAttribute("Appearance")> _
        Public Property Couleur2() As Color
            Get
                Return _Couleur2
            End Get
            Set(ByVal Value As Color)
                _Couleur2 = Value
                Invalidate()
            End Set
        End Property
     
        <Flags()> _
        Public Enum Coins
            None = 0
            TopLeft = 1
            TopRight = 2
            BottomLeft = 4
            BottomRight = 8
            All = TopLeft Or TopRight Or BottomLeft Or BottomRight
            AllTop = TopLeft Or TopRight
            AllLeft = TopLeft Or BottomLeft
            AllRight = TopRight Or BottomRight
            AllBottom = BottomLeft Or BottomRight
        End Enum
     
        Private _Coin As Coins = Coins.All
        <System.ComponentModel.DefaultValueAttribute(GetType(Coins), "All"), _
         System.ComponentModel.CategoryAttribute("Appearance")> _
        Public Property Coin() As Coins
            Get
                Return Me._Coin
            End Get
            Set(ByVal Value As Coins)
                Me._Coin = Value
                If Me.DesignMode = True Then
                    Me.Invalidate()
                End If
            End Set
        End Property
        Private _CoinTaille As Integer = 30
        <System.ComponentModel.DefaultValueAttribute(GetType(Integer), "30"), _
         System.ComponentModel.CategoryAttribute("Appearance"), _
         System.ComponentModel.DescriptionAttribute("The radius of the curve used to paint the corners of the control.")> _
        Public Property Cointaille() As Integer
            Get
                Return Me._CoinTaille
            End Get
            Set(ByVal Value As Integer)
                Me._CoinTaille = Value
                If Me.DesignMode = True Then
                    Me.Invalidate()
                End If
            End Set
        End Property
        Public Function RoundCorners(ByVal Rectangle As RectangleF, Optional ByVal Radius As Integer = 5, Optional ByVal Corners As Coins = Coins.All) As Drawing2D.GraphicsPath
            Dim p As New Drawing2D.GraphicsPath
            Dim x As Single = Rectangle.X
            Dim y As Single = Rectangle.Y
            Dim w As Single = Rectangle.Width
            Dim h As Single = Rectangle.Height
            Dim r As Integer = Radius
            p.StartFigure()
            If CBool(Corners And Coins.TopLeft) Then
                p.AddArc(New RectangleF(x, y, 2 * r, 2 * r), 180, 90)
            Else
                p.AddLine(New PointF(x, y + r), New PointF(x, y))
                p.AddLine(New PointF(x, y), New PointF(x + r, y))
            End If
            p.AddLine(New PointF(x + r, y), New PointF(x + w - r, y))
            If CBool(Corners And Coins.TopRight) Then
                p.AddArc(New RectangleF(x + w - 2 * r, y, 2 * r, 2 * r), 270, 90)
            Else
                p.AddLine(New PointF(x + w - r, y), New PointF(x + w, y))
                p.AddLine(New PointF(x + w, y), New PointF(x + w, y + r))
            End If
            p.AddLine(New PointF(x + w, y + r), New PointF(x + w, y + h - r))
            If CBool(Corners And Coins.BottomRight) Then
                p.AddArc(New RectangleF(x + w - 2 * r, y + h - 2 * r, 2 * r, 2 * r), 0, 90)
            Else
                p.AddLine(New PointF(x + w, y + h - r), New PointF(x + w, y + h))
                p.AddLine(New PointF(x + w, y + h), New PointF(x + w - r, y + h))
            End If
            p.AddLine(New PointF(x + w - r, y + h), New PointF(x + r, y + h))
            If CBool(Corners And Coins.BottomLeft) Then
                p.AddArc(New RectangleF(x, y + h - 2 * r, 2 * r, 2 * r), 90, 90)
            Else
                p.AddLine(New PointF(x + r, y + h), New PointF(x, y + h))
                p.AddLine(New PointF(x, y + h), New PointF(x, y + h - r))
            End If
            p.AddLine(New PointF(x, y + h - r), New PointF(x, y + r))
            p.CloseFigure()
            Return p
        End Function
        Protected Overrides Sub OnPaintBackGround(ByVal e As System.Windows.Forms.PaintEventArgs)
            MyBase.OnPaintBackground(e)
            Dim gp As System.Drawing.Drawing2D.GraphicsPath
            Dim Monrec As Rectangle = ClientRectangle
            'gp = RoundCorners(New Rectangle(Monrec.X + 2, Monrec.Y + 2, Monrec.Width - 4, Monrec.Height - 4), _CoinTaille, _Coin)
            'Dim lgb As System.Drawing.Drawing2D.LinearGradientBrush = New System.Drawing.Drawing2D.LinearGradientBrush(New Rectangle(Monrec.X + 2, Monrec.Y + 2, Monrec.Width - 4, Monrec.Height - 4), _Couleur2, _Couleur1, System.Drawing.Drawing2D.LinearGradientMode.Vertical)
            gp = RoundCorners(Monrec, _CoinTaille, _Coin)
            Dim lgb As System.Drawing.Drawing2D.LinearGradientBrush = New System.Drawing.Drawing2D.LinearGradientBrush(Monrec, _Couleur2, _Couleur1, System.Drawing.Drawing2D.LinearGradientMode.Vertical)
     
            e.Graphics.FillPath(lgb, gp)
            e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
            gp.Dispose()
            lgb.Dispose()
        End Sub
     
     
     
    End Class

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

Discussions similaires

  1. Faire un dégradé de couleur avec du texte
    Par loic20h28 dans le forum Général JavaScript
    Réponses: 22
    Dernier message: 31/01/2010, 23h09
  2. Dégradé de couleurs avec texte
    Par Hobbi1 dans le forum Windows Forms
    Réponses: 12
    Dernier message: 21/06/2009, 19h52
  3. Réponses: 2
    Dernier message: 28/05/2009, 10h48
  4. Réponses: 1
    Dernier message: 19/08/2008, 12h15
  5. Dégradé sur une chaine de caractères avec 3 a 4 couleurs
    Par avogadro dans le forum Composants VCL
    Réponses: 6
    Dernier message: 31/03/2006, 14h21

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