Background de toolstrip personalisés (heritage et gdi+)

Contenu du snippet

Ce ptit code sans prétention permet de styler la toolstrip comme celles de l'explorateur de Vista (un shti plus clair).

Source / Exemple :


using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;

namespace GradientsTools
{
    public class DoubleGradientToolBar : ToolStrip
    {
        public DoubleGradientToolBar()
        {
            this.RenderMode = ToolStripRenderMode.System;
            this.Padding = new Padding(2);
            this.AutoSize = false;
            this.Height = 35;
            this.GripStyle = ToolStripGripStyle.Hidden;
            this.ForeColor = Color.White;
        }

        
        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            Rectangle myRectangleTop = new Rectangle
                (
                this.ClientRectangle.X,
                this.ClientRectangle.Y,
                this.ClientRectangle.Width,
                this.ClientRectangle.Height / 2
                );
            Rectangle myRectangleBottom = new Rectangle
                    (
                    this.ClientRectangle.X,
                    this.ClientRectangle.Y + this.ClientRectangle.Height / 2,
                    this.ClientRectangle.Width,
                    this.ClientRectangle.Height / 2
                    );
            LinearGradientBrush myBrush = new LinearGradientBrush
                (myRectangleTop, bgColorBeginTop, bgColorEndTop, bgGradientOrientation);
            pevent.Graphics.FillRectangle(myBrush, myRectangleTop);

            myBrush = new LinearGradientBrush
                (myRectangleBottom, bgColorBeginBottom, bgColorEndBottom, bgGradientOrientation);
            pevent.Graphics.FillRectangle(myBrush, myRectangleBottom);
            myBrush.Dispose();
            pevent.Graphics.DrawRectangle(new Pen(bgColorBeginBottom, 1), myRectangleTop);
        }

        [Category("Apparence"), DefaultValue(typeof(Color), "0, 200, 200")
        , Description("Couleur de départ du gradient")]
        public Color Bg1ColorBeginTop
        {
            get { return this.bgColorBeginTop; }
            set { this.bgColorBeginTop = value; }
        }
        [Category("Apparence"), DefaultValue(typeof(Color), "0, 100, 100")
        , Description("Couleur de fin du gradient")]
        public Color Bg2ColorEndTop
        {
            get { return this.bgColorEndTop; }
            set { this.bgColorEndTop = value; }
        }

        [Category("Apparence"), DefaultValue(typeof(Color), "0, 60, 60")
        , Description("Couleur de départ du gradient")]
        public Color Bg3ColorBeginBottom
        {
            get { return this.bgColorBeginBottom; }
            set { this.bgColorBeginBottom = value; }
        }
        [Category("Apparence"), DefaultValue(typeof(Color), "0, 200, 200")
        , Description("Couleur de fin du gradient")]
        public Color Bg4ColorEndBottom
        {
            get { return this.bgColorEndBottom; }
            set { this.bgColorEndBottom = value; }
        }

        private Color bgColorBeginTop = Color.FromArgb(0, 200, 200);
        private Color bgColorEndTop = Color.FromArgb(0, 100, 100);
        private Color bgColorBeginBottom = Color.FromArgb(0, 60, 60);
        private Color bgColorEndBottom = Color.FromArgb(0, 200, 200);
        private LinearGradientMode bgGradientOrientation = LinearGradientMode.Vertical;

    }
}

Conclusion :


A noter que je ne l'ai pas essayé autre part que sur Vista, mais normalement, y a pas de pb.

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.