[英]How can I animate a progress indicator in a splash screen of a windows application
I have created a splash screen in my windows application. 我在Windows应用程序中创建了一个初始屏幕。 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. 我希望在初始屏幕上添加一个循环的“响铃”进度指示器(如Windows 8启动屏幕上显示的进度指示器),直到连接主窗体为止。 The code in program.cs is: program.cs中的代码是:
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. logo
是我要向Windows 8启动中添加进度条或响铃进度指示器的启动表单或启动屏幕。
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: 添加一个标准的水平ProgressBar
,并将其样式设置为Marquee
这将给您不确定的“正在发生的进展,但我们不确定何时会完成”外观:
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. 如果需要环形/圆形进度指示器,则最好使用动画的.gif或类似动画以及ImageAnimator
控件。
There is a good example of loading a gif and stepping through the frames on MSDN on the documentation for the ImageAnimator.Animate
method: 在ImageAnimator.Animate
方法的文档上有一个很好的示例,它加载gif并逐步浏览MSDN上的框架:
Create a control, such as "AnimatedProgress": 创建一个控件,例如“ 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: 将此控件添加到logo
表单中:
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.) (我发现通过代码添加它比使用设计器更好,因为我刚刚在AnimatedProgress控件中粗略地引用了该图像,而VS设计器找不到该图像。)
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: 就显示初始屏幕道具的“最简单”方式而言, 必须向Veldmius指出SpalshScreen属性:
Start by adding a reference to Microsoft.VisualBasic.dll
to your project, then update your program.cs to something like: 首先在项目中添加对Microsoft.VisualBasic.dll
的引用,然后将program.cs更新为以下内容:
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. 为了测试这一点,我在主窗体的加载事件中添加了一个Thread.Sleep(5000)
,就可以了-我的徽标页面显示动画进度5秒钟,然后加载主窗体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.