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

来源:互联网 发布:剑灵人女完美身材数据 编辑:程序博客网 时间:2024/04/27 20:51
有一段代码,是在我们的应用程序中将其他应用程序的窗口移动到最前端,当我们的程序在 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;
}
原创粉丝点击