Click here to Skip to main content
12,511,734 members (44,337 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

85.8K views
30 bookmarked
Posted

How to bring window to top with SetForegroundWindow()

, 27 Apr 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Introduction...

Introduction

This can help you to bypass limitation of using ::SetForegroundWindow() function.

Before code some from MSDN:

The system restricts which processes can set the foreground window. A process can set the foreground window only if one of the following conditions is true:

• The process is the foreground process.

• The process was started by the foreground process.

• The process received the last input event.

• There is no foreground process.

• The foreground process is being debugged.

• The foreground is not locked (see LockSetForegroundWindow).

• The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).

• No menus are active.


An application cannot force a window to the foreground while the user is working with another window. Instead, Foreground and Background Windows will activate the window (see SetActiveWindow) and call the function to notify the user.


A process that can set the foreground window can enable another process to set the foreground window by calling the AllowSetForegroundWindow function. The process specified by dwProcessId loses the ability to set the foreground window the next time the user generates input, unless the input is directed at that process, or the next time a process calls AllowSetForegroundWindow, unless that process is specified.


The foreground process can disable calls to SetForegroundWindow by calling the LockSetForegroundWindow function.


The system automatically enables calls to SetForegroundWindow if the user presses the ALT key or takes some action that causes the system itself to change the foreground window (for example, clicking a background window).


This function is provided so applications can prevent other applications from making a foreground change that can interrupt its interaction with the user.

So, there are 2 ways:

First is works like Alt+TAB window switching in Windows

void SetForegroundWindowInternal(HWND hWnd)
{
	if(!::IsWindow(hWnd)) return;
 
	BYTE keyState[256] = {0};
	//to unlock SetForegroundWindow we need to imitate Alt pressing
	if(::GetKeyboardState((LPBYTE)&keyState))
	{
		if(!(keyState[VK_MENU] & 0x80))
		{
			::keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
		}
	}
 
	::SetForegroundWindow(hWnd);
 
	if(::GetKeyboardState((LPBYTE)&keyState))
	{
		if(!(keyState[VK_MENU] & 0x80))
		{
			::keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
		}
	}
}

And second. In this version we are attaches the input processing mechanism of our thread to that of active thread.

void SetForegroundWindowInternal(HWND hWnd)
{
	if(!::IsWindow(hWnd)) return;
 
	//relation time of SetForegroundWindow lock
	DWORD lockTimeOut = 0;
	HWND  hCurrWnd = ::GetForegroundWindow();
	DWORD dwThisTID = ::GetCurrentThreadId(),
	      dwCurrTID = ::GetWindowThreadProcessId(hCurrWnd,0);
 
	//we need to bypass some limitations from Microsoft :)
	if(dwThisTID != dwCurrTID)
	{
		::AttachThreadInput(dwThisTID, dwCurrTID, TRUE);
 
		::SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT,0,&lockTimeOut,0);
		::SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT,0,0,SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
 
		::AllowSetForegroundWindow(ASFW_ANY);
	}
 
	::SetForegroundWindow(hWnd);
 
	if(dwThisTID != dwCurrTID)
	{
		::SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT,0,(PVOID)lockTimeOut,SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
		::AttachThreadInput(dwThisTID, dwCurrTID, FALSE);
	}
}

Have a nice time of day :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Siarhei Boika
Engineer
Belarus Belarus
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
QuestionThank you + bug fix Pin
Axonn Echysttas10-Aug-16 9:05
memberAxonn Echysttas10-Aug-16 9:05 
PraiseVery nice solution Pin
sachin rakate4-Nov-15 20:03
membersachin rakate4-Nov-15 20:03 
GeneralMy vote of 5 Pin
gndnet7-Aug-12 22:23
membergndnet7-Aug-12 22:23 
QuestionNeed to use GetAsyncKeyState() Pin
varkha25-May-12 12:03
membervarkha25-May-12 12:03 
This did not quite work for me. I had changed the code to this:
void SetForegroundWindowInternal(HWND hWnd)
{
    if(!hWnd || !::IsWindow(hWnd)) return;
 
    //to unlock SetForegroundWindow we need to imitate pressing [Alt] key
    bool bPressed = false;
    if((::GetAsyncKeyState(VK_MENU) & 0x8000) == 0)
    {
        bPressed = true;
        ::keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
    }
 
    ::SetForegroundWindow(hWnd);
 
    if(bPressed)
    {
        ::keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
    }
}

Without the change the system (the first version) was getting stuck as if with [Alt] key pressed. Pressing [Esc] with no Alt in this state would switch windows, etc. The only way to get out of this state would be to press [Alt] again couple times. The problem could be that we invoke the function when Alt+Ctrl+F10 is pressed. It seemed to work when Alt was not pressed but fails with Alt already pressed.

thanks again for the direction. This little quirk above was simple to get through.
Valery

General>>The system automatically enables calls to SetForegroundWin... Pin
Naveen13-Dec-10 19:54
memberNaveen13-Dec-10 19:54 
GeneralThanks for sharing, very helpful. but, if the window is mini... Pin
qiuchengw2-Dec-10 15:29
memberqiuchengw2-Dec-10 15:29 
GeneralRe: Thanks for sharing, very helpful.but, if the window is mini... Pin
Aster Veigas7-Dec-13 10:13
memberAster Veigas7-Dec-13 10:13 
GeneralWorks on Win Server 2008 R2 Pin
Chris at IAGAM13-Dec-10 11:48
memberChris at IAGAM13-Dec-10 11:48 
AnswerRe: Works on Win Server 2008 R2 Pin
SHShadow4-Apr-11 21:06
memberSHShadow4-Apr-11 21:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160929.1 | Last Updated 28 Apr 2010
Article Copyright 2010 by Siarhei Boika
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid