其他分享
首页 > 其他分享> > c-即使我获得类窗口的句柄,BringWindowToTop也无法正常工作

c-即使我获得类窗口的句柄,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的构造函数中创建窗口:

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应用程序放在顶部.

解决方法:

有几件事要看…

1)尝试使用SetForegroundWindow()而不是BringWindowToTop().自从我完成Win32编程以来已经有一段时间了,但是我似乎回想起BringWindowToTop()有一些限制(尤其是在不同进程中使用Windows时).

2)Microsoft从Windows 2000开始就SetForegroundWindow()制定了一些规则.简短的版本是,只有最前面的应用程序才能更改前景窗口.这个想法是,不是最前面的应用程序不能“跳到”活动应用程序的前面.如果后台应用程序调用SetForegroundWindow(),则Windows将刷新该应用程序的任务栏按钮,但实际上不会将该应用程序置于最前面.用户必须这样做.我过分简化了规则,但这可能取决于您的特定情况.

标签:c,windows,mfc,createwindow,registerclass
来源: https://codeday.me/bug/20191011/1895919.html