查看單個文章
舊 2018-02-09, 07:59 PM   #1
魔術王子
版區管理員
 
魔術王子 的頭像
榮譽勳章
UID - 115097
在線等級: 級別:42 | 在線時長:1985小時 | 升級還需:36小時級別:42 | 在線時長:1985小時 | 升級還需:36小時
註冊日期: 2004-01-13
住址: 魔術學園
文章: 2945
精華: 0
現金: 14765 金幣
資產: 2678395 金幣
預設 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(dwThisTIDdwCurrTIDTRUE);

        ::
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(dwThisTIDdwCurrTIDFALSE);
    }

這是網站提供的方法,可惜小弟所知有限,無法完整使用,只好修改成自己所知的方式,這是需要放在檔頭檔(.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(dwThisTIDdwCurrTIDTRUE);

        ::
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(dwThisTIDdwCurrTIDFALSE);
    }
}
//--------------------------------------------------------------------------- 
PHP 語法:
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
  if(::
GetForegroundWindow()!=HandleSetForegroundWindowInternal(Handle);
}
//--------------------------------------------------------------------------- 
這是小弟修改過的,這個經驗分享給大家
__________________
『唸金母心咒:嗡。金母。悉地。吽。』
持此咒者,可免一切瘟疫,一切瘟神抱頭四散。

魔術就是欣賞神奇的效果
如果魔術的秘密被破解了
那魔術就失去欣賞的價值
魔術王子 目前離線  
送花文章: 1523, 收花文章: 1553 篇, 收花: 3892 次
回覆時引用此帖