1. #1

    Registriert seit
    22.05.2012
    Beiträge
    202
    Thanked 21 Times in 9 Posts

    Standard Colorslider (Trackbar modifiziert)

    Hey,

    habe dieses Control vor ca. einem Jahr in Google gefunden. Keine Ahnung von wem das ist.

    Name:  unbenannt8zjdf.png
Hits: 169
Größe:  13,6 KB

    Spoiler:Colorslider.cs

    PHP-Code:
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.ComponentModel;
    using System.Drawing.Drawing2D;

    namespace 
    MB.Controls
    {
        
    /// <summary>
        /// Encapsulates control that visualy displays certain integer value and allows user to change it within desired range. It imitates <see cref="System.Windows.Forms.TrackBar"/> as far as mouse usage is concerned.
        /// </summary>
        
    [ToolboxBitmap(typeof(TrackBar))]
        [
    DefaultEvent("Scroll"), DefaultProperty("BarInnerColor")]
        public 
    partial class ColorSlider Control
        
    {
            
    #region Events

            /// <summary>
            /// Fires when Slider position has changed
            /// </summary>
            
    [Description("Event fires when the Value property changes")]
            [
    Category("Action")]
            public 
    event EventHandler ValueChanged;

            
    /// <summary>
            /// Fires when user scrolls the Slider
            /// </summary>
            
    [Description("Event fires when the Slider position is changed")]
            [
    Category("Behavior")]
            public 
    event ScrollEventHandler Scroll;

            
    #endregion

            #region Properties

            
    private Rectangle thumbRect//bounding rectangle of thumb area
            /// <summary>
            /// Gets the thumb rect. Usefull to determine bounding rectangle when creating custom thumb shape.
            /// </summary>
            /// <value>The thumb rect.</value>
            
    [Browsable(false)]
            public 
    Rectangle ThumbRect
            
    {
                
    get { return thumbRect; }
            }

            private 
    Rectangle barRect//bounding rectangle of bar area
            
    private Rectangle barHalfRect;
            private 
    Rectangle thumbHalfRect;
            private 
    Rectangle elapsedRect//bounding rectangle of elapsed area

            
    private int thumbSize 15;
            
    /// <summary>
            /// Gets or sets the size of the thumb.
            /// </summary>
            /// <value>The size of the thumb.</value>
            /// <exception cref="T:System.ArgumentOutOfRangeException">exception thrown when value is lower than zero or grather than half of appropiate dimension</exception>
            
    [Description("Set Slider thumb size")]
            [
    Category("ColorSlider")]
            [
    DefaultValue(15)]
            public 
    int ThumbSize
            
    {
                
    get { return thumbSize; }
                
    set
                
    {
                    if (
    value &
                        
    value < (barOrientation == Orientation.Horizontal ClientRectangle.Width ClientRectangle.Height))
                        
    thumbSize value;
                    else
                        throw new 
    ArgumentOutOfRangeException(
                            
    "TrackSize has to be greather than zero and lower than half of Slider width");
                    
    Invalidate();
                }
            }

            private 
    GraphicsPath thumbCustomShape null;
            
    /// <summary>
            /// Gets or sets the thumb custom shape. Use ThumbRect property to determine bounding rectangle.
            /// </summary>
            /// <value>The thumb custom shape. null means default shape</value>
            
    [Description("Set Slider's thumb's custom shape")]
            [
    Category("ColorSlider")]
            [
    Browsable(false)]
            [
    DefaultValue(typeof(GraphicsPath), "null")]
            public 
    GraphicsPath ThumbCustomShape
            
    {
                
    get { return thumbCustomShape; }
                
    set
                
    {
                    
    thumbCustomShape value;
                    
    thumbSize = (int) (barOrientation == Orientation.Horizontal value.GetBounds().Width value.GetBounds().Height) + 1;
                    
    Invalidate();
                }
            }

            private 
    Size thumbRoundRectSize = new Size(88);
            
    /// <summary>
            /// Gets or sets the size of the thumb round rectangle edges.
            /// </summary>
            /// <value>The size of the thumb round rectangle edges.</value>
            
    [Description("Set Slider's thumb round rect size")]
            [
    Category("ColorSlider")]
            [
    DefaultValue(typeof(Size), "8; 8")]
            public 
    Size ThumbRoundRectSize
            
    {
                
    get { return thumbRoundRectSize; }
                
    set
                
    {
                    
    int h value.Heightvalue.Width;
                    if (
    <= 01;
                    if (
    <= 01;
                    
    thumbRoundRectSize = new Size(wh);
                    
    Invalidate();
                }
            }

            private 
    Size borderRoundRectSize = new Size(88);
            
    /// <summary>
            /// Gets or sets the size of the border round rect.
            /// </summary>
            /// <value>The size of the border round rect.</value>
            
    [Description("Set Slider's border round rect size")]
            [
    Category("ColorSlider")]
            [
    DefaultValue(typeof(Size), "8; 8")]
            public 
    Size BorderRoundRectSize
            
    {
                
    get { return borderRoundRectSize; }
                
    set
                
    {
                    
    int h value.Heightvalue.Width;
                    if (
    <= 01;
                    if (
    <= 01;
                    
    borderRoundRectSize = new Size(wh);
                    
    Invalidate();
                }
            }

            private 
    Orientation barOrientation Orientation.Horizontal;
            
    /// <summary>
            /// Gets or sets the orientation of Slider.
            /// </summary>
            /// <value>The orientation.</value>
            
    [Description("Set Slider orientation")]
            [
    Category("ColorSlider")]
            [
    DefaultValue(Orientation.Horizontal)]
            public 
    Orientation Orientation
            
    {
                
    get { return barOrientation; }
                
    set
                
    {
                    if (
    barOrientation != value)
                    {
                        
    barOrientation value;
                        
    int temp Width;
                        
    Width Height;
                        
    Height temp;
                        if (
    thumbCustomShape != null)
                            
    thumbSize =
                                (int)
                                (
    barOrientation == Orientation.Horizontal
                                     
    thumbCustomShape.GetBounds().Width
                                     
    thumbCustomShape.GetBounds().Height) + 1;
                        
    Invalidate();
                    }
                }
            }


            private 
    int trackerValue 50;
            
    /// <summary>
            /// Gets or sets the value of Slider.
            /// </summary>
            /// <value>The value.</value>
            /// <exception cref="T:System.ArgumentOutOfRangeException">exception thrown when value is outside appropriate range (min, max)</exception>
            
    [Description("Set Slider value")]
            [
    Category("ColorSlider")]
            [
    DefaultValue(50)]
            public 
    int Value
            
    {
                
    get { return trackerValue; }
                
    set
                
    {
                    if (
    value >= barMinimum value <= barMaximum)
                    {
                        
    trackerValue value;
                        if (
    ValueChanged != nullValueChanged(this, new EventArgs());
                        
    Invalidate();
                    }
                    else throw new 
    ArgumentOutOfRangeException("Value is outside appropriate range (min, max)");
                }
            }


            private 
    int barMinimum 0;
            
    /// <summary>
            /// Gets or sets the minimum value.
            /// </summary>
            /// <value>The minimum value.</value>
            /// <exception cref="T:System.ArgumentOutOfRangeException">exception thrown when minimal value is greather than maximal one</exception>
            
    [Description("Set Slider minimal point")]
            [
    Category("ColorSlider")]
            [
    DefaultValue(0)]
            public 
    int Minimum
            
    {
                
    get { return barMinimum; }
                
    set
                
    {
                    if (
    value barMaximum)
                    {
                        
    barMinimum value;
                        if (
    trackerValue barMinimum)
                        {
                            
    trackerValue barMinimum;
                            if (
    ValueChanged != nullValueChanged(this, new EventArgs());
                        }
                        
    Invalidate();
                    }
                    else throw new 
    ArgumentOutOfRangeException("Minimal value is greather than maximal one");
                }
            }


            private 
    int barMaximum 100;
            
    /// <summary>
            /// Gets or sets the maximum value.
            /// </summary>
            /// <value>The maximum value.</value>
            /// <exception cref="T:System.ArgumentOutOfRangeException">exception thrown when maximal value is lower than minimal one</exception>
            
    [Description("Set Slider maximal point")]
            [
    Category("ColorSlider")]
            [
    DefaultValue(100)]
            public 
    int Maximum
            
    {
                
    get { return barMaximum; }
                
    set
                
    {
                    if (
    value barMinimum)
                    {
                        
    barMaximum value;
                        if (
    trackerValue barMaximum)
                        {
                            
    trackerValue barMaximum;
                            if (
    ValueChanged != nullValueChanged(this, new EventArgs());
                        }
                        
    Invalidate();
                    }
                    else throw new 
    ArgumentOutOfRangeException("Maximal value is lower than minimal one");
                }
            }

            private 
    uint smallChange 1;
            
    /// <summary>
            /// Gets or sets trackbar's small change. It affects how to behave when directional keys are pressed
            /// </summary>
            /// <value>The small change value.</value>
            
    [Description("Set trackbar's small change")]
            [
    Category("ColorSlider")]
            [
    DefaultValue(1)]
            public 
    uint SmallChange
            
    {
                
    get { return smallChange; }
                
    set smallChange value; }
            }

            private 
    uint largeChange 5;

            
    /// <summary>
            /// Gets or sets trackbar's large change. It affects how to behave when PageUp/PageDown keys are pressed
            /// </summary>
            /// <value>The large change value.</value>
            
    [Description("Set trackbar's large change")]
            [
    Category("ColorSlider")]
            [
    DefaultValue(5)]
            public 
    uint LargeChange
            
    {
                
    get { return largeChange; }
                
    set largeChange value; }
            }

            private 
    bool drawFocusRectangle true;
            
    /// <summary>
            /// Gets or sets a value indicating whether to draw focus rectangle.
            /// </summary>
            /// <value><c>true</c> if focus rectangle should be drawn; otherwise, <c>false</c>.</value>
            
    [Description("Set whether to draw focus rectangle")]
            [
    Category("ColorSlider")]
            [
    DefaultValue(true)]
            public 
    bool DrawFocusRectangle
            
    {
                
    get { return drawFocusRectangle; }
                
    set
                
    {
                    
    drawFocusRectangle value;
                    
    Invalidate();
                }
            }

            private 
    bool drawSemitransparentThumb true;
            
    /// <summary>
            /// Gets or sets a value indicating whether to draw semitransparent thumb.
            /// </summary>
            /// <value><c>true</c> if semitransparent thumb should be drawn; otherwise, <c>false</c>.</value>
            
    [Description("Set whether to draw semitransparent thumb")]
            [
    Category("ColorSlider")]
            [
    DefaultValue(true)]
            public 
    bool DrawSemitransparentThumb
            
    {
                
    get { return drawSemitransparentThumb; }
                
    set
                
    {
                    
    drawSemitransparentThumb value;
                    
    Invalidate();
                }
            }

            private 
    bool mouseEffects true;
            
    /// <summary>
            /// Gets or sets whether mouse entry and exit actions have impact on how control look.
            /// </summary>
            /// <value><c>true</c> if mouse entry and exit actions have impact on how control look; otherwise, <c>false</c>.</value>
            
    [Description("Set whether mouse entry and exit actions have impact on how control look")]
            [
    Category("ColorSlider")]
            [
    DefaultValue(true)]
            public 
    bool MouseEffects
            
    {
                
    get { return mouseEffects; }
                
    set
                
    {
                    
    mouseEffects value;
                    
    Invalidate();
                }
            }

            private 
    int mouseWheelBarPartitions 10;
            
    /// <summary>
            /// Gets or sets the mouse wheel bar partitions.
            /// </summary>
            /// <value>The mouse wheel bar partitions.</value>
            /// <exception cref="T:System.ArgumentOutOfRangeException">exception thrown when value isn't greather than zero</exception>
            
    [Description("Set to how many parts is bar divided when using mouse wheel")]
            [
    Category("ColorSlider")]
            [
    DefaultValue(10)]
            public 
    int MouseWheelBarPartitions
            
    {
                
    get { return mouseWheelBarPartitions; }
                
    set
                
    {
                    if (
    value 0)
                        
    mouseWheelBarPartitions value;
                    else throw new 
    ArgumentOutOfRangeException("MouseWheelBarPartitions has to be greather than zero");
                }
            }
            
            private 
    Color thumbOuterColor Color.White;
            
    /// <summary>
            /// Gets or sets the thumb outer color .
            /// </summary>
            /// <value>The thumb outer color.</value>
            
    [Description("Set Slider thumb outer color")]
            [
    Category("ColorSlider")]
            [
    DefaultValue(typeof(Color), "White")]
            public 
    Color ThumbOuterColor
            
    {
                
    get { return thumbOuterColor; }
                
    set
                
    {
                    
    thumbOuterColor value;
                    
    Invalidate();
                }
            }


            private 
    Color thumbInnerColor Color.Gainsboro;
            
    /// <summary>
            /// Gets or sets the inner color of the thumb.
            /// </summary>
            /// <value>The inner color of the thumb.</value>
            
    [Description("Set Slider thumb inner color")]
            [
    Category("ColorSlider")]
            [
    DefaultValue(typeof(Color), "Gainsboro")]
            public 
    Color ThumbInnerColor
            
    {
                
    get { return thumbInnerColor; }
                
    set
                
    {
                    
    thumbInnerColor value;
                    
    Invalidate();
                }
            }


            private 
    Color thumbPenColor Color.Silver;
            
    /// <summary>
            /// Gets or sets the color of the thumb pen.
            /// </summary>
            /// <value>The color of the thumb pen.</value>
            
    [Description("Set Slider thumb pen color")]
            [
    Category("ColorSlider")]
            [
    DefaultValue(typeof(Color), "Silver")]
            public 
    Color ThumbPenColor
            
    {
                
    get { return thumbPenColor; }
                
    set
                
    {
                    
    thumbPenColor value;
                    
    Invalidate();
                }
            }


            private 
    Color barOuterColor Color.SkyBlue;
            
    /// <summary>
            /// Gets or sets the outer color of the bar.
            /// </summary>
            /// <value>The outer color of the bar.</value>
            
    [Description("Set Slider bar outer color")]
            [
    Category("ColorSlider")]
            [
    DefaultValue(typeof(Color), "SkyBlue")]
            public 
    Color BarOuterColor
            
    {
                
    get { return barOuterColor; }
                
    set
                
    {
                    
    barOuterColor value;
                    
    Invalidate();
                }
            }


            private 
    Color barInnerColor Color.DarkSlateBlue;
            
    /// <summary>
            /// Gets or sets the inner color of the bar.
            /// </summary>
            /// <value>The inner color of the bar.</value>
            
    [Description("Set Slider bar inner color")]
            [
    Category("ColorSlider")]
            [
    DefaultValue(typeof(Color), "DarkSlateBlue")]
            public 
    Color BarInnerColor
            
    {
                
    get { return barInnerColor; }
                
    set
                
    {
                    
    barInnerColor value;
                    
    Invalidate();
                }
            }


            private 
    Color barPenColor Color.Gainsboro;
            
    /// <summary>
            /// Gets or sets the color of the bar pen.
            /// </summary>
            /// <value>The color of the bar pen.</value>
            
    [Description("Set Slider bar pen color")]
            [
    Category("ColorSlider")]
            [
    DefaultValue(typeof(Color), "Gainsboro")]
            public 
    Color BarPenColor
            
    {
                
    get { return barPenColor; }
                
    set
                
    {
                    
    barPenColor value;
                    
    Invalidate();
                }
            }

            private 
    Color elapsedOuterColor Color.DarkGreen;
            
    /// <summary>
            /// Gets or sets the outer color of the elapsed.
            /// </summary>
            /// <value>The outer color of the elapsed.</value>
            
    [Description("Set Slider's elapsed part outer color")]
            [
    Category("ColorSlider")]
            [
    DefaultValue(typeof(Color), "DarkGreen")]
            public 
    Color ElapsedOuterColor
            
    {
                
    get { return elapsedOuterColor; }
                
    set
                
    {
                    
    elapsedOuterColor value;
                    
    Invalidate();
                }
            }

            private 
    Color elapsedInnerColor Color.Chartreuse;
            
    /// <summary>
            /// Gets or sets the inner color of the elapsed.
            /// </summary>
            /// <value>The inner color of the elapsed.</value>
            
    [Description("Set Slider's elapsed part inner color")]
            [
    Category("ColorSlider")]
            [
    DefaultValue(typeof(Color), "Chartreuse")]
            public 
    Color ElapsedInnerColor
            
    {
                
    get { return elapsedInnerColor; }
                
    set
                
    {
                    
    elapsedInnerColor value;
                    
    Invalidate();
                }
            }

            
    #endregion

            #region Color schemas

            //define own color schemas
            
    private Color[,] aColorSchema = new Color[,]
                {
                    {
                        
    Color.WhiteColor.GainsboroColor.SilverColor.SkyBlueColor.DarkSlateBlueColor.Gainsboro,
                        
    Color.DarkGreenColor.Chartreuse
                    
    },
                    {
                        
    Color.WhiteColor.GainsboroColor.SilverColor.RedColor.DarkRedColor.GainsboroColor.Coral,
                        
    Color.LightCoral
                    
    },
                    {
                        
    Color.WhiteColor.GainsboroColor.SilverColor.GreenYellowColor.YellowColor.GoldColor.Orange,
                        
    Color.OrangeRed
                    
    },
                    {
                        
    Color.WhiteColor.GainsboroColor.SilverColor.RedColor.CrimsonColor.GainsboroColor.DarkViolet
                        
    Color.Violet
                    
    }
                };

            public 
    enum ColorSchemas
            
    {
                
    PerlBlueGreen,
                
    PerlRedCoral,
                
    PerlGold,
                
    PerlRoyalColors
            
    }

            private 
    ColorSchemas colorSchema ColorSchemas.PerlBlueGreen;
            
    /// <summary>
            /// Sets color schema. Color generalization / fast color changing. Has no effect when slider colors are changed manually after schema was applied. 
            /// </summary>
            /// <value>New color schema value</value>
            
    [Description("Set Slider color schema. Has no effect when slider colors are changed manually after schema was applied.")]
            [
    Category("ColorSlider")]
            [
    DefaultValue(typeof(ColorSchemas), "PerlBlueGreen")]
            public 
    ColorSchemas ColorSchema
            
    {
                
    get { return colorSchema; }
                
    set
                
    {
                    
    colorSchema value;
                    
    byte sn = (byte)value;
                    
    thumbOuterColor aColorSchema[sn0];
                    
    thumbInnerColor aColorSchema[sn1];
                    
    thumbPenColor aColorSchema[sn2];
                    
    barOuterColor aColorSchema[sn3];
                    
    barInnerColor aColorSchema[sn4];
                    
    barPenColor aColorSchema[sn5];
                    
    elapsedOuterColor aColorSchema[sn6];
                    
    elapsedInnerColor aColorSchema[sn7];

                    
    Invalidate();
                }
            }

            
    #endregion
            
            #region Constructors

            /// <summary>
            /// Initializes a new instance of the <see cref="ColorSlider"/> class.
            /// </summary>
            /// <param name="min">The minimum value.</param>
            /// <param name="max">The maximum value.</param>
            /// <param name="value">The current value.</param>
            
    public ColorSlider(int minint maxint value)
            {
                
    InitializeComponent();
                
    SetStyle(ControlStyles.AllPaintingInWmPaint ControlStyles.OptimizedDoubleBuffer |
                         
    ControlStyles.ResizeRedraw ControlStyles.Selectable |
                         
    ControlStyles.SupportsTransparentBackColor ControlStyles.UserMouse |
                         
    ControlStyles.UserPainttrue);
                
    BackColor Color.Transparent;

                
    Minimum min;
                
    Maximum max;
                
    Value value;
            }

            
    /// <summary>
            /// Initializes a new instance of the <see cref="ColorSlider"/> class.
            /// </summary>
            
    public ColorSlider() : this(010050) { }

            
    #endregion

            #region Paint

            /// <summary>
            /// Raises the <see cref="E:System.Windows.Forms.Control.Paint"></see> event.
            /// </summary>
            /// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs"></see> that contains the event data.</param>
            
    protected override void OnPaint(PaintEventArgs e)
            {
                if (!
    Enabled)
                {
                    
    Color[] desaturatedColors DesaturateColors(thumbOuterColorthumbInnerColorthumbPenColor,
                                                                 
    barOuterColorbarInnerColorbarPenColor,
                                                                 
    elapsedOuterColorelapsedInnerColor);
                    
    DrawColorSlider(edesaturatedColors[0], desaturatedColors[1], desaturatedColors[2],
                                    
    desaturatedColors[3],
                                    
    desaturatedColors[4], desaturatedColors[5], desaturatedColors[6], desaturatedColors[7]);
                }
                else
                {
                    if (
    mouseEffects && mouseInRegion)
                    {
                        
    Color[] lightenedColors LightenColors(thumbOuterColorthumbInnerColorthumbPenColor,
                                                                
    barOuterColorbarInnerColorbarPenColor,
                                                                
    elapsedOuterColorelapsedInnerColor);
                        
    DrawColorSlider(elightenedColors[0], lightenedColors[1], lightenedColors[2], lightenedColors[3],
                                        
    lightenedColors[4], lightenedColors[5], lightenedColors[6], lightenedColors[7]);
                    }
                    else
                    {
                        
    DrawColorSlider(ethumbOuterColorthumbInnerColorthumbPenColor,
                                        
    barOuterColorbarInnerColorbarPenColor,
                                        
    elapsedOuterColorelapsedInnerColor);
                    }
                }
            }

            
    /// <summary>
            /// Draws the colorslider control using passed colors.
            /// </summary>
            /// <param name="e">The <see cref="T:System.Windows.Forms.PaintEventArgs"/> instance containing the event data.</param>
            /// <param name="thumbOuterColorPaint">The thumb outer color paint.</param>
            /// <param name="thumbInnerColorPaint">The thumb inner color paint.</param>
            /// <param name="thumbPenColorPaint">The thumb pen color paint.</param>
            /// <param name="barOuterColorPaint">The bar outer color paint.</param>
            /// <param name="barInnerColorPaint">The bar inner color paint.</param>
            /// <param name="barPenColorPaint">The bar pen color paint.</param>
            /// <param name="elapsedOuterColorPaint">The elapsed outer color paint.</param>
            /// <param name="elapsedInnerColorPaint">The elapsed inner color paint.</param>
            
    private void DrawColorSlider(PaintEventArgs eColor thumbOuterColorPaintColor thumbInnerColorPaint,
                                         
    Color thumbPenColorPaintColor barOuterColorPaintColor barInnerColorPaint,
                                         
    Color barPenColorPaintColor elapsedOuterColorPaintColor elapsedInnerColorPaint)
            {
                try
                {
                    
    //set up thumbRect aproprietly
                    
    if (barOrientation == Orientation.Horizontal)
                    {
                        
    int TrackX = (((trackerValue barMinimum) * (ClientRectangle.Width thumbSize)) / (barMaximum barMinimum));
                        
    thumbRect = new Rectangle(TrackX1thumbSize 1ClientRectangle.Height 3);
                    }
                    else
                    {
                        
    int TrackY = (((trackerValue barMinimum) * (ClientRectangle.Height thumbSize)) / (barMaximum barMinimum));
                        
    thumbRect = new Rectangle(1TrackYClientRectangle.Width 3thumbSize 1);
                    }

                    
    //adjust drawing rects
                    
    barRect ClientRectangle;
                    
    thumbHalfRect thumbRect;
                    
    LinearGradientMode gradientOrientation;
                    if (
    barOrientation == Orientation.Horizontal)
                    {
                        
    barRect.Inflate(-1, -barRect.Height 3);
                        
    barHalfRect barRect;
                        
    barHalfRect.Height /= 2;
                        
    gradientOrientation LinearGradientMode.Vertical;
                        
    thumbHalfRect.Height /= 2;
                        
    elapsedRect barRect;
                        
    elapsedRect.Width thumbRect.Left thumbSize 2;
                    }
                    else
                    {
                        
    barRect.Inflate(-barRect.Width 3, -1);
                        
    barHalfRect barRect;
                        
    barHalfRect.Width /= 2;
                        
    gradientOrientation LinearGradientMode.Horizontal;
                        
    thumbHalfRect.Width /= 2;
                        
    elapsedRect barRect;
                        
    elapsedRect.Height thumbRect.Top thumbSize 2;
                    }
                    
    //get thumb shape path 
                    
    GraphicsPath thumbPath;
                    if (
    thumbCustomShape == null)
                        
    thumbPath CreateRoundRectPath(thumbRectthumbRoundRectSize);
                    else
                    {
                        
    thumbPath thumbCustomShape;
                        
    Matrix m = new Matrix();
                        
    m.Translate(thumbRect.Left thumbPath.GetBounds().LeftthumbRect.Top thumbPath.GetBounds().Top);
                        
    thumbPath.Transform(m);
                    }

                    
    //draw bar
                    
    using (
                        
    LinearGradientBrush lgbBar =
                            new 
    LinearGradientBrush(barHalfRectbarOuterColorPaintbarInnerColorPaintgradientOrientation)
                        )
                    {
                        
    lgbBar.WrapMode WrapMode.TileFlipXY;
                        
    e.Graphics.FillRectangle(lgbBarbarRect);
                        
    //draw elapsed bar
                        
    using (
                            
    LinearGradientBrush lgbElapsed =
                                new 
    LinearGradientBrush(barHalfRectelapsedOuterColorPaintelapsedInnerColorPaint,
                                                        
    gradientOrientation))
                        {
                            
    lgbElapsed.WrapMode WrapMode.TileFlipXY;
                            if (
    Capture && drawSemitransparentThumb)
                            {
                                
    Region elapsedReg = new Region(elapsedRect);
                                
    elapsedReg.Exclude(thumbPath);
                                
    e.Graphics.FillRegion(lgbElapsedelapsedReg);
                            }
                            else
                                
    e.Graphics.FillRectangle(lgbElapsedelapsedRect);
                        }
                        
    //draw bar band                    
                        
    using (Pen barPen = new Pen(barPenColorPaint0.5f))
                        {
                            
    e.Graphics.DrawRectangle(barPenbarRect);
                        }
                    }

                    
    //draw thumb
                    
    Color newthumbOuterColorPaint thumbOuterColorPaintnewthumbInnerColorPaint thumbInnerColorPaint;
                    if (
    Capture && drawSemitransparentThumb)
                    {
                        
    newthumbOuterColorPaint Color.FromArgb(175thumbOuterColorPaint);
                        
    newthumbInnerColorPaint Color.FromArgb(175thumbInnerColorPaint);
                    }
                    
    using (
                        
    LinearGradientBrush lgbThumb =
                            new 
    LinearGradientBrush(thumbHalfRectnewthumbOuterColorPaintnewthumbInnerColorPaint,
                                                    
    gradientOrientation))
                    {
                        
    lgbThumb.WrapMode WrapMode.TileFlipXY;
                        
    e.Graphics.SmoothingMode SmoothingMode.AntiAlias;
                        
    e.Graphics.FillPath(lgbThumbthumbPath);
                        
    //draw thumb band
                        
    Color newThumbPenColor thumbPenColorPaint;
                        if (
    mouseEffects && (Capture || mouseInThumbRegion))
                            
    newThumbPenColor ControlPaint.Dark(newThumbPenColor);
                        
    using (Pen thumbPen = new Pen(newThumbPenColor))
                        {
                            
    e.Graphics.DrawPath(thumbPenthumbPath);
                        }
                        
    //gp.Dispose();                    
                        /*if (Capture || mouseInThumbRegion)
                            using (LinearGradientBrush lgbThumb2 = new LinearGradientBrush(thumbHalfRect, Color.FromArgb(150, Color.Blue), Color.Transparent, gradientOrientation))
                            {
                                lgbThumb2.WrapMode = WrapMode.TileFlipXY;
                                e.Graphics.FillPath(lgbThumb2, gp);
                            }*/
                    
    }

                    
    //draw focusing rectangle
                    
    if (Focused drawFocusRectangle)
                        
    using (Pen p = new Pen(Color.FromArgb(200barPenColorPaint)))
                        {
                            
    p.DashStyle DashStyle.Dot;
                            
    Rectangle r ClientRectangle;
                            
    r.Width -= 2;
                            
    r.Height--;
                            
    r.X++;
                            
    //ControlPaint.DrawFocusRectangle(e.Graphics, r);                        
                            
    using (GraphicsPath gpBorder CreateRoundRectPath(rborderRoundRectSize))
                            {
                                
    e.Graphics.SmoothingMode SmoothingMode.AntiAlias;
                                
    e.Graphics.DrawPath(pgpBorder);
                            }
                        }
                }
                catch (
    Exception Err)
                {
                    
    Console.WriteLine("DrawBackGround Error in " Name ":" Err.Message);
                }
                finally
                {
                }
            }

            
    #endregion

            #region Overided events

            
    private bool mouseInRegion false;
            
    /// <summary>
            /// Raises the <see cref="E:System.Windows.Forms.Control.EnabledChanged"></see> event.
            /// </summary>
            /// <param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data.</param>
            
    protected override void OnEnabledChanged(EventArgs e)
            {
                
    base.OnEnabledChanged(e);
                
    Invalidate();
            }

            
    /// <summary>
            /// Raises the <see cref="E:System.Windows.Forms.Control.MouseEnter"></see> event.
            /// </summary>
            /// <param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data.</param>
            
    protected override void OnMouseEnter(EventArgs e)
            {
                
    base.OnMouseEnter(e);
                
    mouseInRegion true;
                
    Invalidate();
            }

            
    /// <summary>
            /// Raises the <see cref="E:System.Windows.Forms.Control.MouseLeave"></see> event.
            /// </summary>
            /// <param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data.</param>
            
    protected override void OnMouseLeave(EventArgs e)
            {
                
    base.OnMouseLeave(e);
                
    mouseInRegion false;
                
    mouseInThumbRegion false;
                
    Invalidate();
            }

            
    /// <summary>
            /// Raises the <see cref="E:System.Windows.Forms.Control.MouseDown"></see> event.
            /// </summary>
            /// <param name="e">A <see cref="T:System.Windows.Forms.MouseEventArgs"></see> that contains the event data.</param>
            
    protected override void OnMouseDown(MouseEventArgs e)
            {
                
    base.OnMouseDown(e);
                if (
    e.Button == MouseButtons.Left)
                {
                    
    Capture true;
                    if (
    Scroll != nullScroll(this, new ScrollEventArgs(ScrollEventType.ThumbTracktrackerValue));
                    if (
    ValueChanged != nullValueChanged(this, new EventArgs());
                    
    OnMouseMove(e);
                }
            }

            private 
    bool mouseInThumbRegion false;

            
    /// <summary>
            /// Raises the <see cref="E:System.Windows.Forms.Control.MouseMove"></see> event.
            /// </summary>
            /// <param name="e">A <see cref="T:System.Windows.Forms.MouseEventArgs"></see> that contains the event data.</param>
            
    protected override void OnMouseMove(MouseEventArgs e)
            {
                
    base.OnMouseMove(e);
                
    mouseInThumbRegion IsPointInRect(e.LocationthumbRect);
                if (
    Capture e.Button == MouseButtons.Left)
                {
                    
    ScrollEventType set ScrollEventType.ThumbPosition;
                    
    Point pt e.Location;
                    
    int p barOrientation == Orientation.Horizontal pt.pt.Y;
                    
    int margin thumbSize >> 1;
                    
    -= margin;
                    
    float coef = (float)(barMaximum barMinimum) /
                                 (float)
                                 ((
    barOrientation == Orientation.Horizontal ClientSize.Width ClientSize.Height) -
                                  
    margin);
                    
    trackerValue = (int)(coef barMinimum);

                    if (
    trackerValue <= barMinimum)
                    {
                        
    trackerValue barMinimum;
                        
    set ScrollEventType.First;
                    }
                    else if (
    trackerValue >= barMaximum)
                    {
                        
    trackerValue barMaximum;
                        
    set ScrollEventType.Last;
                    }

                    if (
    Scroll != nullScroll(this, new ScrollEventArgs(settrackerValue));
                    if (
    ValueChanged != nullValueChanged(this, new EventArgs());
                }
                
    Invalidate();
            }

            
    /// <summary>
            /// Raises the <see cref="E:System.Windows.Forms.Control.MouseUp"></see> event.
            /// </summary>
            /// <param name="e">A <see cref="T:System.Windows.Forms.MouseEventArgs"></see> that contains the event data.</param>
            
    protected override void OnMouseUp(MouseEventArgs e)
            {
                
    base.OnMouseUp(e);
                
    Capture false;
                
    mouseInThumbRegion IsPointInRect(e.LocationthumbRect);
                if (
    Scroll != nullScroll(this, new ScrollEventArgs(ScrollEventType.EndScrolltrackerValue));
                if (
    ValueChanged != nullValueChanged(this, new EventArgs());
                
    Invalidate();
            }

            
    /// <summary>
            /// Raises the <see cref="E:System.Windows.Forms.Control.MouseWheel"></see> event.
            /// </summary>
            /// <param name="e">A <see cref="T:System.Windows.Forms.MouseEventArgs"></see> that contains the event data.</param>
            
    protected override void OnMouseWheel(MouseEventArgs e)
            {
                
    base.OnMouseWheel(e);
                
    int v e.Delta 120 * (barMaximum barMinimum) / mouseWheelBarPartitions;
                
    SetProperValue(Value v);
            }

            
    /// <summary>
            /// Raises the <see cref="E:System.Windows.Forms.Control.GotFocus"></see> event.
            /// </summary>
            /// <param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data.</param>
            
    protected override void OnGotFocus(EventArgs e)
            {
                
    base.OnGotFocus(e);
                
    Invalidate();
            }

            
    /// <summary>
            /// Raises the <see cref="E:System.Windows.Forms.Control.LostFocus"></see> event.
            /// </summary>
            /// <param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data.</param>
            
    protected override void OnLostFocus(EventArgs e)
            {
                
    base.OnLostFocus(e);
                
    Invalidate();
            }

            
    /// <summary>
            /// Raises the <see cref="E:System.Windows.Forms.Control.KeyUp"></see> event.
            /// </summary>
            /// <param name="e">A <see cref="T:System.Windows.Forms.KeyEventArgs"></see> that contains the event data.</param>
            
    protected override void OnKeyUp(KeyEventArgs e)
            {
                
    base.OnKeyUp(e);
                switch (
    e.KeyCode)
                {
                    case 
    Keys.Down:
                    case 
    Keys.Left:
                        
    SetProperValue(Value - (int)smallChange);
                        if (
    Scroll != nullScroll(this, new ScrollEventArgs(ScrollEventType.SmallDecrementValue));
                        break;
                    case 
    Keys.Up:
                    case 
    Keys.Right:
                        
    SetProperValue(Value + (int)smallChange);
                        if (
    Scroll != nullScroll(this, new ScrollEventArgs(ScrollEventType.SmallIncrementValue));
                        break;
                    case 
    Keys.Home:
                        
    Value barMinimum;
                        break;
                    case 
    Keys.End:
                        
    Value barMaximum;
                        break;
                    case 
    Keys.PageDown:
                        
    SetProperValue(Value - (int)largeChange);
                        if (
    Scroll != nullScroll(this, new ScrollEventArgs(ScrollEventType.LargeDecrementValue));
                        break;
                    case 
    Keys.PageUp:
                        
    SetProperValue(Value + (int)largeChange);
                        if (
    Scroll != nullScroll(this, new ScrollEventArgs(ScrollEventType.LargeIncrementValue));
                        break;
                }
                if (
    Scroll != null && Value == barMinimumScroll(this, new ScrollEventArgs(ScrollEventType.FirstValue));
                if (
    Scroll != null && Value == barMaximumScroll(this, new ScrollEventArgs(ScrollEventType.LastValue));
                
    Point pt PointToClient(Cursor.Position);
                
    OnMouseMove(new MouseEventArgs(MouseButtons.None0pt.Xpt.Y0));
            }

            
    /// <summary>
            /// Processes a dialog key.
            /// </summary>
            /// <param name="keyData">One of the <see cref="T:System.Windows.Forms.Keys"></see> values that represents the key to process.</param>
            /// <returns>
            /// true if the key was processed by the control; otherwise, false.
            /// </returns>
            
    protected override bool ProcessDialogKey(Keys keyData)
            {
                if (
    keyData == Keys.Tab ModifierKeys == Keys.Shift)
                    return 
    base.ProcessDialogKey(keyData);
                else
                {
                    
    OnKeyDown(new KeyEventArgs(keyData));
                    return 
    true;
                }
            }

            
    #endregion

            #region Help routines

            /// <summary>
            /// Creates the round rect path.
            /// </summary>
            /// <param name="rect">The rectangle on which graphics path will be spanned.</param>
            /// <param name="size">The size of rounded rectangle edges.</param>
            /// <returns></returns>
            
    public static GraphicsPath CreateRoundRectPath(Rectangle rectSize size)
            {
                
    GraphicsPath gp = new GraphicsPath();
                
    gp.AddLine(rect.Left size.Width 2rect.Toprect.Right size.Width 2rect.Top);
                
    gp.AddArc(rect.Right size.Widthrect.Topsize.Widthsize.Height27090);

                
    gp.AddLine(rect.Rightrect.Top size.Height 2rect.Rightrect.Bottom size.Width 2);
                
    gp.AddArc(rect.Right size.Widthrect.Bottom size.Heightsize.Widthsize.Height090);

                
    gp.AddLine(rect.Right size.Width 2rect.Bottomrect.Left size.Width 2rect.Bottom);
                
    gp.AddArc(rect.Leftrect.Bottom size.Heightsize.Widthsize.Height9090);

                
    gp.AddLine(rect.Leftrect.Bottom size.Height 2rect.Leftrect.Top size.Height 2);
                
    gp.AddArc(rect.Leftrect.Topsize.Widthsize.Height18090);
                return 
    gp;
            }

            
    /// <summary>
            /// Desaturates colors from given array.
            /// </summary>
            /// <param name="colorsToDesaturate">The colors to be desaturated.</param>
            /// <returns></returns>
            
    public static Color[] DesaturateColors(params Color[] colorsToDesaturate)
            {
                
    Color[] colorsToReturn = new Color[colorsToDesaturate.Length];
                for (
    int i 0colorsToDesaturate.Lengthi++)
                {
                    
    //use NTSC weighted avarage
                    
    int gray =
                        (int)(
    colorsToDesaturate[i].0.3 colorsToDesaturate[i].0.6 colorsToDesaturate[i].0.1);
                    
    colorsToReturn[i] = Color.FromArgb(-0x010101 * (255 gray) - 1);
                }
                return 
    colorsToReturn;
            }

            
    /// <summary>
            /// Lightens colors from given array.
            /// </summary>
            /// <param name="colorsToLighten">The colors to lighten.</param>
            /// <returns></returns>
            
    public static Color[] LightenColors(params Color[] colorsToLighten)
            {
                
    Color[] colorsToReturn = new Color[colorsToLighten.Length];
                for (
    int i 0colorsToLighten.Lengthi++)
                {
                    
    colorsToReturn[i] = ControlPaint.Light(colorsToLighten[i]);
                }
                return 
    colorsToReturn;
            }

            
    /// <summary>
            /// Sets the trackbar value so that it wont exceed allowed range.
            /// </summary>
            /// <param name="val">The value.</param>
            
    private void SetProperValue(int val)
            {
                if (
    val barMinimumValue barMinimum;
                else if (
    val barMaximumValue barMaximum;
                else 
    Value val;
            }

            
    /// <summary>
            /// Determines whether rectangle contains given point.
            /// </summary>
            /// <param name="pt">The point to test.</param>
            /// <param name="rect">The base rectangle.</param>
            /// <returns>
            ///     <c>true</c> if rectangle contains given point; otherwise, <c>false</c>.
            /// </returns>
            
    private static bool IsPointInRect(Point ptRectangle rect)
            {
                if (
    pt.rect.Left pt.rect.Right pt.rect.Top pt.rect.Bottom)
                    return 
    true;
                else return 
    false;
            }

            
    #endregion
        
    }


    Es ist eine Trackbar - wie der Threadtitel schon sagt - welches von dem Coder modifziert, sogar neu gecodet wurde. Die Credits gehen an den unbekannten Coder.

    Hier die gesamte Projektmappe: DOWNLOAD

    Viel Spaß damit. Ich finde es schicker als die Standard Trackbar die hässlich grau ist.
    Geändert von Darkfield (27.03.2014 um 06:43 Uhr)

Diese Seite nutzt Cookies, um das Nutzererlebnis zu verbessern. Klicken Sie hier, um das Cookie-Tracking zu deaktivieren.