Example #1
0
        protected override void OnPaint(PaintEventArgs pevent)
        {
            var g = pevent.Graphics;

            g.TextRenderingHint = TextRenderingHint.AntiAlias;

            g.Clear(Parent.BackColor);

            //Hover
            Color c = Shade != Shades.None
                ? MaterialSkinManager.GetMaterialColor(Shade)
                : SkinManager.GetFlatButtonHoverBackgroundColor();

            using (Brush b = Shade != Shades.None
                ? new SolidBrush(c)
                : new SolidBrush(Color.FromArgb((int)(_hoverAnimationManager.GetProgress() * c.A), c.RemoveAlpha())))
                // g.FillRectangle(b, ClientRectangle);

                //Ripple
                if (_animationManager.IsAnimating())
                {
                    g.SmoothingMode = SmoothingMode.AntiAlias;
                    for (var i = 0; i < _animationManager.GetAnimationCount(); i++)
                    {
                        var animationValue  = _animationManager.GetProgress(i);
                        var animationSource = _animationManager.GetSource(i);

                        using (Brush rippleBrush = new SolidBrush(Color.FromArgb((int)(101 - (animationValue * 100)), Color.Black)))
                        {
                            var rippleSize = (int)(animationValue * Width * 2);
                            g.FillEllipse(rippleBrush, new Rectangle(animationSource.X - rippleSize / 2, animationSource.Y - rippleSize / 2, rippleSize, rippleSize));
                        }
                    }
                    g.SmoothingMode = SmoothingMode.None;
                }


            //Icon

            if (Icon != null)
            {
                var iconRect = new Rectangle(8, (Height / 2) - Icon.Height / 2, Icon.Width, Icon.Height);

                //create a color matrix object  & set the opacity
                var matrix = new ColorMatrix {
                    Matrix33 = Enabled ? (float)0.75 : (float)0.30
                };

                //set the color(opacity) of the image
                var attributes = new ImageAttributes();
                attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

                // Draw the image
                g.DrawImage(Icon, iconRect, 0, 0, Icon.Width, Icon.Height, GraphicsUnit.Pixel, attributes);
            }

            //Text
            var textRect = ClientRectangle;

            if (Icon != null)
            {
                //
                // Resize and move Text container
                //

                // First 8: left padding
                // 24: icon width
                // Second 4: space between Icon and Text
                // Third 8: right padding
                textRect.Width -= 8 + 24 + 4 + 8;

                // First 8: left padding
                // 24: icon width
                // Second 4: space between Icon and Text
                textRect.X += 8 + 24 + 4;
            }

            var fontColor = Enabled
                ? (Primary ? SkinManager.ColorScheme.PrimaryBrush : SkinManager.GetPrimaryTextBrush())
                : SkinManager.GetFlatButtonDisabledTextBrush();

            if (Shade != Shades.None)
            {
                fontColor = MaterialSkinManager.GetMaterialBrush(Shade);
            }


            g.DrawString(
                Text.ToUpper(),
                SkinManager.ROBOTO_MEDIUM_10,
                fontColor,
                textRect,
                new StringFormat {
                Alignment = MaterialRaisedButton.ContentToTextHAlignment(TextAlign), LineAlignment = MaterialRaisedButton.ContentToTextVAlignment(TextAlign)
            });
        }