header image
 

Loading animation in a winform C#

A simple task – get a “loading” animation when you click a button on a winform

My solution (works!)

1) in your form designer, you need a panel!!!! this is what will be used to draw the loading bitmap

2) in your form constructor you need something like this

Snippet

<title>Snippet</title><pre style="font-family: consolas;">        <span style="color: blue;">public</span> TestForm()
        {
            InitializeComponent();
            <span style="color: green;">//Enable double buffering to reduce flicker.</span>
            <span style="color: blue;">this</span>.SetStyle(<span style="color: rgb(43, 145, 175);">ControlStyles</span>.OptimizedDoubleBuffer, <span style="color: blue;">true</span>);
            <span style="color: green;">//Enable paint via message to reduce flicker. </span>
            <span style="color: blue;">this</span>.SetStyle(<span style="color: rgb(43, 145, 175);">ControlStyles</span>.AllPaintingInWmPaint, <span style="color: blue;">true</span>);
        }

3) In your button click event you need something like this

Snippet

        <span style="color: blue;">private</span> <span style="color: blue;">void</span> btnClickMe_Click(<span style="color: blue;">object</span> sender, <span style="color: rgb(43, 145, 175);">EventArgs</span> e)<br />        {<br />            InitAnimation();<br />            <span style="color: rgb(43, 145, 175);">Thread</span> backgroundThread = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Thread</span>(<span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">ThreadStart</span>(AnimateImage));<br />            backgroundThread.Start();<br /> <br />            //Do something grand here!<br /> <br />            backgroundThread.Abort();<br />            ClearAnimation();<br />        }<br /><br />4) helper stuff, that does the animation work ...<br /><br /><title>Snippet</title><pre style="font-family: consolas;"><span style="color: blue;">        #region</span> Animation<br /> <br />        <span style="color: blue;">void</span> InitAnimation()<br />        {<br />            panel1.Visible = <span style="color: blue;">true</span>; // this is your panel added in step 1<br />        }<br /> <br /> <br />        <span style="color: blue;">void</span> AnimateImage()<br />        {<br />            <span style="color: rgb(43, 145, 175);">Stream</span> strImage = <span style="color: blue;">this</span>.GetType().Assembly.GetManifestResourceStream(<span style="color: rgb(163, 21, 21);">"WindowsApplication1.Images.loadingAnimation.gif"</span>); // its up to u to figure out the resource name, <br />                                 //else use an absolute file path in the new Bitmap constructor, see line below<br />            <span style="color: rgb(43, 145, 175);">Bitmap</span> animatedImage = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Bitmap</span>(strImage); <span style="color: green;">//new Bitmap(@"C:\Images\loadingAnimation.gif");</span><br /><br />            <span style="color: rgb(43, 145, 175);">Graphics</span> gfx = panel1.CreateGraphics();<br />            gfx.DrawImage(animatedImage, <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Point</span>(0, 0));<br /> <br />            <span style="color: rgb(43, 145, 175);">ImageAnimator</span>.Animate(animatedImage, <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">EventHandler</span>(<span style="color: blue;">this</span>.OnFrameChanged));<br /> <br />            <span style="color: blue;">while</span> (<span style="color: blue;">true</span>)<br />            {<br />                <span style="color: rgb(43, 145, 175);">ImageAnimator</span>.UpdateFrames();<br />                gfx.DrawImage(animatedImage, <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Point</span>(0, 0));<br />            }<br />        }<br /> <br />        <span style="color: blue;">private</span> <span style="color: blue;">void</span> OnFrameChanged(<span style="color: blue;">object</span> o, <span style="color: rgb(43, 145, 175);">EventArgs</span> e)<br />        {<br />            <span style="color: green;">//Force a call to the Paint event handler.</span><br />            <span style="color: blue;">this</span>.Invalidate();<br />        }<br /> <br />        <span style="color: blue;">void</span> ClearAnimation()<br />        {<br />            panel1.Visible = <span style="color: blue;">false</span>;<br />        }<br /> <br /><span style="color: blue;">        #endregion</span>

~ by shoel on July 8, 2010 .



Leave a Reply