PageRenderTime 76ms CodeModel.GetById 10ms app.highlight 59ms RepoModel.GetById 2ms app.codeStats 0ms

/Skimpt3/Skimpt3/controls/GlassButton.cs

http://skimpt.googlecode.com/
C# | 908 lines | 717 code | 76 blank | 115 comment | 99 complexity | e266ed0d4f8178a749dc35b0eea84ccd MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.ComponentModel;
  4using System.Data;
  5using System.Drawing;
  6using System.Text;
  7using System.Windows.Forms;
  8using System.Drawing.Drawing2D;
  9using System.Drawing.Text;
 10using PushButtonState = System.Windows.Forms.VisualStyles.PushButtonState;
 11
 12namespace SkimptControls
 13{
 14    /// <summary>
 15    /// Represents a glass button control.
 16    /// </summary>
 17    [ToolboxBitmap(typeof(GlassButton)), ToolboxItem(true), ToolboxItemFilter("System.Windows.Forms"), Description("Raises an event when the user clicks it.")]
 18    public partial class GlassButton : Button
 19    {
 20        #region " Constructors "
 21
 22        /// <summary>
 23        /// Initializes a new instance of the <see cref="Glass.GlassButton" /> class.
 24        /// </summary>
 25        public GlassButton()
 26        {
 27            InitializeComponent();
 28            timer.Interval = animationLength / framesCount;
 29            base.BackColor = Color.Transparent;
 30            BackColor = Color.Black;
 31            ForeColor = Color.White;
 32            OuterBorderColor = Color.White;
 33            InnerBorderColor = Color.Black;
 34            ShineColor = Color.White;
 35            GlowColor = Color.FromArgb(-7488001);//unchecked((int)(0xFF8DBDFF)));
 36            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint, true);
 37            SetStyle(ControlStyles.Opaque, false);
 38        }
 39
 40        #endregion
 41
 42        #region " Fields and Properties "
 43
 44        private Color _backColor;
 45        /// <summary>
 46        /// Gets or sets the background color of the control.
 47        /// </summary>
 48        /// <returns>A <see cref="T:System.Drawing.Color" /> value representing the background color.</returns>
 49        [DefaultValue(typeof(Color), "Black")]
 50        public virtual new Color BackColor
 51        {
 52            get { return _backColor; }
 53            set
 54            {
 55                if (!_backColor.Equals(value))
 56                {
 57                    _backColor = value;
 58                    UseVisualStyleBackColor = false;
 59                    CreateFrames();
 60                    OnBackColorChanged(EventArgs.Empty);
 61                }
 62            }
 63        }
 64
 65        /// <summary>
 66        /// Gets or sets the foreground color of the control.
 67        /// </summary>
 68        /// <returns>The foreground <see cref="T:System.Drawing.Color" /> of the control.</returns>
 69        [DefaultValue(typeof(Color), "White")]
 70        public virtual new Color ForeColor
 71        {
 72            get { return base.ForeColor; }
 73            set
 74            {
 75                base.ForeColor = value;
 76            }
 77        }
 78
 79        private Color _innerBorderColor;
 80        /// <summary>
 81        /// Gets or sets the inner border color of the control.
 82        /// </summary>
 83        /// <returns>A <see cref="T:System.Drawing.Color" /> value representing the color of the inner border.</returns>
 84        [DefaultValue(typeof(Color), "Black"), Category("Appearance"), Description("The inner border color of the control.")]
 85        public virtual Color InnerBorderColor
 86        {
 87            get { return _innerBorderColor; }
 88            set
 89            {
 90                if (_innerBorderColor != value)
 91                {
 92                    _innerBorderColor = value;
 93                    CreateFrames();
 94                    if (IsHandleCreated)
 95                    {
 96                        Invalidate();
 97                    }
 98                    OnInnerBorderColorChanged(EventArgs.Empty);
 99                }
100            }
101        }
102
103        private Color _outerBorderColor;
104        /// <summary>
105        /// Gets or sets the outer border color of the control.
106        /// </summary>
107        /// <returns>A <see cref="T:System.Drawing.Color" /> value representing the color of the outer border.</returns>
108        [DefaultValue(typeof(Color), "White"), Category("Appearance"), Description("The outer border color of the control.")]
109        public virtual Color OuterBorderColor
110        {
111            get { return _outerBorderColor; }
112            set
113            {
114                if (_outerBorderColor != value)
115                {
116                    _outerBorderColor = value;
117                    CreateFrames();
118                    if (IsHandleCreated)
119                    {
120                        Invalidate();
121                    }
122                    OnOuterBorderColorChanged(EventArgs.Empty);
123                }
124            }
125        }
126
127        private Color _shineColor;
128        /// <summary>
129        /// Gets or sets the shine color of the control.
130        /// </summary>
131        /// <returns>A <see cref="T:System.Drawing.Color" /> value representing the shine color.</returns>
132        [DefaultValue(typeof(Color), "White"), Category("Appearance"), Description("The shine color of the control.")]
133        public virtual Color ShineColor
134        {
135            get { return _shineColor; }
136            set
137            {
138                if (_shineColor != value)
139                {
140                    _shineColor = value;
141                    CreateFrames();
142                    if (IsHandleCreated)
143                    {
144                        Invalidate();
145                    }
146                    OnShineColorChanged(EventArgs.Empty);
147                }
148            }
149        }
150
151        private Color _glowColor;
152        /// <summary>
153        /// Gets or sets the glow color of the control.
154        /// </summary>
155        /// <returns>A <see cref="T:System.Drawing.Color" /> value representing the glow color.</returns>
156        [DefaultValue(typeof(Color), "255,141,189,255"), Category("Appearance"), Description("The glow color of the control.")]
157        public virtual Color GlowColor
158        {
159            get { return _glowColor; }
160            set
161            {
162                if (_glowColor != value)
163                {
164                    _glowColor = value;
165                    CreateFrames();
166                    if (IsHandleCreated)
167                    {
168                        Invalidate();
169                    }
170                    OnGlowColorChanged(EventArgs.Empty);
171                }
172            }
173        }
174
175        private bool _fadeOnFocus;
176        /// <summary>
177        /// Gets or sets a value indicating whether the button should fade in and fade out when it's getting and loosing the focus.
178        /// </summary>
179        /// <value><c>true</c> if fading with changing the focus; otherwise, <c>false</c>.</value>
180        [DefaultValue(false), Category("Appearance"), Description("Indicates whether the button should fade in and fade out when it is getting and loosing the focus.")]
181        public virtual bool FadeOnFocus
182        {
183            get { return _fadeOnFocus; }
184            set
185            {
186                if (_fadeOnFocus != value)
187                {
188                    _fadeOnFocus = value;
189                }
190            }
191        }
192
193        private bool _isHovered;
194        private bool _isFocused;
195        private bool _isFocusedByKey;
196        private bool _isKeyDown;
197        private bool _isMouseDown;
198        private bool IsPressed { get { return _isKeyDown || (_isMouseDown && _isHovered); } }
199
200        /// <summary>
201        /// Gets the state of the button control.
202        /// </summary>
203        /// <value>The state of the button control.</value>
204        [Browsable(false)]
205        public PushButtonState State
206        {
207            get
208            {
209                if (!Enabled)
210                {
211                    return PushButtonState.Disabled;
212                }
213                if (IsPressed)
214                {
215                    return PushButtonState.Pressed;
216                }
217                if (_isHovered)
218                {
219                    return PushButtonState.Hot;
220                }
221                if (_isFocused || IsDefault)
222                {
223                    return PushButtonState.Default;
224                }
225                return PushButtonState.Normal;
226            }
227        }
228
229        #endregion
230
231        #region " Events "
232
233        /// <summary>Occurs when the value of the <see cref="P:Glass.GlassButton.InnerBorderColor" /> property changes.</summary>
234        [Description("Event raised when the value of the InnerBorderColor property is changed."), Category("Property Changed")]
235        public event EventHandler InnerBorderColorChanged;
236
237        /// <summary>
238        /// Raises the <see cref="E:Glass.GlassButton.InnerBorderColorChanged" /> event.
239        /// </summary>
240        /// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
241        protected virtual void OnInnerBorderColorChanged(EventArgs e)
242        {
243            if (InnerBorderColorChanged != null)
244            {
245                InnerBorderColorChanged(this, e);
246            }
247        }
248
249        /// <summary>Occurs when the value of the <see cref="P:Glass.GlassButton.OuterBorderColor" /> property changes.</summary>
250        [Description("Event raised when the value of the OuterBorderColor property is changed."), Category("Property Changed")]
251        public event EventHandler OuterBorderColorChanged;
252
253        /// <summary>
254        /// Raises the <see cref="E:Glass.GlassButton.OuterBorderColorChanged" /> event.
255        /// </summary>
256        /// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
257        protected virtual void OnOuterBorderColorChanged(EventArgs e)
258        {
259            if (OuterBorderColorChanged != null)
260            {
261                OuterBorderColorChanged(this, e);
262            }
263        }
264
265        /// <summary>Occurs when the value of the <see cref="P:Glass.GlassButton.ShineColor" /> property changes.</summary>
266        [Description("Event raised when the value of the ShineColor property is changed."), Category("Property Changed")]
267        public event EventHandler ShineColorChanged;
268
269        /// <summary>
270        /// Raises the <see cref="E:Glass.GlassButton.ShineColorChanged" /> event.
271        /// </summary>
272        /// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
273        protected virtual void OnShineColorChanged(EventArgs e)
274        {
275            if (ShineColorChanged != null)
276            {
277                ShineColorChanged(this, e);
278            }
279        }
280
281        /// <summary>Occurs when the value of the <see cref="P:Glass.GlassButton.GlowColor" /> property changes.</summary>
282        [Description("Event raised when the value of the GlowColor property is changed."), Category("Property Changed")]
283        public event EventHandler GlowColorChanged;
284
285        /// <summary>
286        /// Raises the <see cref="E:Glass.GlassButton.GlowColorChanged" /> event.
287        /// </summary>
288        /// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
289        protected virtual void OnGlowColorChanged(EventArgs e)
290        {
291            if (GlowColorChanged != null)
292            {
293                GlowColorChanged(this, e);
294            }
295        }
296
297        #endregion
298
299        #region " Overrided Methods "
300
301        /// <summary>
302        /// Raises the <see cref="E:System.Windows.Forms.Control.SizeChanged" /> event.
303        /// </summary>
304        /// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
305        protected override void OnSizeChanged(EventArgs e)
306        {
307            CreateFrames();
308            base.OnSizeChanged(e);
309        }
310
311        /// <summary>
312        /// Raises the <see cref="E:System.Windows.Forms.Control.Click" /> event.
313        /// </summary>
314        /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
315        protected override void OnClick(EventArgs e)
316        {
317            _isKeyDown = _isMouseDown = false;
318            base.OnClick(e);
319        }
320
321        /// <summary>
322        /// Raises the <see cref="E:System.Windows.Forms.Control.Enter" /> event.
323        /// </summary>
324        /// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
325        protected override void OnEnter(EventArgs e)
326        {
327            _isFocused = _isFocusedByKey = true;
328            base.OnEnter(e);
329            if (_fadeOnFocus)
330            {
331                FadeIn();
332            }
333        }
334
335        /// <summary>
336        /// Raises the <see cref="E:System.Windows.Forms.Control.Leave" /> event.
337        /// </summary>
338        /// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
339        protected override void OnLeave(EventArgs e)
340        {
341            base.OnLeave(e);
342            _isFocused = _isFocusedByKey = _isKeyDown = _isMouseDown = false;
343            Invalidate();
344            if (_fadeOnFocus)
345            {
346                FadeOut();
347            }
348        }
349
350        /// <summary>
351        /// Raises the <see cref="M:System.Windows.Forms.ButtonBase.OnKeyUp(System.Windows.Forms.KeyEventArgs)" /> event.
352        /// </summary>
353        /// <param name="e">A <see cref="T:System.Windows.Forms.KeyEventArgs" /> that contains the event data.</param>
354        protected override void OnKeyDown(KeyEventArgs e)
355        {
356            if (e.KeyCode == Keys.Space)
357            {
358                _isKeyDown = true;
359                Invalidate();
360            }
361            base.OnKeyDown(e);
362        }
363
364        /// <summary>
365        /// Raises the <see cref="M:System.Windows.Forms.ButtonBase.OnKeyUp(System.Windows.Forms.KeyEventArgs)" /> event.
366        /// </summary>
367        /// <param name="e">A <see cref="T:System.Windows.Forms.KeyEventArgs" /> that contains the event data.</param>
368        protected override void OnKeyUp(KeyEventArgs e)
369        {
370            if (_isKeyDown && e.KeyCode == Keys.Space)
371            {
372                _isKeyDown = false;
373                Invalidate();
374            }
375            base.OnKeyUp(e);
376        }
377
378        /// <summary>
379        /// Raises the <see cref="E:System.Windows.Forms.Control.MouseDown" /> event.
380        /// </summary>
381        /// <param name="e">A <see cref="T:System.Windows.Forms.MouseEventArgs" /> that contains the event data.</param>
382        protected override void OnMouseDown(MouseEventArgs e)
383        {
384            if (!_isMouseDown && e.Button == MouseButtons.Left)
385            {
386                _isMouseDown = true;
387                _isFocusedByKey = false;
388                Invalidate();
389            }
390            base.OnMouseDown(e);
391        }
392
393        /// <summary>
394        /// Raises the <see cref="E:System.Windows.Forms.Control.MouseUp" /> event.
395        /// </summary>
396        /// <param name="e">A <see cref="T:System.Windows.Forms.MouseEventArgs" /> that contains the event data.</param>
397        protected override void OnMouseUp(MouseEventArgs e)
398        {
399            if (_isMouseDown)
400            {
401                _isMouseDown = false;
402                Invalidate();
403            }
404            base.OnMouseUp(e);
405        }
406
407        /// <summary>
408        /// Raises the <see cref="M:System.Windows.Forms.Control.OnMouseMove(System.Windows.Forms.MouseEventArgs)" /> event.
409        /// </summary>
410        /// <param name="e">A <see cref="T:System.Windows.Forms.MouseEventArgs" /> that contains the event data.</param>
411        protected override void OnMouseMove(MouseEventArgs e)
412        {
413            base.OnMouseMove(e);
414            if (e.Button != MouseButtons.None)
415            {
416                if (!ClientRectangle.Contains(e.X, e.Y))
417                {
418                    if (_isHovered)
419                    {
420                        _isHovered = false;
421                        Invalidate();
422                    }
423                }
424                else if (!_isHovered)
425                {
426                    _isHovered = true;
427                    Invalidate();
428                }
429            }
430        }
431
432        /// <summary>
433        /// Raises the <see cref="E:System.Windows.Forms.Control.MouseEnter" /> event.
434        /// </summary>
435        /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
436        protected override void OnMouseEnter(EventArgs e)
437        {
438            _isHovered = true;
439            FadeIn();
440            Invalidate();
441            base.OnMouseEnter(e);
442        }
443
444        /// <summary>
445        /// Raises the <see cref="E:System.Windows.Forms.Control.MouseLeave" /> event.
446        /// </summary>
447        /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
448        protected override void OnMouseLeave(EventArgs e)
449        {
450            _isHovered = false;
451            if (!(FadeOnFocus && _isFocusedByKey)) FadeOut();
452            Invalidate();
453            base.OnMouseLeave(e);
454        }
455
456        #endregion
457
458        #region " Painting "
459
460        /// <summary>
461        /// Raises the <see cref="M:System.Windows.Forms.ButtonBase.OnPaint(System.Windows.Forms.PaintEventArgs)" /> event.
462        /// </summary>
463        /// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs" /> that contains the event data.</param>
464        protected override void OnPaint(PaintEventArgs e)
465        {
466            DrawButtonBackgroundFromBuffer(e.Graphics);
467            DrawForegroundFromButton(e);
468            DrawButtonForeground(e.Graphics);
469
470            if (Paint != null)
471            {
472                Paint(this, e);
473            }
474        }
475
476        /// <summary>
477        /// Occurs when the control is redrawn.
478        /// </summary>
479        public new event PaintEventHandler Paint;
480
481        private void DrawButtonBackgroundFromBuffer(Graphics graphics)
482        {
483            int frame;
484            if (!Enabled)
485            {
486                frame = FRAME_DISABLED;
487            }
488            else if (IsPressed)
489            {
490                frame = FRAME_PRESSED;
491            }
492            else if (!IsAnimating && _currentFrame == 0)
493            {
494                frame = FRAME_NORMAL;
495            }
496            else
497            {
498                if (!HasAnimationFrames)
499                {
500                    CreateFrames(true);
501                }
502                frame = FRAME_ANIMATED + _currentFrame;
503            }
504            if (_frames == null || _frames.Count == 0)
505            {
506                CreateFrames();
507            }
508            graphics.DrawImage(_frames[frame], Point.Empty);
509        }
510
511        private Image CreateBackgroundFrame(bool pressed, bool hovered,
512            bool animating, bool enabled, float glowOpacity)
513        {
514            Rectangle rect = ClientRectangle;
515            if (rect.Width <= 0)
516            {
517                rect.Width = 1;
518            }
519            if (rect.Height <= 0)
520            {
521                rect.Height = 1;
522            }
523            Image img = new Bitmap(rect.Width, rect.Height);
524            using (Graphics g = Graphics.FromImage(img))
525            {
526                g.Clear(Color.Transparent);
527                DrawButtonBackground(g, rect, pressed, hovered, animating, enabled,
528                    _outerBorderColor, _backColor, _glowColor, _shineColor, _innerBorderColor,
529                    glowOpacity);
530            }
531            return img;
532        }
533
534        private static void DrawButtonBackground(Graphics g, Rectangle rectangle,
535            bool pressed, bool hovered, bool animating, bool enabled,
536            Color outerBorderColor, Color backColor, Color glowColor, Color shineColor,
537            Color innerBorderColor, float glowOpacity)
538        {
539            SmoothingMode sm = g.SmoothingMode;
540            g.SmoothingMode = SmoothingMode.AntiAlias;
541
542            #region " white border "
543            Rectangle rect = rectangle;
544            rect.Width--;
545            rect.Height--;
546            using (GraphicsPath bw = CreateRoundRectangle(rect, 4))
547            {
548                using (Pen p = new Pen(outerBorderColor))
549                {
550                    g.DrawPath(p, bw);
551                }
552            }
553            #endregion
554
555            rect.X++;
556            rect.Y++;
557            rect.Width -= 2;
558            rect.Height -= 2;
559            Rectangle rect2 = rect;
560            rect2.Height >>= 1;
561
562            #region " content "
563            using (GraphicsPath bb = CreateRoundRectangle(rect, 2))
564            {
565                int opacity = pressed ? 0xcc : 0x7f;
566                using (Brush br = new SolidBrush(Color.FromArgb(opacity, backColor)))
567                {
568                    g.FillPath(br, bb);
569                }
570            }
571            #endregion
572
573            #region " glow "
574            if ((hovered || animating) && !pressed)
575            {
576                using (GraphicsPath clip = CreateRoundRectangle(rect, 2))
577                {
578                    g.SetClip(clip, CombineMode.Intersect);
579                    using (GraphicsPath brad = CreateBottomRadialPath(rect))
580                    {
581                        using (PathGradientBrush pgr = new PathGradientBrush(brad))
582                        {
583                            int opacity = (int)(0xB2 * glowOpacity + .5f);
584                            RectangleF bounds = brad.GetBounds();
585                            pgr.CenterPoint = new PointF((bounds.Left + bounds.Right) / 2f, (bounds.Top + bounds.Bottom) / 2f);
586                            pgr.CenterColor = Color.FromArgb(opacity, glowColor);
587                            pgr.SurroundColors = new Color[] { Color.FromArgb(0, glowColor) };
588                            g.FillPath(pgr, brad);
589                        }
590                    }
591                    g.ResetClip();
592                }
593            }
594            #endregion
595
596            #region " shine "
597            if (rect2.Width > 0 && rect2.Height > 0)
598            {
599                rect2.Height++;
600                using (GraphicsPath bh = CreateTopRoundRectangle(rect2, 2))
601                {
602                    rect2.Height++;
603                    int opacity = 0x99;
604                    if (pressed | !enabled)
605                    {
606                        opacity = (int)(.4f * opacity + .5f);
607                    }
608                    using (LinearGradientBrush br = new LinearGradientBrush(rect2, Color.FromArgb(opacity, shineColor), Color.FromArgb(opacity / 3, shineColor), LinearGradientMode.Vertical))
609                    {
610                        g.FillPath(br, bh);
611                    }
612                }
613                rect2.Height -= 2;
614            }
615            #endregion
616
617            #region " black border "
618            using (GraphicsPath bb = CreateRoundRectangle(rect, 3))
619            {
620                using (Pen p = new Pen(innerBorderColor))
621                {
622                    g.DrawPath(p, bb);
623                }
624            }
625            #endregion
626
627            g.SmoothingMode = sm;
628        }
629
630        private void DrawButtonForeground(Graphics g)
631        {
632            if (Focused && ShowFocusCues/* && isFocusedByKey*/)
633            {
634                Rectangle rect = ClientRectangle;
635                rect.Inflate(-4, -4);
636                ControlPaint.DrawFocusRectangle(g, rect);
637            }
638        }
639
640        private Button _imageButton;
641        private void DrawForegroundFromButton(PaintEventArgs pevent)
642        {
643            if (_imageButton == null)
644            {
645                _imageButton = new Button();
646                _imageButton.Parent = new TransparentControl();
647                _imageButton.SuspendLayout();
648                _imageButton.BackColor = Color.Transparent;
649                _imageButton.FlatAppearance.BorderSize = 0;
650                _imageButton.FlatStyle = FlatStyle.Flat;
651            }
652            else
653            {
654                _imageButton.SuspendLayout();
655            }
656            _imageButton.AutoEllipsis = AutoEllipsis;
657            if (Enabled)
658            {
659                _imageButton.ForeColor = ForeColor;
660            }
661            else
662            {
663                _imageButton.ForeColor = Color.FromArgb((3 * ForeColor.R + _backColor.R) >> 2,
664                    (3 * ForeColor.G + _backColor.G) >> 2,
665                    (3 * ForeColor.B + _backColor.B) >> 2);
666            }
667            _imageButton.Font = Font;
668            _imageButton.RightToLeft = RightToLeft;
669            _imageButton.Image = Image;
670            if (Image != null && !Enabled)
671            {
672                Size size = Image.Size;
673                float[][] newColorMatrix = new float[5][];
674                newColorMatrix[0] = new float[] { 0.2125f, 0.2125f, 0.2125f, 0f, 0f };
675                newColorMatrix[1] = new float[] { 0.2577f, 0.2577f, 0.2577f, 0f, 0f };
676                newColorMatrix[2] = new float[] { 0.0361f, 0.0361f, 0.0361f, 0f, 0f };
677                float[] arr = new float[5];
678                arr[3] = 1f;
679                newColorMatrix[3] = arr;
680                newColorMatrix[4] = new float[] { 0.38f, 0.38f, 0.38f, 0f, 1f };
681                System.Drawing.Imaging.ColorMatrix matrix = new System.Drawing.Imaging.ColorMatrix(newColorMatrix);
682                System.Drawing.Imaging.ImageAttributes disabledImageAttr = new System.Drawing.Imaging.ImageAttributes();
683                disabledImageAttr.ClearColorKey();
684                disabledImageAttr.SetColorMatrix(matrix);
685                _imageButton.Image = new Bitmap(Image.Width, Image.Height);
686                using (Graphics gr = Graphics.FromImage(_imageButton.Image))
687                {
688                    gr.DrawImage(Image, new Rectangle(0, 0, size.Width, size.Height), 0, 0, size.Width, size.Height, GraphicsUnit.Pixel, disabledImageAttr);
689                }
690            }
691            _imageButton.ImageAlign = ImageAlign;
692            _imageButton.ImageIndex = ImageIndex;
693            _imageButton.ImageKey = ImageKey;
694            _imageButton.ImageList = ImageList;
695            _imageButton.Padding = Padding;
696            _imageButton.Size = Size;
697            _imageButton.Text = Text;
698            _imageButton.TextAlign = TextAlign;
699            _imageButton.TextImageRelation = TextImageRelation;
700            _imageButton.UseCompatibleTextRendering = UseCompatibleTextRendering;
701            _imageButton.UseMnemonic = UseMnemonic;
702            _imageButton.ResumeLayout();
703            InvokePaint(_imageButton, pevent);
704            if (_imageButton.Image != null && _imageButton.Image != Image)
705            {
706                _imageButton.Image.Dispose();
707                _imageButton.Image = null;
708            }
709        }
710
711        private class TransparentControl : Control
712        {
713            protected override void OnPaintBackground(PaintEventArgs pevent) { }
714            protected override void OnPaint(PaintEventArgs e) { }
715        }
716
717        private static GraphicsPath CreateRoundRectangle(Rectangle rectangle, int radius)
718        {
719            GraphicsPath path = new GraphicsPath();
720            int l = rectangle.Left;
721            int t = rectangle.Top;
722            int w = rectangle.Width;
723            int h = rectangle.Height;
724            int d = radius << 1;
725            path.AddArc(l, t, d, d, 180, 90); // topleft
726            path.AddLine(l + radius, t, l + w - radius, t); // top
727            path.AddArc(l + w - d, t, d, d, 270, 90); // topright
728            path.AddLine(l + w, t + radius, l + w, t + h - radius); // right
729            path.AddArc(l + w - d, t + h - d, d, d, 0, 90); // bottomright
730            path.AddLine(l + w - radius, t + h, l + radius, t + h); // bottom
731            path.AddArc(l, t + h - d, d, d, 90, 90); // bottomleft
732            path.AddLine(l, t + h - radius, l, t + radius); // left
733            path.CloseFigure();
734            return path;
735        }
736
737        private static GraphicsPath CreateTopRoundRectangle(Rectangle rectangle, int radius)
738        {
739            GraphicsPath path = new GraphicsPath();
740            int l = rectangle.Left;
741            int t = rectangle.Top;
742            int w = rectangle.Width;
743            int h = rectangle.Height;
744            int d = radius << 1;
745            path.AddArc(l, t, d, d, 180, 90); // topleft
746            path.AddLine(l + radius, t, l + w - radius, t); // top
747            path.AddArc(l + w - d, t, d, d, 270, 90); // topright
748            path.AddLine(l + w, t + radius, l + w, t + h); // right
749            path.AddLine(l + w, t + h, l, t + h); // bottom
750            path.AddLine(l, t + h, l, t + radius); // left
751            path.CloseFigure();
752            return path;
753        }
754
755        private static GraphicsPath CreateBottomRadialPath(Rectangle rectangle)
756        {
757            GraphicsPath path = new GraphicsPath();
758            RectangleF rect = rectangle;
759            rect.X -= rect.Width * .35f;
760            rect.Y -= rect.Height * .15f;
761            rect.Width *= 1.7f;
762            rect.Height *= 2.3f;
763            path.AddEllipse(rect);
764            path.CloseFigure();
765            return path;
766        }
767
768        #endregion
769
770        #region " Unused Properties & Events "
771
772        /// <summary>This property is not relevant for this class.</summary>
773        /// <returns>This property is not relevant for this class.</returns>
774        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)]
775        public new FlatButtonAppearance FlatAppearance
776        {
777            get { return base.FlatAppearance; }
778        }
779
780        /// <summary>This property is not relevant for this class.</summary>
781        /// <returns>This property is not relevant for this class.</returns>
782        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)]
783        public new FlatStyle FlatStyle
784        {
785            get { return base.FlatStyle; }
786            set { base.FlatStyle = value; }
787        }
788
789        /// <summary>This property is not relevant for this class.</summary>
790        /// <returns>This property is not relevant for this class.</returns>
791        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)]
792        public new bool UseVisualStyleBackColor
793        {
794            get { return base.UseVisualStyleBackColor; }
795            set { base.UseVisualStyleBackColor = value; }
796        }
797
798        #endregion
799
800        #region " Animation Support "
801
802        private List<Image> _frames;
803
804        private const int FRAME_DISABLED = 0;
805        private const int FRAME_PRESSED = 1;
806        private const int FRAME_NORMAL = 2;
807        private const int FRAME_ANIMATED = 3;
808
809        private bool HasAnimationFrames
810        {
811            get
812            {
813                return _frames != null && _frames.Count > FRAME_ANIMATED;
814            }
815        }
816
817        private void CreateFrames()
818        {
819            CreateFrames(false);
820        }
821
822        private void CreateFrames(bool withAnimationFrames)
823        {
824            DestroyFrames();
825            if (!IsHandleCreated)
826            {
827                return;
828            }
829            if (_frames == null)
830            {
831                _frames = new List<Image>();
832            }
833            _frames.Add(CreateBackgroundFrame(false, false, false, false, 0));
834            _frames.Add(CreateBackgroundFrame(true, true, false, true, 0));
835            _frames.Add(CreateBackgroundFrame(false, false, false, true, 0));
836            if (!withAnimationFrames)
837            {
838                return;
839            }
840            for (int i = 0; i < framesCount; i++)
841            {
842                _frames.Add(CreateBackgroundFrame(false, true, true, true, (float)i / (framesCount - 1F)));
843            }
844        }
845
846        private void DestroyFrames()
847        {
848            if (_frames != null)
849            {
850                while (_frames.Count > 0)
851                {
852                    _frames[_frames.Count - 1].Dispose();
853                    _frames.RemoveAt(_frames.Count - 1);
854                }
855            }
856        }
857
858        private const int animationLength = 300;
859        private const int framesCount = 10;
860        private int _currentFrame;
861        private int _direction;
862
863        private bool IsAnimating
864        {
865            get
866            {
867                return _direction != 0;
868            }
869        }
870
871        private void FadeIn()
872        {
873            _direction = 1;
874            timer.Enabled = true;
875        }
876
877        private void FadeOut()
878        {
879            _direction = -1;
880            timer.Enabled = true;
881        }
882
883        private void timer_Tick(object sender, EventArgs e)
884        {
885            if (!timer.Enabled)
886            {
887                return;
888            }
889            Refresh();
890            _currentFrame += _direction;
891            if (_currentFrame == -1)
892            {
893                _currentFrame = 0;
894                timer.Enabled = false;
895                _direction = 0;
896                return;
897            }
898            if (_currentFrame == framesCount)
899            {
900                _currentFrame = framesCount - 1;
901                timer.Enabled = false;
902                _direction = 0;
903            }
904        }
905
906        #endregion
907    }
908}