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

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

LockSetForegroundWindow在Delphi的Windows单元中并没有声明,需要自己声明,激活的函数重新封装如下:


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

 

窗口就可以自己激活自己了 

 

相关文章: