将窗口移动到前端, BringWindowToTop() 的问题

May 30th, 2008 | Tags:

有一段代码,是在我们的应用程序中将其他应用程序的窗口移动到最前端,当我们的程序在 Vista 下运行的时候遇到了些问题,有时候被移动窗口只是闪动任务栏上的按钮,并未能将窗口移动到前方来. 研究了一下,发现是否能够移动成功和当前自身进程所附加的输入上下文有关, 参见
WIN32 API AttachThreadInput()…

写了个 MyBringWindowToTop() 如下, 这是个 draft 把我用到过的能把窗口拿到最前方的 API 都罗列在里面了, 也没有正确的返回值, 供参考.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
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;
}

相关日志

  1. Tim
    April 26th, 2010 at 09:30
    Reply | Quote | #1

    比我写的好!!!

    Reply