Это может быть легко достигнуто путем отображения отдельной формы, выполненной в другом потоке.В этой форме (назовите ее frmSplash) вы можете поместить анимированный GIF или статический текст.В основной форме вам потребуется следующий код:
Объявите некоторые переменные после класса.
public partial class frmMain : Form
{
public Thread th1;
static frmSplash splash;
const int kSplashUpdateInterval_ms = 1;
// Rest of code omitted
Затем добавьте следующий метод в основную форму.Это запустит экран-заставку:
static public void StartSplash()
{
// Instance a splash form given the image names
splash = new frmSplash(kSplashUpdateInterval_ms);
// Run the form
Application.Run(splash);
}
Далее вам нужен метод, чтобы закрыть экран-заставку:
private void CloseSplash()
{
if (splash == null)
return;
// Shut down the splash screen
splash.Invoke(new EventHandler(splash.KillMe));
splash.Dispose();
splash = null;
}
Затем в основной загрузке формы сделайте следующее:
private void frmMain_Load(object sender, EventArgs e)
{
try
{
Thread splashThread = new Thread(new ThreadStart(StartSplash));
splashThread.Start();
// Set the main form invisible so that only the splash form shows
this.Visible = false;
// Perform all long running work here. Loading of grids, checks etc.
BindSalesPerson();
BindCustomer();
BindBrand();
// Set the main form visible again
this.Visible = true;
}
catch (Exception ex)
{
// Do some exception handling here
}
finally
{
// After all is done, close your splash. Put it here, so that if your code throws an exception, the finally will close the splash form
CloseSplash();
}
}
Затем, если основная форма закрыта, убедитесь, что ваш экран-заставка также закрыт:
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
// Make sure the splash screen is closed
CloseSplash();
base.OnClosing(e);
}
Код для формы Splash (в файле frmSplash.cs) следующий:
public partial class frmSplash : Form
{
System.Threading.Timer splashTimer = null;
int curAnimCell = 0;
int numUpdates = 0;
int timerInterval_ms = 0;
public frmSplash(int timerInterval)
{
timerInterval_ms = timerInterval;
InitializeComponent();
}
private void frmSplash_Load(object sender, EventArgs e)
{
this.Text = "";
this.MaximizeBox = false;
this.MinimizeBox = false;
this.ControlBox = false;
this.FormBorderStyle = FormBorderStyle.None;
this.Menu = null;
}
public int GetUpMilliseconds()
{
return numUpdates * timerInterval_ms;
}
public void KillMe(object o, EventArgs e)
{
//splashTimer.Dispose();
this.Close();
}
}
Надеюсь, это поможет вам.Возможно, это не самый лучший код, когда-либо написанный, но он работал для меня.