窗口如何激活自己

- 中国WEB开发者网络 (http://www.webasp.net)
-- 技术教程 (http://www.webasp.net/article/)
--- 窗口如何激活自己 (http://www.webasp.net/article/28/27945.htm)
-- 作者:未知
-- 发布日期: 2006-11-10

在Window98以下,如果程序要激活自己,只需要简单的调用SetForegroundWindow即可达到目的。但到Win98以后,再也没有这么简单了。

    Window98以下,如果程序要激活自己,只需要简单SetForegroundWindow即可到目的。但到Win98以后,再也这么简单了。

新建一个简单的工程,加Timer控件,时间间3秒,接着在时间事件中SetForegroundWindow(Handle),好,行程序,窗口切到后台,3之后,看到的只是任务栏,窗口仍然在后面。

怎么回事呢,原Win98以后,窗口要使用SetForegroundWindow激活自己,必得到允许的方式有很多种,我只介绍最简单的一种,就是利用这个APILockSetForegroundWindow先解锁Foreground的窗口,然后再调用SetForegroundWindow

LockSetForegroundWindowDelphiWindows单元中并没有声明,需要自己声明,我将激活的函数重新封装如下,需要的朋友直接用就可以了:

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 > 4or
    ((Win32MajorVersion = 4and
    (Win32MinorVersion > 0)))) then
    LockSetForegroundWindow(LSFW_UNLOCK);
  Result := SetForegroundWindow(Handle);
end;

现在你在时间事件中写下如下代码:

Application.Restore;

wdSetForegroundWindow(Handle);

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


webasp.net