API

SetForegroundWindow 작동 잘 안될 때 AttachThreadInput 사용

디버그정 2012. 12. 10. 13:25

구글링등 사이트 검색하면 SetForegroundWindowForce라는 프로그램 소스가 보이는데 이것은 좀 잘못된 소스이다.

이 잘못된 소스에서는 AttachThreadInput 첫번째 인수와 두번째 인수를 거꾸로 줬다.

원래의 foreground 윈도우의 tid를 두번째 인수로 줘야 된다.

좀 이상하게 작동해서 msdn에서 함수 설명 읽어보고 첫 인수와 두번째 위치를 바꾸니 제대로 되었다.

 

아래는 올바르게 수정한 소스이다.

void __stdcall SetForegroundWindowForce(HWND hWnd)
{
 HWND hForeground;
 DWORD id, foreground_id;

 hForeground = GetForegroundWindow();
 if (hForeground == hWnd) return;

 foreground_id = GetWindowThreadProcessId(hForeground, NULL);
 id = GetWindowThreadProcessId(hWnd, NULL);
 if (AttachThreadInput(id, foreground_id, TRUE))
 {
  SetForegroundWindow(hWnd);
  BringWindowToTop(hWnd);
  AttachThreadInput(id, foreground_id, FALSE); 
 }
}

 

 

아래는 구글링해 찾은 자료이다. 다양한 방법이 있다. 비베 등 타언어 소스이지만 의미는 이해가리라 믿는다.

{
 Windows 98/2000 doesn"t want to foreground a window when
 some other window has the keyboard focus.
 ForceForegroundWindow is an enhanced SetForeGroundWindow/bringtofront
 function to bring a window to the front.
}

{
 Manchmal funktioniert die SetForeGroundWindow Funktion
 nicht so, wie sie sollte; besonders unter Windows 98/2000,
 wenn ein anderes Fenster den Fokus hat.
 ForceForegroundWindow ist eine "verbesserte" Version von
 der SetForeGroundWindow API-Funktion, um ein Fenster in
 den Vordergrund zu bringen.
}

function ForceForegroundWindow(hwnd: THandle): Boolean;
const
 SPI_GETFOREGROUNDLOCKTIMEOUT = $2000;
 SPI_SETFOREGROUNDLOCKTIMEOUT = $2001;
var
 ForegroundThreadID: DWORD;
 ThisThreadID: DWORD;
 timeout: DWORD;
begin
 if IsIconic(hwnd) then ShowWindow(hwnd, SW_RESTORE);

 if GetForegroundWindow = hwnd then Result := True
 else
 begin
   // Windows 98/2000 doesn"t want to foreground a window when some other
   // window has keyboard focus

   if ((Win32Platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion > 4)) or
     ((Win32Platform = VER_PLATFORM_WIN32_WINDOWS) and
     ((Win32MajorVersion > 4) or ((Win32MajorVersion = 4) and
     (Win32MinorVersion > 0)))) then
   begin
     // Code from Karl E. Peterson, www.mvps.org/vb/sample.htm
     // Converted to Delphi by Ray Lischner
     // Published in The Delphi Magazine 55, page 16

     Result := False;
     ForegroundThreadID := GetWindowThreadProcessID(GetForegroundWindow, nil);
     ThisThreadID := GetWindowThreadPRocessId(hwnd, nil);
     if AttachThreadInput(ThisThreadID, ForegroundThreadID, True) then
     begin
       BringWindowToTop(hwnd); // IE 5.5 related hack
       SetForegroundWindow(hwnd);
       AttachThreadInput(ThisThreadID, ForegroundThreadID, False);
       Result := (GetForegroundWindow = hwnd);
     end;
     if not Result then
     begin
       // Code by Daniel P. Stasinski
       SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, @timeout, 0);
       SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject(0),
         SPIF_SENDCHANGE);
       BringWindowToTop(hwnd); // IE 5.5 related hack
       SetForegroundWindow(hWnd);
       SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject(timeout), SPIF_SENDCHANGE);
     end;
   end
   else
   begin
     BringWindowToTop(hwnd); // IE 5.5 related hack
     SetForegroundWindow(hwnd);
   end;

   Result := (GetForegroundWindow = hwnd);
 end;
end; { ForceForegroundWindow }

// 2. Way:
//**********************************************

procedure ForceForegroundWindow(hwnd: THandle);
 // (W) 2001 Daniel Rolf
 // http://www.finecode.de
 // rolf@finecode.de
var
 hlp: TForm;
begin
 hlp := TForm.Create(nil);
 try
   hlp.BorderStyle := bsNone;
   hlp.SetBounds(0, 0, 1, 1);
   hlp.FormStyle := fsStayOnTop;
   hlp.Show;
   mouse_event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
   mouse_event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
   SetForegroundWindow(hwnd);
 finally
   hlp.Free;
 end;
end;

// 3. Way:
//**********************************************
// by Thomas Stutz

{
 As far as you know the SetForegroundWindow function on Windows 98/2000 can
 not force a window to the foreground while the user is working with another window.
 Instead, SetForegroundWindow will activate the window and call the FlashWindowEx
 function to notify the user. However in some kind of applications it is necessary
 to make another window active and put the thread that created this window into the
 foreground and of course, you can do it using one more undocumented function from
 the USER32.DLL.

 void SwitchToThisWindow (HWND hWnd,  // Handle to the window that should be activated
 BOOL bRestore // Restore the window if it is minimized
);

}

procedure SwitchToThisWindow(h1: hWnd; x: bool); stdcall;
 external user32 Name "SwitchToThisWindow";
        {x = false: Size unchanged, x = true: normal size}

procedure TForm1.Button2Click(Sender: TObject);
begin
 SwitchToThisWindow(FindWindow("notepad", nil), True);
end;