The Windows Forms library initializes GDI+ to do its drawing when it starts. GDI+ spawns a worker thread on which it creates a hidden message-only window ("GDI+ Window"). If this "GDI+ Window" is the first window created on a desktop then the "GDI+ Window" will be set as the foreground window. Since the "GDI+ Window" is created on a separate thread, there is a race condition whether it is created first or the application's main window is created first.
Set the application's window as the foreground window after it has been created by calling the Win32 function SetForegroundWindow() and passing in the window's handle. This can be done in .NET applications though a P/Invoke call.
Applications which explicitly initialize GDI+ may be able to do so after creating their main window.
Example in C#:
public partial class Form1 : Form
static extern bool SetForegroundWindow(IntPtr hWnd);
protected override void OnLoad(EventArgs e)
Article ID: 943453 - Last Review: Dec 3, 2007 - Revision: 1