【问题标题】:BringWindowToTop is Not working even if I get the handle to Class Window即使我获得了 Class Window 的句柄,BringWindowToTop 也无法正常工作
【发布时间】:2011-05-11 05:53:41
【问题描述】:

我正在通过以下方法注册我的班级:

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

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 is not NULL ,但它没有将我的应用程序放在前面。我是否需要注册任何其他类而不是 CNDSClientDlg。我想将我的 MFC 应用程序置顶。

【问题讨论】:

    标签: c++ windows mfc createwindow registerclass


    【解决方案1】:
    SetWindowPos(&wndTopMost, -1, -1, -1, -1,  SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
    SetForegroundWindow();
    

    【讨论】:

    • 请详细说明这个答案。
    • 您能解释一下这个答案如何解决 OP 的问题,以便我们都可以从中学习吗?现在它读起来就像一个神奇的咒语。
    【解决方案2】:

    【讨论】:

    • @cbranch ,@Frédéric Hamidi,谢谢这是工作。但这会带来一个新的空白窗口,而不是我正在运行的窗口。我认为这是因为创建窗口。我需要显示我的应用程序而不是新窗口
    • 我不太确定什么是相同的类名。但是是的,我的 MFC 应用程序中有多个类(对话框),我正在使用其中一个,CNDSClientDlg
    【解决方案3】:

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

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

    如果两个应用程序都不是前台应用程序,那么您不应该能够将窗口置于前面,尽管有办法做到这一点(无论是无意的还是故意的)。

    【讨论】:

      【解决方案4】:

      有几件事要看...

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

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

      【讨论】:

        猜你喜欢
        • 2023-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-08
        • 2018-12-18
        • 2019-03-15
        • 2021-05-07
        相关资源
        最近更新 更多