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 :

Bordures arrondies sur un panel


Sujet :

Windows Forms

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    25
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Janvier 2011
    Messages : 25
    Par défaut Bordures arrondies sur un panel
    Bonjour,
    J'ai un projet de stage qui consiste à faire une maquette d'application en client lourd et je souhaiterais améliorer le design en mettant des bordures arrondies sur mes panel (entre autre).
    J'ai pas mal chercher mais je n'ai rien trouvé qui aborde ce sujet.

    Une autre idée que je veux mettre en place est l'ajout d'une croix de fermeture dans l'onglet d'un tabPage.
    j'ai trouvé quelques sujets qui en parle mais apparemment je n'ai pas le niveau, si vous avez quelques exemple simple et concret ce serait génial!

    Pour info, je suis sous VS2010 et je développe en WinForm.

    Merci d'avance pour votre aide.

  2. #2
    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 control panel avec bordure
    Bonjour eternal_winds
    Pour avoir des bordures arrondies sur control ,il fau customiser le panel en le redessinant dans sa methode paint qu'il faut "overrider" ou surcharger.
    Ensuite dessiner les 4 cotes et les 4 arc des bords avec la classe GraphicsPath GDI.
    La classe Graphics sait "composer" les figures complexes avec ses GraphicsPath.StartFigure(), GraphicsPath.AddArc(),GraphicsPath.AddLine. ..

    Sans oublier aussi que le systeme GDI gere les coordonnees positives de l'axe Y vers le bas et que les angles positifs sont comptes dans le sens-antihoraire pour rappel.

    Enfin ne pas oublier d'assigner le path dessine à la propriete Region du controle customise qui indiquera à window que les clics souris ne seront pas interceptes en dehors du path dessine.

    Rajouter les proprietes personalisables par l'utilisateur :rayon des bords,epaisseur des trait etc.......
    voici un premier exemple ShapedPanel tres simple pour se familiariser avec la methode en vb.net:
    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
     
    Imports System
    Imports System.Diagnostics
    Imports System.ComponentModel
    Imports System.Windows.Forms
    Imports System.Drawing
    Imports System.Drawing.Drawing2D
    Public Class ShapedPanel
        Private pen As Pen = New Pen(_borderColor, penWidth)
        Private Shared ReadOnly penWidth As Single = 2.0F
     
        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 _borderColor As Color = Color.White
        <Browsable(True)> _
        Public Property BorderColor() As Color
            Get
                Return _borderColor
            End Get
            Set(ByVal Value As Color)
                _borderColor = Value
                pen = New Pen(_borderColor, penWidth)
                Invalidate()
            End Set
        End Property
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            MyBase.OnPaint(e)
            'Ajoutez ici votre code de dessin personnalisé
            ExtendedDraw(e)
            DrawBorder(e.Graphics)
        End Sub
     
     
    #Region "Methods"
        Private Sub ExtendedDraw(ByVal e As PaintEventArgs)
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
            Dim path As GraphicsPath = New GraphicsPath()
     
            path.StartFigure()
            path.StartFigure()
            path.AddArc(GetLeftUpper(Edge), 180, 90)
            path.AddLine(Edge, 0, Width - Edge, 0)
            path.AddArc(GetRightUpper(Edge), 270, 90)
            path.AddLine(Width, Edge, Width, Height - Edge)
            path.AddArc(GetRightLower(Edge), 0, 90)
            path.AddLine(Width - Edge, Height, Edge, Height)
            path.AddArc(GetLeftLower(Edge), 90, 90)
            path.AddLine(0, Height - Edge, 0, Edge)
            path.CloseFigure()
            Region = New Region(path)
        End Sub
     
     
        Private Sub DrawBorder(ByVal graphics As Graphics)
            DrawSingleBorder(graphics)
        End Sub
        Private Sub DrawSingleBorder(ByVal graphics As Graphics)
            graphics.DrawArc(pen, New Rectangle(0, 0, Edge, Edge), _
                             180, 90)
            graphics.DrawArc(pen, New Rectangle(Width - Edge - 1, -1, _
                             Edge, Edge), 270, 90)
            graphics.DrawArc(pen, New Rectangle(Width - Edge - 1, _
                             Height - Edge - 1, Edge, Edge), 0, 90)
            graphics.DrawArc(pen, New Rectangle(0, Height - Edge - 1, _
                             Edge, Edge), 90, 90)
            graphics.DrawRectangle(pen, 0.0F, 0.0F, CType((Width - 1), _
                                   Single), CType((Height - 1), Single))
        End Sub
        Private Function GetLeftUpper(ByVal e As Integer) As Rectangle
            Return New Rectangle(0, 0, e, e)
        End Function
     
        Private Function GetRightUpper(ByVal e As Integer) As Rectangle
            Return New Rectangle(Width - e, 0, e, e)
        End Function
     
        Private Function GetRightLower(ByVal e As Integer) As Rectangle
            Return New Rectangle(Width - e, Height - e, e, e)
        End Function
     
        Private Function GetLeftLower(ByVal e As Integer) As Rectangle
            Return New Rectangle(0, Height - e, e, e)
        End Function
    #End Region
    #Region "Properties"
        Private _edge As Integer = 50
        <Browsable(True)> _
        Public Property Edge() As Integer
            Get
                Return _edge
            End Get
            Set(ByVal Value As Integer)
                _edge = Value
                Invalidate()
            End Set
        End Property
    #End Region
     
     
     
    End Class
    voici un deuxieme exemple A1Panel plus elabore avec la methode SetStyle() qui accelere le temps de dessin du controle en csharp.net:
    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
     
    //You can customize follwing things in this panel 
    //•Gradeint Start Color
    //•Gradient End Color
    //•Border Color
    //•Border Width
    //•Shadow Offset
    //•Round corner radius
    //•Panel Image
    //•Panel Image location
    //.....
    using System;
    using System.ComponentModel;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
    using System.Drawing.Drawing2D;
     
    namespace A1PanelCSharp
    {
        // A1Panel class
        public partial class A1Panel : Panel
        {
            int _borderWidth = 1;
            [Browsable(true), Category(A1PanelGlobals.A1Category)]
            [DefaultValue(1)]
            public int BorderWidth
            {
                get { return _borderWidth; }
                set { _borderWidth = value; Invalidate(); }
            }
     
            int _shadowOffSet = 5;
            [Browsable(true), Category(A1PanelGlobals.A1Category)]
            [DefaultValue(5)]
            public int ShadowOffSet
            {
                get
                {
                    return _shadowOffSet;
                }
                set { _shadowOffSet = Math.Abs(value); Invalidate(); }
            }
     
            int _roundCornerRadius = 4;
            [Browsable(true), Category(A1PanelGlobals.A1Category)]
            [DefaultValue(4)]
            public int RoundCornerRadius
            {
                get { return _roundCornerRadius; }
                set { _roundCornerRadius = Math.Abs(value); Invalidate(); }
            }
     
            Image _image;
            [Browsable(true), Category(A1PanelGlobals.A1Category)]
            public Image Image
            {
                get { return _image; }
                set { _image = value; Invalidate(); }
            }
     
            Point _imageLocation = new Point(4, 4);
            [Browsable(true), Category(A1PanelGlobals.A1Category)]
            [DefaultValue("4,4")]
            public Point ImageLocation
            {
                get { return _imageLocation; }
                set { _imageLocation = value; Invalidate(); }
            }
     
            Color _borderColor = Color.Gray;
            [Browsable(true), Category(A1PanelGlobals.A1Category)]
            [DefaultValue("Color.Gray")]
            public Color BorderColor
            {
                get { return _borderColor; }
                set { _borderColor = value; Invalidate(); }
            }
     
            Color _gradientStartColor = Color.White;
            [Browsable(true), Category(A1PanelGlobals.A1Category)]
            [DefaultValue("Color.White")]
            public Color GradientStartColor
            {
                get { return _gradientStartColor; }
                set { _gradientStartColor = value; Invalidate(); }
            }
     
            Color _gradientEndColor = Color.Gray;
            [Browsable(true), Category(A1PanelGlobals.A1Category)]
            [DefaultValue("Color.Gray")]
            public Color GradientEndColor
            {
                get { return _gradientEndColor; }
                set { _gradientEndColor = value; Invalidate(); }
            }
     
            public A1Panel()
            {
                this.SetStyle(ControlStyles.DoubleBuffer, true);
                this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
                this.SetStyle(ControlStyles.ResizeRedraw, true);
                this.SetStyle(ControlStyles.UserPaint, true);
                this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
                InitializeComponent();
            }
     
            protected override void OnPaintBackground(PaintEventArgs e)
            {
                base.OnPaintBackground(e);
     
                int tmpShadowOffSet = Math.Min(Math.Min(_shadowOffSet, this.Width - 2), this.Height - 2);
                int tmpSoundCornerRadius = Math.Min(Math.Min(_roundCornerRadius, this.Width - 2), this.Height - 2);
                if (this.Width > 1 && this.Height > 1)
                {
                    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
     
                    Rectangle rect = new Rectangle(0, 0, this.Width - tmpShadowOffSet - 1,
                                       this.Height - tmpShadowOffSet - 1);
     
                    Rectangle rectShadow = new Rectangle(tmpShadowOffSet, tmpShadowOffSet,
                                       this.Width - tmpShadowOffSet - 1, this.Height - tmpShadowOffSet - 1);
     
                    GraphicsPath graphPathShadow = A1PanelGraphics.GetRoundPath(rectShadow, tmpSoundCornerRadius);
                    GraphicsPath graphPath = A1PanelGraphics.GetRoundPath(rect, tmpSoundCornerRadius);
     
                    if (tmpSoundCornerRadius > 0)
                    {
                        using (PathGradientBrush gBrush = new PathGradientBrush(graphPathShadow))
                        {
                            gBrush.WrapMode = WrapMode.Clamp;
                            ColorBlend colorBlend = new ColorBlend(3);
                            colorBlend.Colors = new Color[]{Color.Transparent,
                    Color.FromArgb(180, Color.DimGray),
                    Color.FromArgb(180, Color.DimGray)};
     
                            colorBlend.Positions = new float[] { 0f, .1f, 1f };
     
                            gBrush.InterpolationColors = colorBlend;
                            e.Graphics.FillPath(gBrush, graphPathShadow);
                        }
                    }
     
                    // Draw backgroup
                    LinearGradientBrush brush = new LinearGradientBrush(rect,
                    this._gradientStartColor,
                    this._gradientEndColor,
                    LinearGradientMode.BackwardDiagonal);
                    e.Graphics.FillPath(brush, graphPath);
                    e.Graphics.DrawPath(new Pen(Color.FromArgb(180, this._borderColor), _borderWidth), graphPath);
     
                    // Draw Image
                    if (_image != null)
                    {
                        e.Graphics.DrawImageUnscaled(_image, _imageLocation);
                    }
                }
            }
        }
        // A1PanelGraphics class
        internal class A1PanelGraphics
        {
            public static GraphicsPath GetRoundPath(Rectangle r, int depth)
            {
                GraphicsPath graphPath = new GraphicsPath();
     
                graphPath.AddArc(r.X, r.Y, depth, depth, 180, 90);
                graphPath.AddArc(r.X + r.Width - depth, r.Y, depth, depth, 270, 90);
                graphPath.AddArc(r.X + r.Width - depth, r.Y + r.Height - depth, depth, depth, 0, 90);
                graphPath.AddArc(r.X, r.Y + r.Height - depth, depth, depth, 90, 90);
                graphPath.AddLine(r.X, r.Y + r.Height - depth, r.X, r.Y + depth / 2);
     
                return graphPath;
            }
        }
     
        // A1PanelGlobals class
        internal class A1PanelGlobals
        {
            public const string A1Category = "A1";
        }
     
    }
    Generer le projet et les 2 controles apparaissent dans la toolboox pret pour etre droppe sur un winform...............
    bon code............

Discussions similaires

  1. Probleme de bordure sur mon Panel
    Par JLuc01 dans le forum VB.NET
    Réponses: 2
    Dernier message: 09/12/2013, 20h05
  2. [Math]Problème troncage ou arrondi sur des valeurs
    Par Carrel dans le forum Général Java
    Réponses: 6
    Dernier message: 07/10/2009, 15h11
  3. bordure arrondie sur bouton
    Par barbiche dans le forum Interfaces Graphiques en Java
    Réponses: 4
    Dernier message: 14/05/2007, 13h50
  4. Pbs d'affichage d'une image sur un panel
    Par ysr1 dans le forum C++Builder
    Réponses: 2
    Dernier message: 23/09/2004, 09h55
  5. Problem avec les *.AVI sur les panels
    Par NaDiA_SoFt dans le forum C++Builder
    Réponses: 3
    Dernier message: 31/08/2003, 22h50

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