BringWindowToTop不工作,即使我得到类窗口的句柄

我正在用以下方法注册我的类:

BOOL CNDSClientDlg::InitInstance() { //Register Window Updated on 16th Nov 2010, @Subhen // Register our unique class name that we wish to use WNDCLASS wndcls; memset(&wndcls, 0, sizeof(WNDCLASS)); wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW; wndcls.lpfnWndProc = ::DefWindowProc; wndcls.hInstance = AfxGetInstanceHandle(); wndcls.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wndcls.lpszMenuName = NULL; //Class name for using FindWindow later wndcls.lpszClassName = _T("CNDSClientDlg"); // Register new class and exit if it fails if(!AfxRegisterClass(&wndcls)) // [C] { return FALSE; } } 

然后调用InitInstance方法并在Class的构造函数中创build窗口:

 CNDSClientDlg::CNDSClientDlg(CWnd* pParent /*=NULL*/) : CDialog(CNDSClientDlg::IDD, pParent) { InitInstance(); HWND hWnd; hInst = AfxGetInstanceHandle(); // Store instance handle in our global variable hWnd = CreateWindow(_T("CNDSClientDlg"), "NDS", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInst, NULL); } 

现在在我的另一个应用程序中,我正在寻找窗口并试图将其带入顶端:

编辑能够带来下面的代码新创build的Windows

  CWnd *pWndPrev = NULL; CWnd *FirstChildhWnd = NULL; pWndPrev = CWnd::FindWindow(_T("CNDSClientDlg"),NULL); if(pWndPrev != NULL) { //pWndPrev->BringWindowToTop(); WINDOWPLACEMENT wndplacement; pWndPrev->GetWindowPlacement(&wndplacement); wndplacement.showCmd = SW_RESTORE; pWndPrev->SetWindowPlacement(&wndplacement); pWndPrev->SetForegroundWindow(); FirstChildhWnd = pWndPrev->GetLastActivePopup(); if (pWndPrev != FirstChildhWnd) { // a pop-up window is active, bring it to the top too FirstChildhWnd->GetWindowPlacement(&wndplacement); wndplacement.showCmd = SW_RESTORE; FirstChildhWnd->SetWindowPlacement(&wndplacement); FirstChildhWnd->SetForegroundWindow(); } 

我能够find窗口,因为pWndPrev不是NULL,但它没有提出我的应用程序前面。 我需要注册任何其他类而不是CNDSClientDlg。 我想把我的MFC应用程序顶部。

有几件事要看…

1)尝试SetForegroundWindow()而不是BringWindowToTop()。 我已经完成了Win32编程已经有一段时间了,但是我似乎记得BringWindowToTop()有一些限制(特别是在不同进程中使用Windows时)。

2)Microsoft从Windows 2000开始就有关SetForegroundWindow()的一些规定。简短的说法是只有最前面的应用程序可以改变前景窗口。 这个想法是,一个不是最前面的应用程序不能“跳到”活动应用程序的前面。 如果一个后台应用程序调用SetForegroundWindow(),Windows将会刷新应用程序的任务栏按钮,但实际上不会将应用程序放在前面。 用户必须这样做。 我只是简单的规则,但这可能是一些要看你的具体情况。

BringWindowToTop()只在调用进程是前台进程或者接收到最后一个输入事件时才起作用。

而是调用CWnd :: SetForegroundWindow() 。

在调用SetForegroundWindow之前,可能需要在“其他”应用程序中调用AllowSetForegroundWindow 。

这是假设你的其他应用程序是前台应用程序,并试图将其前台状态传递给窗口的应用程序。

如果这两个应用程序都不是前台应用程序,那么你不应该能够把前面的窗口,虽然有办法做到这一点(无论是故意的和故意的)。

 SetWindowPos(&wndTopMost, -1, -1, -1, -1, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW); SetForegroundWindow();