Windows API指令SetForegroundWindow討論
這是最近在寫一個程式,用到Windows AP指令的SetForegroundWindow
這指令的功能是將視窗推到最上層,在以前Windows XP的系統可以正常運作
但拿到Windows 7後竟然無法使用
後來藉由網路搜尋才知道原來很多人都有這個困擾,最後終於再這個網站找到解法
https://www.codeproject.com/Tips/764...oregroundWindo
PHP 語法:
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); } }
這是網站提供的方法,可惜小弟所知有限,無法完整使用,只好修改成自己所知的方式,這是需要放在檔頭檔(.H)
PHP 語法:
void __fastcall SetForegroundWindowInternal(HWND hWnd);
PHP 語法:
void __fastcall TForm1::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); } } //---------------------------------------------------------------------------
PHP 語法:
void __fastcall TForm1::Timer1Timer(TObject *Sender) { if(::GetForegroundWindow()!=Handle) SetForegroundWindowInternal(Handle); } //---------------------------------------------------------------------------
這是小弟修改過的,這個經驗分享給大家
|