Microsoft KB Archive/943453

= A 'GDI+ Window' may appear when launching an application on an empty desktop or start a screensaver =

Article ID: 943453

Article Last Modified on 12/3/2007

-

APPLIES TO


 * Microsoft .NET Framework 3.0
 * Microsoft .NET Framework 3.5
 * Microsoft .NET Framework 2.0

-



Source: Microsoft Support



RAPID PUBLISHING
RAPID PUBLISHING ARTICLES PROVIDE INFORMATION DIRECTLY FROM WITHIN THE MICROSOFT SUPPORT ORGANIZATION. THE INFORMATION CONTAINED HEREIN IS CREATED IN RESPONSE TO EMERGING OR UNIQUE TOPICS, OR IS INTENDED SUPPLEMENT OTHER KNOWLEDGE BASE INFORMATION.



Action
A Windows Forms application is run on a desktop with no existing windows (for example, a screen saver).



Result
A &quot;GDI+ Window&quot; appears and is set as the foreground window. Keyboard input does not go to the application's window.



Cause
The Windows Forms library initializes GDI+ to do its drawing when it starts. G|DI+ spawns a worker thread on which it creates a hidden message-only |window (&quot;|GDI+ |Window&quot;). If this &quot;GDI+ Window&quot; is the first window created on a desktop then the &quot;GDI+ Window&quot; will be set as the foreground window. Since the &quot;GDI+ Window&quot; 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.



Resolution
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.



MoreInformation
Example in C#:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace MyApplication

{

public partial class Form1 : Form

{

public Form1

{

InitializeComponent;

}

[DllImport(&quot;user32.dll&quot;)]

[return: MarshalAs(UnmanagedType.Bool)]

static extern bool SetForegroundWindow(IntPtr hWnd);

protected override void OnLoad(EventArgs e)

{

SetForegroundWindow(this.Handle);

base.OnLoad(e);

}

}

}



DISCLAIMER
MICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED &quot;AS IS&quot; WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES.

Keywords: kbnomt kbrapidpub KB943453

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.