史萊姆論壇

史萊姆論壇 (http://forum.slime.com.tw/)
-   程式語言討論區 (http://forum.slime.com.tw/f76.html)
-   -   Windows API指令SetForegroundWindow討論 (http://forum.slime.com.tw/thread288151.html)

魔術王子 2018-02-09 07:59 PM

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);
}
//--------------------------------------------------------------------------- 

這是小弟修改過的,這個經驗分享給大家

mini 2018-02-10 04:54 PM

不怎麼寫C++所以看不太懂
個人常用的方式是使用 WinAPI 裡的 SetWindowPos

SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, IgnoreResize Or IgnoreMove)
SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, ShowWindow Or IgnoreResize Or IgnoreMove)
這是 非.Net視窗(外部窗體) 的方法
如視窗是 .Net元件只要將屬性 .TopMost = True 再 .TopMost = False就可以了

.NET的宣告如下
PHP 語法:

    Public Const HWND_TOP 0
    
Public Const HWND_NOTOPMOST = -2
    
Public Const HWND_TOPMOST = -1     '將窗口置於列表頂部,並位於任何最頂部窗口的前面

    <Flags> Public Enum SetWindowPosFlags As UInteger
        SynchronousWindowPosition = &H4000
        DeferErase = &H2000
        DrawFrame = &H20
        FrameChanged = &H20
        HideWindow = &H80
        DoNotActivate = &H10
        DoNotCopyBits = &H100
        IgnoreMove = &H2
        DoNotChangeOwnerZOrder = &H200
        DoNotRedraw = &H8
        DoNotReposition = &H200
        DoNotSendChangingEvent = &H400
        IgnoreResize = &H1
        IgnoreZOrder = &H4
        ShowWindow = &H40
    End Enum

    <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Friend Shared Function SetWindowPos( _
        ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, _
        ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, _
        ByVal uFlags As SetWindowPosFlags) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function 

原理很簡單
就是先置頂 再取消置頂 就會放在最上層
如不想獲得焦點就加個 Or DoNotActivate

===============

比較想知道如何能
得到一份目前 桌面上的視窗 順序清單
windows系統有提供這種資訊嗎?

魔術王子 2018-02-13 07:27 PM

C++ Builder 6.0應該算是Windows 98,XP時代的產品
下面這篇可以參考看看
http://delphi.ktop.com.tw/board.php?tid=30743


所有時間均為台北時間。現在的時間是 09:53 AM

Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2025, Jelsoft Enterprises Ltd.

『服務條款』

* 有問題不知道該怎麼解決嗎?請聯絡本站的系統管理員 *


SEO by vBSEO 3.6.1