---
title: "将窗口移动到前端"
author: "java-he"
date: "2010-01-04T10:01:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2010-01-04T10:01:00Z"
id: 755447
url: "https://java-he.iteye.com/blog/755447"
---

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