none
Utilisation des Skins pour Windows Forms RRS feed

  • Discussion générale

  • Bonjour

    Aujourd’hui je vous propose un petit exemple sur la création d’une forme ronde avec des boutons ronds.

    Il y a quelques problèmes sur ce type de formes :

    La première c’est que on doit cacher le frontière (propriété FormBorderStyle)  = None.

    En faisant ceci, les boutons Minimiser, Fermer, Maximiser sont aussi cachées.

    Aussi, on ne peut pas déplacer la fenêtre. Si pour les boutons Minimiser, Fermer, Maximiser on peut ajouter des boutons dans l’appli, pour gérer le déplacement de la fenêtre on doit écrire plus de code.

    Pour le debout on doit créer une image (J’ai utilisé Paint et j’ai créé une éclipse).  L’image c’est bien sur rectangulaire, mais ce qu’on veut cacher aura une couleur distincte.

    Dans les propriétés de la forme,  vous sélectez BackgroundImage\Importer\ et sélectez l’image crée.  

    Windows Forms Forme Ronde

    Aussi, pour BackgroundImageLayout selectez Center.

    Toujours dans les propriétés de la forme mettez TransparencyKey avec la couleur de l’image qu’on veut la cacher (Vert dans mon cas   - Hexa 00FF00).

    Pour déplacer la fenêtre avec un click souris n’ importe où dans la fenêtre, vous devez traiter les

    evènements MouseDown, MouseUp et MouseMove:

         public Boolean mouseMove = false;
            public Point mouseLocation; 
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    mouseMove = true;
                    mouseLocation = e.Location; 
                }
            }
    
            private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                if (mouseMove)
                {
                    // Get the difference between the two points
                    int xDiff = mouseLocation.X - e.Location.X;
                    int yDiff = mouseLocation.Y - e.Location.Y;
    
                    // Set the new point
                    int x = this.Location.X - xDiff;
                    int y = this.Location.Y - yDiff;
                    this.Location = new Point(x, y);
                }
            }
    
            private void Form1_MouseUp(object sender, MouseEventArgs e)
            {
                mouseMove = false;
            }

    et VB.NET

    Public mouseMove As [Boolean] = False
    Public mouseLocation As Point
    Public Sub New()
    	InitializeComponent()
    End Sub
    
    Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs)
    	If e.Button = System.Windows.Forms.MouseButtons.Left Then
    		mouseMove = True
    		mouseLocation = e.Location
    	End If
    End Sub
    
    Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs)
    	If mouseMove Then
    		' Get the difference between the two points
    		Dim xDiff As Integer = mouseLocation.X - e.Location.X
    		Dim yDiff As Integer = mouseLocation.Y - e.Location.Y
    
    		' Set the new point
    		Dim x As Integer = Me.Location.X - xDiff
    		Dim y As Integer = Me.Location.Y - yDiff
    		Me.Location = New Point(x, y)
    	End If
    End Sub
    
    Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs)
    	mouseMove = False
    End Sub

    à suivre


    Aurel BERA, Microsoft
    Microsoft propose ce service gratuitement, dans le but d'aider les utilisateurs et d'élargir les connaissances générales liées aux produits et technologies Microsoft. Ce contenu est fourni "tel quel" et il n'implique aucune responsabilité de la part de Microsoft.


    mardi 9 avril 2013 13:57

Toutes les réponses

  • Bouton Rond:

    On a créé un nouveau projet de type Class Library avec le code C# :

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Windows.Forms;
    using System.Drawing.Drawing2D;
    
    namespace AdvButton
    {
        /// <summary>
        /// Summary description for RoundButton.
        /// </summary>
        public class RoundButton : Button
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;
            private Pen _pen = null;
            SolidBrush _brushText = null, _brushInside = null;
            private byte _colorgradient = 2;		// fading effect
            private Point _textStartPoint = new Point(0, 0);
            private byte _colorStepGradient = 2;	// in pixels
            private bool _fadeOut = false;
            private bool _bDrawOutline = false;
            private Pen _dashedPen = null;
            private Pen _blackPen = null;
    
            // These are for drawing when you hover over the button
            private Color _hoverColor = Color.FromKnownColor(KnownColor.ControlDark);
            private Pen _hoverPen = null;
            private SolidBrush _hoverBrushInside = null;
    
            [
            Category("Button step-in color"),
            Description("This color will show up when you hover over the button")
            ]
            public Color HoverColor
            {
                get
                {
                    return _hoverColor;
                }
                set
                {
                    _hoverColor = value;
                    _hoverPen.Color = value;
                    _hoverBrushInside.Color = value;
                }
            }
    
            [
            Category("Text start point"),
            Description("Point where your text would start getting drawn")
            ]
            public Point TextStartPoint
            {
                get
                {
                    return _textStartPoint;
                }
                set
                {
                    _textStartPoint = value;
                }
            }
    
            [
            Category("Color gradient"),
            Description("Indicates how sharp a color transition you want")
            ]
            public byte ColorGradient
            {
                get
                {
                    return _colorgradient;
                }
                set
                {
                    _colorgradient = value;
                }
            }
    
            [
            Category("Color step gradient"),
            Description("Indicates how many every pixels you want color change")
            ]
            public byte ColorStepGradient
            {
                get
                {
                    return _colorStepGradient;
                }
                set
                {
                    _colorStepGradient = value;
                }
            }
    
            [
            Category("Fade"),
            Description("Fade out")
            ]
            public bool FadeOut
            {
                get
                {
                    return _fadeOut;
                }
                set
                {
                    _fadeOut = value;
                }
            }
    
            public RoundButton()
            {
                // This call is required by the Windows.Forms Form Designer.
                InitializeComponent();
    
                // TODO: Add any initialization after the InitComponent call
                _pen = new Pen(BackColor);
                _brushInside = new SolidBrush(BackColor);
                _brushText = new SolidBrush(ForeColor);
    
                _hoverPen = new Pen(_hoverColor);
                _hoverBrushInside = new SolidBrush(_hoverColor);
    
                _blackPen = new Pen(Color.FromKnownColor(KnownColor.Black), 2);
                _dashedPen = new Pen(Color.FromKnownColor(KnownColor.Black), 1);
                _dashedPen.DashStyle = DashStyle.Dot;
            }
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    if (components != null)
                        components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Component Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify 
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                // 
                // RoundButton
                // 
                this.ForeColorChanged += new System.EventHandler(this.RoundButton_ForeColorChanged);
                this.Enter += new System.EventHandler(this.RoundButton_Enter);
                this.MouseEnter += new System.EventHandler(this.RoundButton_MouseEnter);
                this.BackColorChanged += new System.EventHandler(this.RoundButton_BackColorChanged);
                this.Leave += new System.EventHandler(this.RoundButton_Leave);
                this.MouseLeave += new System.EventHandler(this.RoundButton_MouseLeave);
    
            }
            #endregion
    
            protected override void OnPaint(PaintEventArgs pe)
            {
                Graphics g = pe.Graphics;
                ColorButton(g);
    
                GraphicsPath path = new GraphicsPath();
                path.AddEllipse(0, 0, ClientSize.Width, ClientSize.Height);
                this.Region = new Region(path);
            }
    
            void ColorButton(Graphics g)
            {
                ColorButton5(g, _pen, _brushInside);
            }
    
            // Fills color. No color gradient.
            void ColorButton1(Graphics g)
            {
                g.DrawEllipse(_pen, 0, 0, ClientSize.Width, ClientSize.Height);
                g.FillEllipse(_brushInside, 0, 0, ClientSize.Width, ClientSize.Height);
            }
    
            // Fills color with color gradient. Color gets darker toward the righthand-bottom corner.
            void ColorButton2(Graphics g)
            {
                int x = 0, y = 0;
                Color origPenColor = _pen.Color;
                Color origBrushColor = _brushInside.Color;
    
                for (; x <= ClientSize.Width / 2 && y <= ClientSize.Height / 2; x += _colorStepGradient, y += _colorStepGradient)
                {
                    g.DrawEllipse(_pen, x, y, ClientSize.Width, ClientSize.Height);
                    g.FillEllipse(_brushInside, x, y, ClientSize.Width, ClientSize.Height);
    
                    byte newR = ((byte)(_pen.Color.R - _colorgradient) > 0 ? (byte)(_pen.Color.R - _colorgradient) : _pen.Color.R);
                    byte newG = ((byte)(_pen.Color.G - _colorgradient) > 0 ? (byte)(_pen.Color.G - _colorgradient) : _pen.Color.G);
                    byte newB = ((byte)(_pen.Color.B - _colorgradient) > 0 ? (byte)(_pen.Color.B - _colorgradient) : _pen.Color.B);
    
                    Color newcolor = Color.FromArgb(newR, newG, newB);
                    _pen.Color = newcolor;
                    _brushInside.Color = newcolor;
                }
    
                _pen.Color = origPenColor;
                _brushInside.Color = origBrushColor;
    
                DrawText(g);
            }
    
            // Fills color with color gradient. Color gets darker towards the center. Respects the image property set 
            // by the user.
            void ColorButton3(Graphics g)
            {
                int x = 0, y = 0;
                Color origPenColor = _pen.Color;
                Color origBrushColor = _brushInside.Color;
                int width = ClientSize.Width, height = ClientSize.Height;
    
                for (; x <= width / 2 && y <= height / 2; x += _colorStepGradient, y += _colorStepGradient, width -= 2 * _colorStepGradient, height -= 2 * _colorStepGradient)
                {
                    g.DrawEllipse(_pen, x, y, width, height);
                    g.FillEllipse(_brushInside, x, y, width, height);
    
                    byte newR = ((byte)(_pen.Color.R - _colorgradient) > 0 ? (byte)(_pen.Color.R - _colorgradient) : _pen.Color.R);
                    byte newG = ((byte)(_pen.Color.G - _colorgradient) > 0 ? (byte)(_pen.Color.G - _colorgradient) : _pen.Color.G);
                    byte newB = ((byte)(_pen.Color.B - _colorgradient) > 0 ? (byte)(_pen.Color.B - _colorgradient) : _pen.Color.B);
    
                    Color newcolor = Color.FromArgb(newR, newG, newB);
                    _pen.Color = newcolor;
                    _brushInside.Color = newcolor;
                }
    
                _pen.Color = origPenColor;
                _brushInside.Color = origBrushColor;
    
                DrawText(g);
                DrawImage(g);
            }
    
            // Implements fade out/ in property. Color gets lighter (fade in) or darker (fade out) towards the center.
            void ColorButton4(Graphics g)
            {
                int x = 0, y = 0;
                Color origPenColor = _pen.Color;
                Color origBrushColor = _brushInside.Color;
                int width = ClientSize.Width, height = ClientSize.Height;
    
                for (; x <= width / 2 && y <= height / 2; x += _colorStepGradient, y += _colorStepGradient, width -= 2 * _colorStepGradient, height -= 2 * _colorStepGradient)
                {
                    g.DrawEllipse(_pen, x, y, width, height);
                    g.FillEllipse(_brushInside, x, y, width, height);
    
                    byte newR = _pen.Color.R;
                    byte newG = _pen.Color.G;
                    byte newB = _pen.Color.B;
    
                    if (_fadeOut)
                    {
                        if (_pen.Color.R + _colorgradient <= 255)
                            newR = (byte)(_pen.Color.R + _colorgradient);
    
                        if (_pen.Color.G + _colorgradient <= 255)
                            newG = (byte)(_pen.Color.G + _colorgradient);
    
                        if (_pen.Color.B + _colorgradient <= 255)
                            newB = (byte)(_pen.Color.B + _colorgradient);
                    }
                    else
                    {
                        if (_pen.Color.R - _colorgradient >= 0)
                            newR = (byte)(_pen.Color.R - _colorgradient);
    
                        if (_pen.Color.G - _colorgradient >= 0)
                            newG = (byte)(_pen.Color.G - _colorgradient);
    
                        if (_pen.Color.B - _colorgradient >= 0)
                            newB = (byte)(_pen.Color.B - _colorgradient);
                    }
    
                    Color newcolor = Color.FromArgb(newR, newG, newB);
                    _pen.Color = newcolor;
                    _brushInside.Color = newcolor;
                }
    
                _pen.Color = origPenColor;
                _brushInside.Color = origBrushColor;
    
                DrawText(g);
                DrawImage(g);
            }
    
            // ColorButton4 modified to take in pen and brush arguments. Needed for hover-coloring.
            // Draws a focus rectangle when the button has focus.
            void ColorButton5(Graphics g, Pen pen, SolidBrush brush)
            {
                int x = 0, y = 0;
                Color origPenColor = pen.Color;
                Color origBrushColor = brush.Color;
                int width = ClientSize.Width, height = ClientSize.Height;
    
                for (; x <= width / 2 && y <= height / 2; x += _colorStepGradient, y += _colorStepGradient, width -= 2 * _colorStepGradient, height -= 2 * _colorStepGradient)
                {
                    // Draw the focus ellipse
                    if (_bDrawOutline && (x == 0))
                    {
                        // Draw solid black outline
                        g.DrawEllipse(_blackPen, x, y, width, height);
                        x++; y++; width -= 2; height -= 2;
                        g.FillEllipse(brush, x, y, width, height);
    
                        g.DrawEllipse(_blackPen, x, y, width, height);
                        x++; y++; width -= 2; height -= 2;
                        g.FillEllipse(brush, x, y, width, height);
                    }
                    else
                        g.DrawEllipse(pen, x, y, width, height);
    
                    if (_bDrawOutline && (x == 6))
                    {
                        // Draw dashed black (inner ellipse of focus ellipse)
                        g.DrawEllipse(_dashedPen, x, y, width, height);
                        x += 1; y += 1; width -= 2; height -= 2;
                    }
    
                    g.FillEllipse(brush, x, y, width, height);
    
                    byte newR = pen.Color.R;
                    byte newG = pen.Color.G;
                    byte newB = pen.Color.B;
    
                    if (_fadeOut)
                    // outer rim -> darker color
                    {
                        if (pen.Color.R + _colorgradient <= 255)
                            newR = (byte)(pen.Color.R + _colorgradient);
    
                        if (pen.Color.G + _colorgradient <= 255)
                            newG = (byte)(pen.Color.G + _colorgradient);
    
                        if (pen.Color.B + _colorgradient <= 255)
                            newB = (byte)(pen.Color.B + _colorgradient);
                    }
                    else
                    // outer rim -> lighter color
                    {
                        if (pen.Color.R - _colorgradient >= 0)
                            newR = (byte)(pen.Color.R - _colorgradient);
    
                        if (pen.Color.G - _colorgradient >= 0)
                            newG = (byte)(pen.Color.G - _colorgradient);
    
                        if (pen.Color.B - _colorgradient >= 0)
                            newB = (byte)(pen.Color.B - _colorgradient);
                    }
    
                    Color newcolor = Color.FromArgb(newR, newG, newB);
                    pen.Color = newcolor;
                    brush.Color = newcolor;
                }
    
                // restore the original color
                pen.Color = origPenColor;
                brush.Color = origBrushColor;
    
                DrawText(g);
                DrawImage(g);
            }
    
            private void DrawText(Graphics g)
            {
    
                SizeF size = g.MeasureString(this.Text, this.Font);
    
                g.DrawString(this.Text, this.Font, _brushText, new PointF(_textStartPoint.X + this.Width / 2 - size.Width / 2, _textStartPoint.Y + this.Height / 2 - size.Height / 2));
            }
    
            private void DrawImage(Graphics g)
            {
                // depends on ImageAlign
                if (Image != null)
                {
                    Rectangle rc = new Rectangle(new Point((this.Width - Image.Width) / 2, (this.Height - Image.Height) / 2), new Size(Image.Width, Image.Height));
                    g.DrawImage(this.Image, rc);
                }
    
            }
    
            private void RoundButton_MouseEnter(object sender, System.EventArgs e)
            {
                _pen.Color = _hoverColor;
                _brushInside.Color = _hoverColor;
                this.Invalidate();
            }
    
            private void RoundButton_MouseLeave(object sender, System.EventArgs e)
            {
                _pen.Color = BackColor;
                _brushInside.Color = BackColor;
                this.Invalidate();
            }
    
            private void RoundButton_Enter(object sender, System.EventArgs e)
            {
                _bDrawOutline = true;
            }
    
            private void RoundButton_Leave(object sender, System.EventArgs e)
            {
                _bDrawOutline = false;
            }
    
            private void RoundButton_BackColorChanged(object sender, System.EventArgs e)
            {
                _pen.Color = BackColor;
                _brushInside.Color = BackColor;
            }
    
            private void RoundButton_ForeColorChanged(object sender, System.EventArgs e)
            {
                _brushText.Color = ForeColor;
            }
        }
    }

    et VB.NET:

    Imports System.Collections
    Imports System.ComponentModel
    Imports System.Drawing
    Imports System.Data
    Imports System.Windows.Forms
    Imports System.Drawing.Drawing2D
    
    Namespace AdvButton
    	''' <summary>
    	''' Summary description for RoundButton.
    	''' </summary>
    	Public Class RoundButton
    		Inherits Button
    		''' <summary>
    		''' Required designer variable.
    		''' </summary>
    		Private components As System.ComponentModel.Container = Nothing
    		Private _pen As Pen = Nothing
    		Private _brushText As SolidBrush = Nothing, _brushInside As SolidBrush = Nothing
    		Private _colorgradient As Byte = 2
    		' fading effect
    		Private _textStartPoint As New Point(0, 0)
    		Private _colorStepGradient As Byte = 2
    		' in pixels
    		Private _fadeOut As Boolean = False
    		Private _bDrawOutline As Boolean = False
    		Private _dashedPen As Pen = Nothing
    		Private _blackPen As Pen = Nothing
    
    		' These are for drawing when you hover over the button
    		Private _hoverColor As Color = Color.FromKnownColor(KnownColor.ControlDark)
    		Private _hoverPen As Pen = Nothing
    		Private _hoverBrushInside As SolidBrush = Nothing
    
    		<Category("Button step-in color"), Description("This color will show up when you hover over the button")> _
    		Public Property HoverColor() As Color
    			Get
    				Return _hoverColor
    			End Get
    			Set
    				_hoverColor = value
    				_hoverPen.Color = value
    				_hoverBrushInside.Color = value
    			End Set
    		End Property
    
    		<Category("Text start point"), Description("Point where your text would start getting drawn")> _
    		Public Property TextStartPoint() As Point
    			Get
    				Return _textStartPoint
    			End Get
    			Set
    				_textStartPoint = value
    			End Set
    		End Property
    
    		<Category("Color gradient"), Description("Indicates how sharp a color transition you want")> _
    		Public Property ColorGradient() As Byte
    			Get
    				Return _colorgradient
    			End Get
    			Set
    				_colorgradient = value
    			End Set
    		End Property
    
    		<Category("Color step gradient"), Description("Indicates how many every pixels you want color change")> _
    		Public Property ColorStepGradient() As Byte
    			Get
    				Return _colorStepGradient
    			End Get
    			Set
    				_colorStepGradient = value
    			End Set
    		End Property
    
    		<Category("Fade"), Description("Fade out")> _
    		Public Property FadeOut() As Boolean
    			Get
    				Return _fadeOut
    			End Get
    			Set
    				_fadeOut = value
    			End Set
    		End Property
    
    		Public Sub New()
    			' This call is required by the Windows.Forms Form Designer.
    			InitializeComponent()
    
    			' TODO: Add any initialization after the InitComponent call
    			_pen = New Pen(BackColor)
    			_brushInside = New SolidBrush(BackColor)
    			_brushText = New SolidBrush(ForeColor)
    
    			_hoverPen = New Pen(_hoverColor)
    			_hoverBrushInside = New SolidBrush(_hoverColor)
    
    			_blackPen = New Pen(Color.FromKnownColor(KnownColor.Black), 2)
    			_dashedPen = New Pen(Color.FromKnownColor(KnownColor.Black), 1)
    			_dashedPen.DashStyle = DashStyle.Dot
    		End Sub
    
    		''' <summary>
    		''' Clean up any resources being used.
    		''' </summary>
    		Protected Overrides Sub Dispose(disposing As Boolean)
    			If disposing Then
    				If components IsNot Nothing Then
    					components.Dispose()
    				End If
    			End If
    			MyBase.Dispose(disposing)
    		End Sub
    
    		#Region "Component Designer generated code"
    		''' <summary>
    		''' Required method for Designer support - do not modify 
    		''' the contents of this method with the code editor.
    		''' </summary>
    		Private Sub InitializeComponent()
    			' 
    			' RoundButton
    			' 
    			AddHandler Me.ForeColorChanged, New System.EventHandler(AddressOf Me.RoundButton_ForeColorChanged)
    			AddHandler Me.Enter, New System.EventHandler(AddressOf Me.RoundButton_Enter)
    			AddHandler Me.MouseEnter, New System.EventHandler(AddressOf Me.RoundButton_MouseEnter)
    			AddHandler Me.BackColorChanged, New System.EventHandler(AddressOf Me.RoundButton_BackColorChanged)
    			AddHandler Me.Leave, New System.EventHandler(AddressOf Me.RoundButton_Leave)
    			AddHandler Me.MouseLeave, New System.EventHandler(AddressOf Me.RoundButton_MouseLeave)
    
    		End Sub
    		#End Region
    
    		Protected Overrides Sub OnPaint(pe As PaintEventArgs)
    			Dim g As Graphics = pe.Graphics
    			ColorButton(g)
    
    			Dim path As New GraphicsPath()
    			path.AddEllipse(0, 0, ClientSize.Width, ClientSize.Height)
    			Me.Region = New Region(path)
    		End Sub
    
    		Private Sub ColorButton(g As Graphics)
    			ColorButton5(g, _pen, _brushInside)
    		End Sub
    
    		' Fills color. No color gradient.
    		Private Sub ColorButton1(g As Graphics)
    			g.DrawEllipse(_pen, 0, 0, ClientSize.Width, ClientSize.Height)
    			g.FillEllipse(_brushInside, 0, 0, ClientSize.Width, ClientSize.Height)
    		End Sub
    
    		' Fills color with color gradient. Color gets darker toward the righthand-bottom corner.
    		Private Sub ColorButton2(g As Graphics)
    			Dim x As Integer = 0, y As Integer = 0
    			Dim origPenColor As Color = _pen.Color
    			Dim origBrushColor As Color = _brushInside.Color
    
    			While x <= ClientSize.Width \ 2 AndAlso y <= ClientSize.Height \ 2
    				g.DrawEllipse(_pen, x, y, ClientSize.Width, ClientSize.Height)
    				g.FillEllipse(_brushInside, x, y, ClientSize.Width, ClientSize.Height)
    
    				Dim newR As Byte = (If(CByte(_pen.Color.R - _colorgradient) > 0, CByte(_pen.Color.R - _colorgradient), _pen.Color.R))
    				Dim newG As Byte = (If(CByte(_pen.Color.G - _colorgradient) > 0, CByte(_pen.Color.G - _colorgradient), _pen.Color.G))
    				Dim newB As Byte = (If(CByte(_pen.Color.B - _colorgradient) > 0, CByte(_pen.Color.B - _colorgradient), _pen.Color.B))
    
    				Dim newcolor As Color = Color.FromArgb(newR, newG, newB)
    				_pen.Color = newcolor
    				_brushInside.Color = newcolor
    				x += _colorStepGradient
    				y += _colorStepGradient
    			End While
    
    			_pen.Color = origPenColor
    			_brushInside.Color = origBrushColor
    
    			DrawText(g)
    		End Sub
    
    		' Fills color with color gradient. Color gets darker towards the center. Respects the image property set 
    		' by the user.
    		Private Sub ColorButton3(g As Graphics)
    			Dim x As Integer = 0, y As Integer = 0
    			Dim origPenColor As Color = _pen.Color
    			Dim origBrushColor As Color = _brushInside.Color
    			Dim width As Integer = ClientSize.Width, height As Integer = ClientSize.Height
    
    			While x <= width \ 2 AndAlso y <= height \ 2
    				g.DrawEllipse(_pen, x, y, width, height)
    				g.FillEllipse(_brushInside, x, y, width, height)
    
    				Dim newR As Byte = (If(CByte(_pen.Color.R - _colorgradient) > 0, CByte(_pen.Color.R - _colorgradient), _pen.Color.R))
    				Dim newG As Byte = (If(CByte(_pen.Color.G - _colorgradient) > 0, CByte(_pen.Color.G - _colorgradient), _pen.Color.G))
    				Dim newB As Byte = (If(CByte(_pen.Color.B - _colorgradient) > 0, CByte(_pen.Color.B - _colorgradient), _pen.Color.B))
    
    				Dim newcolor As Color = Color.FromArgb(newR, newG, newB)
    				_pen.Color = newcolor
    				_brushInside.Color = newcolor
    				x += _colorStepGradient
    				y += _colorStepGradient
    				width -= 2 * _colorStepGradient
    				height -= 2 * _colorStepGradient
    			End While
    
    			_pen.Color = origPenColor
    			_brushInside.Color = origBrushColor
    
    			DrawText(g)
    			DrawImage(g)
    		End Sub
    
    		' Implements fade out/ in property. Color gets lighter (fade in) or darker (fade out) towards the center.
    		Private Sub ColorButton4(g As Graphics)
    			Dim x As Integer = 0, y As Integer = 0
    			Dim origPenColor As Color = _pen.Color
    			Dim origBrushColor As Color = _brushInside.Color
    			Dim width As Integer = ClientSize.Width, height As Integer = ClientSize.Height
    
    			While x <= width \ 2 AndAlso y <= height \ 2
    				g.DrawEllipse(_pen, x, y, width, height)
    				g.FillEllipse(_brushInside, x, y, width, height)
    
    				Dim newR As Byte = _pen.Color.R
    				Dim newG As Byte = _pen.Color.G
    				Dim newB As Byte = _pen.Color.B
    
    				If _fadeOut Then
    					If _pen.Color.R + _colorgradient <= 255 Then
    						newR = CByte(_pen.Color.R + _colorgradient)
    					End If
    
    					If _pen.Color.G + _colorgradient <= 255 Then
    						newG = CByte(_pen.Color.G + _colorgradient)
    					End If
    
    					If _pen.Color.B + _colorgradient <= 255 Then
    						newB = CByte(_pen.Color.B + _colorgradient)
    					End If
    				Else
    					If _pen.Color.R - _colorgradient >= 0 Then
    						newR = CByte(_pen.Color.R - _colorgradient)
    					End If
    
    					If _pen.Color.G - _colorgradient >= 0 Then
    						newG = CByte(_pen.Color.G - _colorgradient)
    					End If
    
    					If _pen.Color.B - _colorgradient >= 0 Then
    						newB = CByte(_pen.Color.B - _colorgradient)
    					End If
    				End If
    
    				Dim newcolor As Color = Color.FromArgb(newR, newG, newB)
    				_pen.Color = newcolor
    				_brushInside.Color = newcolor
    				x += _colorStepGradient
    				y += _colorStepGradient
    				width -= 2 * _colorStepGradient
    				height -= 2 * _colorStepGradient
    			End While
    
    			_pen.Color = origPenColor
    			_brushInside.Color = origBrushColor
    
    			DrawText(g)
    			DrawImage(g)
    		End Sub
    
    		' ColorButton4 modified to take in pen and brush arguments. Needed for hover-coloring.
    		' Draws a focus rectangle when the button has focus.
    		Private Sub ColorButton5(g As Graphics, pen As Pen, brush As SolidBrush)
    			Dim x As Integer = 0, y As Integer = 0
    			Dim origPenColor As Color = pen.Color
    			Dim origBrushColor As Color = brush.Color
    			Dim width As Integer = ClientSize.Width, height As Integer = ClientSize.Height
    
    			While x <= width \ 2 AndAlso y <= height \ 2
    				' Draw the focus ellipse
    				If _bDrawOutline AndAlso (x = 0) Then
    					' Draw solid black outline
    					g.DrawEllipse(_blackPen, x, y, width, height)
    					x += 1
    					y += 1
    					width -= 2
    					height -= 2
    					g.FillEllipse(brush, x, y, width, height)
    
    					g.DrawEllipse(_blackPen, x, y, width, height)
    					x += 1
    					y += 1
    					width -= 2
    					height -= 2
    					g.FillEllipse(brush, x, y, width, height)
    				Else
    					g.DrawEllipse(pen, x, y, width, height)
    				End If
    
    				If _bDrawOutline AndAlso (x = 6) Then
    					' Draw dashed black (inner ellipse of focus ellipse)
    					g.DrawEllipse(_dashedPen, x, y, width, height)
    					x += 1
    					y += 1
    					width -= 2
    					height -= 2
    				End If
    
    				g.FillEllipse(brush, x, y, width, height)
    
    				Dim newR As Byte = pen.Color.R
    				Dim newG As Byte = pen.Color.G
    				Dim newB As Byte = pen.Color.B
    
    				If _fadeOut Then
    					' outer rim -> darker color
    					If pen.Color.R + _colorgradient <= 255 Then
    						newR = CByte(pen.Color.R + _colorgradient)
    					End If
    
    					If pen.Color.G + _colorgradient <= 255 Then
    						newG = CByte(pen.Color.G + _colorgradient)
    					End If
    
    					If pen.Color.B + _colorgradient <= 255 Then
    						newB = CByte(pen.Color.B + _colorgradient)
    					End If
    				Else
    					' outer rim -> lighter color
    					If pen.Color.R - _colorgradient >= 0 Then
    						newR = CByte(pen.Color.R - _colorgradient)
    					End If
    
    					If pen.Color.G - _colorgradient >= 0 Then
    						newG = CByte(pen.Color.G - _colorgradient)
    					End If
    
    					If pen.Color.B - _colorgradient >= 0 Then
    						newB = CByte(pen.Color.B - _colorgradient)
    					End If
    				End If
    
    				Dim newcolor As Color = Color.FromArgb(newR, newG, newB)
    				pen.Color = newcolor
    				brush.Color = newcolor
    				x += _colorStepGradient
    				y += _colorStepGradient
    				width -= 2 * _colorStepGradient
    				height -= 2 * _colorStepGradient
    			End While
    
    			' restore the original color
    			pen.Color = origPenColor
    			brush.Color = origBrushColor
    
    			DrawText(g)
    			DrawImage(g)
    		End Sub
    
    		Private Sub DrawText(g As Graphics)
    
    			Dim size As SizeF = g.MeasureString(Me.Text, Me.Font)
    
    			g.DrawString(Me.Text, Me.Font, _brushText, New PointF(_textStartPoint.X + Me.Width \ 2 - size.Width / 2, _textStartPoint.Y + Me.Height \ 2 - size.Height / 2))
    		End Sub
    
    		Private Sub DrawImage(g As Graphics)
    			' depends on ImageAlign
    			If Image IsNot Nothing Then
    				Dim rc As New Rectangle(New Point((Me.Width - Image.Width) \ 2, (Me.Height - Image.Height) \ 2), New Size(Image.Width, Image.Height))
    				g.DrawImage(Me.Image, rc)
    			End If
    
    		End Sub
    
    		Private Sub RoundButton_MouseEnter(sender As Object, e As System.EventArgs)
    			_pen.Color = _hoverColor
    			_brushInside.Color = _hoverColor
    			Me.Invalidate()
    		End Sub
    
    		Private Sub RoundButton_MouseLeave(sender As Object, e As System.EventArgs)
    			_pen.Color = BackColor
    			_brushInside.Color = BackColor
    			Me.Invalidate()
    		End Sub
    
    		Private Sub RoundButton_Enter(sender As Object, e As System.EventArgs)
    			_bDrawOutline = True
    		End Sub
    
    		Private Sub RoundButton_Leave(sender As Object, e As System.EventArgs)
    			_bDrawOutline = False
    		End Sub
    
    		Private Sub RoundButton_BackColorChanged(sender As Object, e As System.EventArgs)
    			_pen.Color = BackColor
    			_brushInside.Color = BackColor
    		End Sub
    
    		Private Sub RoundButton_ForeColorChanged(sender As Object, e As System.EventArgs)
    			_brushText.Color = ForeColor
    		End Sub
    	End Class
    End Namespace

    J'ai modifiee le DrawText a partir d'ici:

    http://www.codeproject.com/Articles/5082/Round-Button-in-C

    Cordialement,


    Aurel BERA, Microsoft
    Microsoft propose ce service gratuitement, dans le but d'aider les utilisateurs et d'élargir les connaissances générales liées aux produits et technologies Microsoft. Ce contenu est fourni "tel quel" et il n'implique aucune responsabilité de la part de Microsoft.


    mardi 9 avril 2013 14:06