Display Splash Screen for VSTO Applications

While developing an Office Add In using VSTO, we found out when some of the operations takes up very long time – it would be helpful to the end-user if we could display a Splash screen (with a progress bar / wait message), so that the user does not think the application has hung.

The following example from MSDN Forum shows how to implement a Splash Screen for VSTO Applications. The approach is quite simple – display a modal form on a background thread.

public delegate
void InvokeClose();

private void
ThisAddIn_Startup(object sender, System.EventArgs e)

{

      // MLSplashScreen is the custome splash screen, inherited
from System.Windows.Forms.Form

      private MLSplashScreen
splashScreen = new MLSplashScreen(); 

     

      IntPtr
hwndWin = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;

      NativeWindow parent = new NativeWindow();

      parent.AssignHandle(hwndWin);

      try

      {            

            System.Threading.Thread t = new
System.Threading.Thread(SplashScreenProc);

            t.Start(parent);

            // Do the Long Operation, here

 

            InvokeClose
invokeClose = new InvokeClose(splashScreen.Close);

            splashScreen.Invoke(invokeClose);

      }

      catch (Exception
ex)

      {

      }

      finally

      {

            parent.ReleaseHandle();

      }

}

// Display the splash screen.

private void
SplashScreenProc(object param)

        {

            IWin32Window parent = (IWin32Window)param;

           
splashScreen.ShowDialog(parent);

        }

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: