即使我得到类窗口的句柄,BringWindowToTop也不起作用

时间:2010-11-17 05:17:53

标签: c++ windows mfc createwindow registerclass

我正在使用以下方法注册我的课程:

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的构造函数中创建窗口:

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);


}

现在在我的其他应用程序中,我正在找到窗口并试图将其置于顶部:

修改 能够使用以下代码带来新处理的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();
                        }

我能够找到窗口,因为pWndPrev不是NULL,但它没有将我的应用程序提前。我是否需要注册任何其他类而不是CNDSClientDlg。我想将我的MFC应用程序放在首位。

4 个答案:

答案 0 :(得分:11)

要注意的一些事情......

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

2)微软针对从Windows 2000开始的SetForegroundWindow()制定了一些规则。简短版本是只有最前面的应用程序可以更改前台窗口。我们的想法是,不是最前面的应用程序不能“跳到”活动应用程序的前面。如果后台应用程序调用SetForegroundWindow(),Windows将闪烁应用程序的任务栏按钮,但实际上不会将应用程序带到前面。用户必须这样做。我过分简化规则,但根据您的具体情况,这可能需要考虑。

答案 1 :(得分:6)

答案 2 :(得分:2)

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

假设您的其他应用程序是前台应用程序,并尝试将其前台状态传递给带有窗口的应用程序。

如果这两个应用程序都不是前台应用程序,那么你不会假设能够将窗口带到前面,尽管有办法(无论是意外还是故意)。< / p>

答案 3 :(得分:0)

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