BringWindowToTop is Not working even if I get the

2020-04-14 08:40发布

I am registering my Class in the following method:

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

and then calling the InitInstance method and creating the window in constructor of the 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);


}

Now in my other application I am finding the window and trying to bring to top:

Edit Able to bring newlyCreated Windows with below code

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

I am able to find the window as pWndPrev is not NULL , but It is not bringing up my application to front. Do I need to register any other class Instead of CNDSClientDlg. I want to bring my MFC application to top.

4条回答
别忘想泡老子
2楼-- · 2020-04-14 09:00

You may need to call AllowSetForegroundWindow in your "other" application before calling SetForegroundWindow.

That is assuming your other application is the foreground app and is trying to pass on its foreground status to the application with the window.

If neither app is the foreground app then you're not supposed to be able to bring a window to the front, although there are ways to do it (both accidentally and on purpose).

查看更多
我欲成王,谁敢阻挡
4楼-- · 2020-04-14 09:20
SetWindowPos(&wndTopMost, -1, -1, -1, -1,  SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
SetForegroundWindow();
查看更多
迷人小祖宗
5楼-- · 2020-04-14 09:23

A few things to look at...

1) Try SetForegroundWindow() instead of BringWindowToTop(). It's been awhile since I've done Win32 programming, but I seem to recall that BringWindowToTop() has some limitations (especially when working with windows in different processes).

2) There are some rules that Microsoft put in place regarding SetForegroundWindow() starting with Windows 2000. The short version is that only the front-most application can change the foreground window. The idea is that an application that is not front-most cannot "jump in front of" the active application. If a background application calls SetForegroundWindow(), Windows will flash the taskbar button for the app, but will not actually bring the app to the front. The user must do that. I'm oversimplifying the rules, but this may be something to look at depending on your specific scenario.

查看更多
登录 后发表回答