How to bring window to the foreground?

本文探讨了在Windows 98/2000等操作系统中将应用程序窗口置于前台的有效方法。介绍了三种技巧:使用未公开的SwitchToThisWindow API函数;通过调整系统参数;利用AttachThreadInput函数实现跨线程窗口激活。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

How to bring window to the foreground?

In the past the question was trivial - SetForegroundWindow() Win32 API function was working fine in the time of Win95 and WinNT4. But since Win98 the things have been changed. The function call will not bring the appication window to the foreground in Win98/2K if the application is not currently active (has an input focus). This gives only minimized window blinking in the task bar. In general, that a good idea (recommended by Microsoft) to not irritate a user with suddenly popuping window, but sometimes it is really required. There are some overcomings of the matter. I know three of them.

The first one intends using undocumented SwitchToThisWindow() API function from user32.dll.

   HMODULE hLib = GetModuleHandle("user32.dll");
   void (__stdcall *SwitchToThisWindow)(HWND, BOOL);
   (FARPROC &)SwitchToThisWindow = GetProcAddress(hLib, "SwitchToThisWindow");
   SwitchToThisWindow(hWnd, TRUE);

But it does not work in the lates versions of Win98/2K.

Another means is to use something like this:

   DWORD dwTimeout;
   SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, &dwTimeout, 0);
   SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, 0, 0);
   SetForegroundWindow(hWnd);
   SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, (LPVOID)dwTimeout, 0);

It works in Win98 but not in Win2K.

And the last third trick is to use AttachThreadInput() to help with the desired behaviour.

   HWND hCurrWnd;
   int iMyTID;
   int iCurrTID;

   hCurrWnd = ::GetForegroundWindow();
   iMyTID   = GetCurrentThreadId();
   iCurrTID = GetWindowThreadProcessId(hCurrWnd,0);

   AttachThreadInput(iMyTID, iCurrTID, TRUE);
   SetForegroundWindow(hWnd);
   AttachThreadInput(iMyTID, iCurrTID, FALSE);

This works in Win98/ME/2K always. I have tested it in Win2K. Works indeed. For the details see recommended links.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值