现在的位置: 首页 > 综合 > 正文

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

2013年01月17日 ⁄ 综合 ⁄ 共 988字 ⁄ 字号 评论关闭

有一段代码,是在我们的应用程序中将其他应用程序的窗口移动到最前端,当我们的程序在 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;
}

本文来自CSDN博客:http://blog.csdn.net/felixz/archive/2007/03/28/1544486.aspx

抱歉!评论已关闭.