Setforegroundwindow 将窗口置最前

Setforegroundwindow 将窗口置最前

   windows下简单的调用Setforegroundwindow并不能将窗口置最前,三种方法可以实现该功能。

1、先置topmost,然后取消
 SetWindowPos(HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
 SetWindowPos(HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);

2、改变系统环境参数
这些定义winuser.h里都有的
#define SPI_GETFOREGROUNDLOCKTIMEOUT 0x2000
#define SPI_SETFOREGROUNDLOCKTIMEOUT 0x2001
#define SPIF_UPDATEINIFILE 0x0001
#define SPIF_SENDWININICHANGE 0x0002

先设置环境变量
DWORD lockTime = 0;
SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, &lockTime, 0);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, 0, 
                     SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);

退出程序时还原环境变量

SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, lockTime,
SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
3、AllowSetForegroundWindow
 HMODULE hModule = NULL;
 ASFW_PTR pProcAddress = NULL;
 
 hModule = GetModuleHandle("User32");
 if (hModule != NULL)
  pProcAddress = (ASFW_PTR)GetProcAddress(hModule, "AllowSetForegroundWindow");
 if (pProcAddress != NULL)
 {
  nResult = (*pProcAddress)(-1);
  if (nResult == FALSE )
   MessageBox(NULL, "Could not set foreground permission", NULL, MB_OK | MB_ICONERROR);
 }

你可能感兴趣的:(Setforegroundwindow 将窗口置最前)