Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i have a button.

but the button is not animated.

I want animate it like Guna.UI2 or Bunifu.

The source code is :
C#
[ToolboxBitmap(typeof(Button))]
public class SButton : Button
{
    int bra = 20;
    StringAlignment ha = StringAlignment.Center;
    StringAlignment va = StringAlignment.Center;
    public StringAlignment HorizontalAlignment
    {
        get { return ha; }
        set { ha = value; Invalidate(); }
    }
    public StringAlignment VerticalAlignment
    {
        get { return va; }
        set { va = value; Invalidate(); }
    }
    public int BorderRadius
    {
        get { return bra; }
        set { bra = value; this.Invalidate(); }
    }
    public SButton() { BackColor = Color.White; ForeColor = Color.Black; Font = new Font("Segoe UI", 13, FontStyle.Bold); }
    static int GetFontWidth(int textlength, Font font)
    {
        return textlength * (int)font.Size;
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        // Draw default button
        base.OnPaint(e);
        // High quality smoothing mode
        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        //Clear the Windows button graphics
        e.Graphics.Clear(Parent.BackColor);
        if (bra < 2)
        {
            SolidBrush s = new SolidBrush(BackColor);
            e.Graphics.FillRectangle(s, ClientRectangle);
        }
        else
        {
            GraphicsPath path = new GraphicsPath();
            path.AddArc(new Rectangle(0, 0, bra, bra), 180, 90);
            path.AddArc(new Rectangle(Width - bra, 0, bra, bra), -90, 90);
            path.AddArc(new Rectangle(Width - bra, Height - bra, bra, bra), 0, 90);
            path.AddArc(new Rectangle(0, Height - bra, bra, bra), 90, 90);
            e.Graphics.FillPath(new SolidBrush(BackColor), path);
        }
        // Draw text with his font
        e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor),ClientRectangle, new StringFormat() { LineAlignment = ha, Alignment = va } );
        // If Image property is not null, draw image with his align
        if (Image != null)
        {
            if (ImageAlign == ContentAlignment.TopLeft)
            {
                e.Graphics.DrawImage(Image, new Point(16, 16));
            }else if (ImageAlign == ContentAlignment.TopCenter)
            {
                e.Graphics.DrawImage(Image, new Point(this.Width / 2 - Image.Width / 2, 16));
            }else if (ImageAlign == ContentAlignment.TopRight)
            {
                e.Graphics.DrawImage(Image, new Point(this.Width - Image.Width - 16, 16));
            }else if (ImageAlign == ContentAlignment.MiddleLeft)
            {
                e.Graphics.DrawImage(Image, new Point(16, this.Height / 2 - Image.Height / 2));
            }else if (ImageAlign == ContentAlignment.MiddleCenter)
            {
                e.Graphics.DrawImage(Image, new Point(this.Width / 2 - Image.Width / 2, this.Height / 2 - Image.Height / 2));
            }else if (ImageAlign == ContentAlignment.MiddleRight)
            {
                e.Graphics.DrawImage(Image, new Point(this.Width - Image.Width - 16, this.Height / 2 - Image.Height / 2));
            }else if (ImageAlign == ContentAlignment.BottomLeft)
            {
                e.Graphics.DrawImage(Image, new Point(16, this.Height - Image.Height - 16));
            }
            else if (ImageAlign == ContentAlignment.BottomCenter)
            {
                e.Graphics.DrawImage(Image, new Point(this.Width / 2 - Image.Width / 2, this.Height - Image.Height - 16));
            }
            else if (ImageAlign == ContentAlignment.BottomRight)
            {
                e.Graphics.DrawImage(Image, new Point(this.Width - Image.Width - 16, this.Height - Image.Height - 16));
            }
        }
    }
}


What I have tried:

Search the Internet for animated buttons.
Posted
Updated 7-Jul-22 13:00pm
Comments
PIEBALDconsult 7-Jul-22 19:16pm    
Is that what your users want?
I absolutely positively do not want anything "animated" in the tools I use.
[no name] 7-Jul-22 21:09pm    
Use an image control with a click handler. Cycle some images for animation.

1 solution

First, WinForms is not good at this.

Next, you're going to have to use a Timer to kick off each frame of your animation, and you'll have to implement your own "storyboard" to track how much you have to change based on the amount of time that has elapsed since the last frame of your button was drawn. NO, THE TIMER INTERVAL IS NOT GOOD AT THAT!

The other thing is, how do you want to "animate" the button? What code you write will be dictated by that requirement.

This would be far easier to do in WPF. The downside to WPF is it has a very steep learning curve.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900