Reputation: 4414
I have a SplashScreen that should be shown in front of all other windows in the application.
Since it is a SplashScreen
, this cannot be modal dialog. Instead, this should be shown by mean of other thread.
I create the splash screen this way:
SplashScreenForm = new SplashScreen(mainForm);
// SplashScreenForm.TopMost = true;
And to show it, I am using this call, called from a different thread:
Application.Run(SplashScreenForm);
If I uncomment SplashScreenForm.TopMost = true
, the splash is shown on top of other windows, even on top of windows belonging to different applications.
If you want to know how the thread is created:
public void ShowSplashScreen()
{
SplashScreenThread = new Thread(new ThreadStart(ShowForm));
SplashScreenThread.IsBackground = true;
SplashScreenThread.Name = "SplashScreenThread";
SplashScreenThread.Start();
}
private static void ShowForm()
{
Application.Run(SplashScreenForm);
}
How can I do it?
Upvotes: 2
Views: 390
Reputation: 7409
Something like:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Thread splashThread = new Thread(new ThreadStart(
delegate
{
splashForm = new SplashForm();
Application.Run(splashForm);
}
));
splashThread.SetApartmentState(ApartmentState.STA);
splashThread.Start();
// Load main form and do lengthy operations
MainForm mainForm = new MainForm();
mainForm.Load += new EventHandler(mainForm_Load);
Application.Run(mainForm);
}
Then later after time-consuming operations are ended:
static void mainForm_Load(object sender, EventArgs e)
{
if (splashForm == null)
return;
splashForm.Invoke(new Action(splashForm.Close));
splashForm.Dispose();
splashForm = null;
}
This will start your splash screen before your main form and only dismiss it when the lengthy operations are completed in mainForm_Load
.
Upvotes: 1
Reputation: 5930
You can try calling function SetForegroundWindow(HWND)
from WinAPI
:
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
// somewhere in the code :
SetForegroundWindow(SplashScreenForm.Handle);
Upvotes: 0