C# 欢迎在c上加载#
我正在用C#制作一个程序,它快结束了,我在想,在5秒钟前加载一个欢迎表单,显示程序名称和其他内容会很有趣。。。就像微软word中的这个C# 欢迎在c上加载#,c#,forms,C#,Forms,我正在用C#制作一个程序,它快结束了,我在想,在5秒钟前加载一个欢迎表单,显示程序名称和其他内容会很有趣。。。就像微软word中的这个 但我不知道该怎么做。请给我一些建议……如果您正在使用WPF,您可以使用该对象 在WinForms中,这有点困难,但您可以使用开始 请记住,启动屏幕是指当应用程序需要一段时间才能加载,以帮助用户感觉有什么事情正在发生时的情况。如果只是将应用程序加载延迟5秒,实际上是在降低用户的体验。五秒钟是很长的时间…好吧,你创建一个表单,将其边框设置为无,放置一个图像,win
但我不知道该怎么做。请给我一些建议……如果您正在使用WPF,您可以使用该对象 在WinForms中,这有点困难,但您可以使用开始
请记住,启动屏幕是指当应用程序需要一段时间才能加载,以帮助用户感觉有什么事情正在发生时的情况。如果只是将应用程序加载延迟5秒,实际上是在降低用户的体验。五秒钟是很长的时间…好吧,你创建一个表单,将其边框设置为无,放置一个图像,winforms定时器设置为在打开主表单的5秒钟后启动,然后你就可以开始了
然而,更复杂的启动屏幕(WinForms)需要GDI+、剪辑等,但我想这可以做到…我使用“System.Theading”来实现,这对我来说非常有效。下面的代码在应用程序(在下面的示例中称为MainForm())加载或初始化时在单独的线程上启动“启动屏幕”。首先,在“main()”方法中(在program.cs文件中,除非已重命名),您应该显示初始屏幕。这将是您希望在启动时向用户显示的WinForm或WPF表单。这是从main()启动的,如下所示:
[STAThread]
static void Main()
{
// Splash screen, which is terminated in MainForm.
SplashForm.ShowSplash();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Run UserCost.
Application.Run(new MainForm());
}
在SplashScreen代码中,您需要以下内容:
public partial class SplashForm : Form
{
// Thredding.
private static Thread _splashThread;
private static SplashForm _splashForm;
public SplashForm()
{
InitializeComponent();
}
// Show the Splash Screen (Loading...)
public static void ShowSplash()
{
if (_splashThread == null)
{
// show the form in a new thread
_splashThread = new Thread(new ThreadStart(DoShowSplash));
_splashThread.IsBackground = true;
_splashThread.Start();
}
}
// Called by the thread
private static void DoShowSplash()
{
if (_splashForm == null)
_splashForm = new SplashForm();
// create a new message pump on this thread (started from ShowSplash)
Application.Run(_splashForm);
}
// Close the splash (Loading...) screen
public static void CloseSplash()
{
// Need to call on the thread that launched this splash
if (_splashForm.InvokeRequired)
_splashForm.Invoke(new MethodInvoker(CloseSplash));
else
Application.ExitThread();
}
}
这将在单独的背景线程上启动启动启动窗体,允许您同时继续呈现主应用程序。要在应用程序初始化后结束并关闭启动屏幕,请将以下内容放置在默认构造函数中(如果需要,可以重载构造函数):
这些都是不言自明的,您应该能够自己确定代码的确切工作方式。我希望它能帮助您。除非加载应用程序需要很长时间,否则不要这样做。Microsoft使用此选项是因为Office可能需要一段时间才能加载。您可能会感兴趣,但您的用户会感到厌烦。在WinForms中不是这样;在C#中。在VB.NET中有一个属性。是的,在VB.NET中,在ApplicationServices命名空间中有许多这样的方便类。非常感谢!!!我想你说得对,用户体验降低了,但我还是想试试,只是为了学习,你的例子很完美。。。thanksNote:此示例使用WinForms,由于此代码的“线程性质”,它不应该太慢应用程序的初始化顺序,尤其是在使用多核机器的情况下。话虽如此,上面的评论站启动屏幕可以给人一种专业的感觉,但如果你的应用程序渲染很快,这可能是没有必要的。
public MainForm()
{
// ready to go, now initialise main and close the splashForm.
InitializeComponent();
SplashForm.CloseSplash();
}