Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

SetForegroundWindow (user32)
 
.
Summary

C# Signature:

// For Windows Mobile, replace user32.dll with coredll.dll
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);

VB Signature:

Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As IntPtr) As Long

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

This is especially useful for test automation to make sure the Application Under Test (AUT) retains focus before manipulating it.

This may be called several times in an automated test script, so it is best to create a method similar to the example below.

Note this tip from the Microsoft documentation:

If the window being set to the foreground is minimized, the window may not be visible to the end user. This function also does not reactivate the last active child window by default. To ensure that the window is restored and the last active child window is reactivated, combine the handle to the window with 0x01 using the logical OR operator

E.g. SetForegroundWindow((HWND)(((ULONG) hwnd) | 0x01) );

NOTE: This tip only applies to Windows Mobile and Windows Embedded CE!

Sample Code (C#):

public static void Main(string[] args)
{
    // test code here...
    CheckAutFocus(myProcess.MainWindowHandle);
    // more test code...
}

public static void CheckAutFocus(hWnd)
{
    if (GetForegroundWindow() != hWnd)
    {
        SetForegroundWindow(hWnd);
    }
}

Sample Code (C#):

        public static bool BringWindowToTop(string windowName, bool wait)
        {
            int hWnd = FindWindow(windowName, wait);
            if (hWnd != 0)
            {
                return SetForegroundWindow((IntPtr)hWnd);
            }
            return false;
        }

        // THE FOLLOWING METHOD REFERENCES THE FindWindowAPI
        public static int FindWindow(string windowName, bool wait)
        {
            int hWnd = FindWindow(null, windowName);
            while (wait && hWnd == 0)
            {
                System.Threading.Thread.Sleep(500);
                hWnd = FindWindow(null, windowName);
            }

            return hWnd;
        }

Alternative Managed API:

The ManagedWindowsApi project (http://mwinapi.sourceforge.net) provides a static property ManagedWinapi.SystemWindow.ForegroundWindow which can be set.

Documentation

Here is an alternative Managed API to FindWindow, The article also describes a way to CloseWindow of another process like notepad, not sure if there is such thing in Win32 API, but at least you can do it in .NET! Here is the article:

http://www.mycsharpcorner.com/Post.aspx?postID=32

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions