Reputation: 41
I have created a splash screen in my windows application. I want a cycling "ring" progress indicator (like the one shown on the Windows 8 boot screen) to be added on splash screen until the main form is connected. The code in program.cs is:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
logo f = new logo();
f.Shown += new EventHandler((o, e) =>
{
System.Threading.Thread t = new System.Threading.Thread(() =>
{
System.Threading.Thread.Sleep(4000);
f.Invoke(new Action(() => { f.Close(); }));
});
t.IsBackground = true;
t.Start();
});
}
logo
is the start up form or splash screen I want to add progress bar or ring progress indicator to as in windows 8 startup.
Upvotes: 1
Views: 3125
Reputation: 26956
There isn't a specific "cycle" progress ring control within the default control set, so I'd say you have two options:
Add a standard horizontal ProgressBar
, and set its style to Marquee
- this will give you the indeterminate "progress is happening but we're not sure when it's going to finish" look:
myProgressBar.Style = ProgressBarStyle.Marquee;
If you want a ring/circular progress indicator, then you're better off using an animated .gif or similar and the ImageAnimator
control.
There is a good example of loading a gif and stepping through the frames on MSDN on the documentation for the ImageAnimator.Animate
method:
Create a control, such as "AnimatedProgress":
public partial class AnimatedProgress : UserControl
{
//Create a Bitmpap Object.
Bitmap animatedImage = new Bitmap("circle_progress_animation.gif");
bool currentlyAnimating = false;
//This method begins the animation.
public void AnimateImage()
{
if (!currentlyAnimating)
{
//Begin the animation only once.
ImageAnimator.Animate(animatedImage, new EventHandler(this.OnFrameChanged));
currentlyAnimating = true;
}
}
private void OnFrameChanged(object o, EventArgs e)
{
//Force a call to the Paint event handler.
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
//Begin the animation.
AnimateImage();
//Get the next frame ready for rendering.
ImageAnimator.UpdateFrames();
//Draw the next frame in the animation.
e.Graphics.DrawImage(this.animatedImage, new Point(0, 0));
}
}
Add this control to your logo
form:
public Logo()
{
InitializeComponent();
var progressSwirl = new AnimatedProgress();
progressSwirl.Location = new Point(50, 50);
Controls.Add(progressSwirl);
}
(I found adding it via code worked better than using the designer as I'd just referenced the image fairly crudely in my AnimatedProgress control and the VS designer couldn't find the image.)
Your cycling ring will then appear on your splash screen.
In terms of the "simplest" way to show the splash screen props must go to Veldmius for pointing out the SpalshScreen property:
Start by adding a reference to Microsoft.VisualBasic.dll
to your project, then update your program.cs to something like:
using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;
namespace WindowsFormsApplication1
{
public class Startup : WindowsFormsApplicationBase
{
protected override void OnCreateSplashScreen()
{
SplashScreen = new logo();
}
protected override void OnCreateMainForm()
{
MainForm = new Form1();
}
}
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
new Startup().Run(new string[]{});
}
}
}
To test this, I added a Thread.Sleep(5000)
to the loading event of my main form, and there you have it - my logo page displayed with an animated progress for 5 seconds then my main form loaded in.
Upvotes: 2