2010-11-17 145 views
2

我登记我的类在下面的方法: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方法和类的构造函数创建窗口:

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


} 

现在在我的其他应用程序我发现窗口,并试图把顶部:

编辑 能带来newlyCreated窗户用下面的代码

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不为空,但它不是把我的应用程序前。我需要注册任何其他类而不是CNDSClientDlg。我想把我的MFC应用程序置顶。

回答

11

有几件事情来看待......

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

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

6
+0

@cbranch,@FrédéricHamidi,感谢这是Workinng。但是这会产生一个新的空白窗口,而不是我的跑窗。我认为这是因为创建窗口。我需要显示我的应用程序不是新窗口 – Simsons 2010-11-17 07:25:54

+0

@Subhen,你的意思是你在其他应用程序中创建了一个具有相同类名的窗口? – 2010-11-17 07:28:48

+0

我不太确定什么是相同的类名。但是,我的MFC应用程序中有多个类(对话框),我正在使用其中的一个,CNDSClientDlg – Simsons 2010-11-17 07:32:05

2

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

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

如果这两个应用程序都不是前台应用程序,那么你不是假设能够为前面带来一个窗口,虽然有办法(无意和故意)。

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

请详细解答这个问题。 – 2012-09-18 14:34:11

+0

你能解释一下这个答案如何解决OP的问题,这样我们都可以从中学习吗?现在它看起来像一个神奇的咒语。 – 2012-09-21 17:37:21

相关问题