SetForegroundWindow激活窗口


在Window98以下,如果程序要激活自己,只需要簡單的調用SetForegroundWindow即可達到目的。但到Win98以后,再也沒有這么簡單了。

新建一個簡單的工程,加進一個Timer控件,設置時間間隔為3秒,接着在時間事件中寫SetForegroundWindow(Handle),好,運行程序,將窗口切換到后台,3秒鍾之后,你看到的只是任務欄上閃了閃,窗口仍然躺在后面。

這是怎么回事呢,原來在Win98以后,窗口要使用SetForegroundWindow激活自己, 必須得到“允許”,允許的方式有很多種,我只介紹最簡單的一種,就是利用這個API:LockSetForegroundWindow先解鎖 Foreground的窗口,然后再調用SetForegroundWindow。

LockSetForegroundWindow在Delphi的Windows單元中並沒有聲明,需要自己聲明,我將激活的函數重新封裝如下,需要的朋友直接用就可以了:

const
LSFW_LOCK     = 1;
LSFW_UNLOCK   = 2;
function LockSetForegroundWindow(uLockCode: DWORD): BOOL; stdcall;
implementation
function LockSetForegroundWindow; external 'user32.dll' name 'LockSetForegroundWindow';
function wdSetForegroundWindow(Handle: THandle): Boolean;
begin
//-----------------------------------------------------
//作者:linzhenqun
//時間:2006-11-1
//說明:使Win98以上的窗口都可以設置Foreground的函數
//-----------------------------------------------------
if ((Win32Platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion> 4))//up win 2000
    or ((Win32Platform = VER_PLATFORM_WIN32_WINDOWS) and //up win 98
    ((Win32MajorVersion > 4) or
    ((Win32MajorVersion = 4) and
    (Win32MinorVersion > 0)))) then
    LockSetForegroundWindow(LSFW_UNLOCK);
Result := SetForegroundWindow(Handle);
end;

現在你在時間事件中寫下如下代碼:

Application.Restore;

SetForegroundWindow(Handle);

那么,窗口就可以自己激活自己了,爽吧!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM