有一段代码,是在我们的应用程序中将其他应用程序的窗口移动到最前端,当我们的程序在 Vista 下运行的时候遇到了些问题,有时候被移动窗口只是闪动任务栏上的按钮,并未能将窗口移动到前方来. 研究了一下,发现是否能够移动成功和当前自身进程所附加的输入上下文有关, 参见
WIN32 API AttachThreadInput()...
写了个 MyBringWindowToTop() 如下, 这是个 draft 把我用到过的能把窗口拿到最前方的 API 都罗列在里面了, 也没有正确的返回值, 供参考.
BOOL MyBringWindowToTop(HWNDhWnd)
{
HWNDhFrgWnd = ::GetForegroundWindow();
AttachThreadInput( GetWindowThreadProcessId(hFrgWnd, NULL), GetCurrentThreadId(), TRUE );
::SetForegroundWindow(hWnd);
::BringWindowToTop(hWnd);
if(!::BringWindowToTop(hWnd))
{
printf("BringWindowToTop Error %d/n", GetLastError());
}
else
{
printf("BringWindowToTop OK/n");
}
if(!::SetForegroundWindow(hWnd))
{
printf("SetForegroundWindow Error %d/n", GetLastError());
}
else
{
printf("SetForegroundWindow OK/n");
}
SwitchToThisWindow(hWnd, TRUE);
AttachThreadInput(GetWindowThreadProcessId(hFrgWnd, NULL),
GetCurrentThreadId(), FALSE);
returnTRUE;
}
在Vista系统下,使用API BringWindowToTop()移动窗口到最前端时,有时只能使任务栏按钮闪烁而无法真正置于前端。问题与当前进程的输入上下文相关,通过研究并利用AttachThreadInput()等API,编写了一个MyBringWindowToTop()函数,结合SetForegroundWindow()和SwitchToThisWindow()尝试解决这个问题。函数包含了错误检查,并在必要时调整线程输入上下文。
580





