2009. 10. 22. 06:04

2K/XP 에서 포커스를 훔치는 방법

How to steal focus on 2K/XP

I bet that sometimes you long for the old days when a simple SetForegroundWindow brought your dialog into focus. Sigh! Now with 2K/XP things have sorta changed so that if you try a simple SetForegroundWindow you end up flashing the taskbar icon a few times (I never counted but something tells me it flashes thrice). Not exactly what you wanted to do, eh? Luckily there are ways to bring your dialog into the foreground.

The trick is to use AttachThreadInput to attach the thread that owns the current foreground window to our thread, then call SetForegroundWindow and then detach the attached thread, again using AttachThreadInput. Cool, huh?


//Attach foreground window thread
//to our thread
AttachThreadInput(
GetWindowThreadProcessId(
::GetForegroundWindow(),NULL),
GetCurrentThreadId(),TRUE);

//Do our stuff here ;-)
SetForegroundWindow();
SetFocus(); //Just playing safe

//Detach the attached thread
AttachThreadInput(
GetWindowThreadProcessId(
::GetForegroundWindow(),NULL),
GetCurrentThreadId(),FALSE);

From : http://www.voidnish.com/Articles/ShowArticle.aspx?code=dlgboxtricks