'SetForegroundWindow'에 해당되는 글 1건

  1. 2007.09.13 뒤에 있는 윈도우를 앞으로 보내기

뒤에 있는 윈도우를 앞으로 보내기

개발 2007. 9. 13. 14:05

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

목적 : 윈도우를 Foreground로 보내고 싶을때

보통 윈도우가 어떤 윈도우에 가려져 있는 경우나 또는 다른윈도우의 뒤에 있어서 않보이게 된경우 프로그램적으로 윈도우를 앞으로 나오게 할때 BringWindowToTop(), SetForegroundWindow 를 사용하여 처리를 하면 윈도우가 앞으로 나오게 된다. 그러나 가끔 않나올때가 있는데, 이럴때는 아래처럼 강제로 나오게 하는 방법이다.

void ForceForegroundWindow(HWND hWnd)
{
 if(hWnd == NULL) return;

 // DWORD SPI_GETFOREGROUNDLOCKTIMEOUT = 0x2000;
 // const DWORD SPI_SETFOREGROUNDLOCKTIMEOUT = 0x2001;

 DWORD ForegroundThreadID;
 DWORD ThisThreadID;
 DWORD Timeout;
 BOOL Result;

 if(GetForegroundWindow() == hWnd) {
  return;
 }
 else {
  // Windows 98/2000 doesn't want to foreground a window when some other
  // window has keyboard focus

  OSVERSIONINFO os;
  os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  GetVersionEx(&os);
  if((os.dwPlatformId == VER_PLATFORM_WIN32_NT && os.dwMajorVersion > 4) ||
   (os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS &&
   (os.dwMajorVersion > 4 || (os.dwMajorVersion == 4 && os.dwMinorVersion > 0)))){

    Result = FALSE;
    ForegroundThreadID = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
    ThisThreadID = GetWindowThreadProcessId(hWnd, NULL);
    if(AttachThreadInput(ThisThreadID, ForegroundThreadID, TRUE)) {
     BringWindowToTop(hWnd);
     SetForegroundWindow(hWnd);
     AttachThreadInput(ThisThreadID, ForegroundThreadID, FALSE);
     Result = GetForegroundWindow() == hWnd;
    }

    if(!Result) {
     SystemParametersInfo(0x2000, 0, &Timeout, 0);
     SystemParametersInfo(0x2001, 0, NULL, SPIF_SENDCHANGE);
     BringWindowToTop(hWnd);
     SetForegroundWindow(hWnd);
     SystemParametersInfo(0x2001, 0, &Timeout, SPIF_SENDCHANGE);
    }
   }
  else
  {
   BringWindowToTop(hWnd);
   SetForegroundWindow(hWnd);
  }
 }
}




: