即使我获得了 Class Window 的句柄,BringWindowToTop 也无法正常工作
我正在通过以下方法注册我的班级:
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;
}
}
然后调用InitInstance方法在类的构造函数中创建窗口:
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:
编辑能够使用以下代码带来新创建的 Windows
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();
}
我能够找到窗口,因为 pWndPrev
不是 NULL ,但它没有将我的应用程序放在前面.我是否需要注册任何其他类而不是 CNDSClientDlg.我想将我的 MFC 应用程序置顶.
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.
推荐答案
有几件事要看...
1) 尝试使用 SetForegroundWindow() 而不是 BringWindowToTop().自从我完成 Win32 编程以来已经有一段时间了,但我似乎记得 BringWindowToTop() 有一些限制(尤其是在不同进程中使用窗口时).
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) 从 Windows 2000 开始,Microsoft 针对 SetForegroundWindow() 制定了一些规则.简短的版本是只有最前面的应用程序才能更改前景窗口.这个想法是,不是最前面的应用程序不能跳到"活动应用程序的前面.如果后台应用程序调用 SetForegroundWindow(),Windows 将闪??烁应用程序的任务栏按钮,但实际上不会将应用程序带到最前面.用户必须这样做.我过度简化了规则,但这可能需要根据您的具体情况来考虑.
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.
相关文章