BringWindowToTop是不工作,即使我得到类窗口的句柄 [英] BringWindowToTop is Not working even if I get the handle to Class Window

查看:241
本文介绍了BringWindowToTop是不工作,即使我得到类窗口的句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下方法注册我的类:

  BOOL CNDSClientDlg :: InitInstance()
{
//注册窗口于2010年11月16日更新,@Subhen
//注册我们希望使用的唯一类名
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;

//后面使用FindWindow的类名
wndcls.lpszClassName = _T(CNDSClientDlg);
//注册新类,如果失败,退出

if(!AfxRegisterClass(& wndcls))// [C]

{
return FALSE;
}
}

然后调用InitInstance方法并创建窗口类的构造函数:

  CNDSClientDlg :: CNDSClientDlg(CWnd * pParent / * = NULL * /)
:CDialog (CNDSClientDlg :: IDD,pParent)

{
InitInstance();

HWND hWnd;
hInst = AfxGetInstanceHandle(); //存储实例句柄在我们的全局变量
hWnd = CreateWindow(_T(CNDSClientDlg),NDS,WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,hInst, ;


}

编辑
可以通过以下代码为新创建的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)
{
//一个弹出窗口是活动的,把它放到顶部
FirstChildhWnd-> GetWindowPlacement(& wndplacement) ;
wndplacement.showCmd = SW_RESTORE;
FirstChildhWnd-> SetWindowPlacement(& wndplacement);
FirstChildhWnd-> SetForegroundWindow();
}



我可以找到 pWndPrev 不是NULL,但它不是把我的应用程序提前。我需要注册任何其他类而不是CNDSClientDlg。

解决方案

有些事情要看...



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



2 )对于从Windows 2000开始的SetForegroundWindow(),有一些Microsoft规定的规则。简短版本是只有最前面的应用程序可以更改前台窗口。这个想法是,不是最前面的应用程序不能跳跃在活动应用程序的前面。如果后台应用程序调用SetForegroundWindow(),Windows将闪烁应用程序的任务栏按钮,但实际上不会将应用程序带到前面。用户必须这样做。我过度简化规则,但这可能取决于您的具体情况。


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.

解决方案

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.

这篇关于BringWindowToTop是不工作,即使我得到类窗口的句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆