Window does not maximize when trying to run one instance of program
-
Only one instance of the program can run at one time. When launching the program from explorer, the code below works just fine. But when I try to launch it from another program, the following occurs: 1)When an instance already exists, it only activates and maximizes the window when the window is in the minimized state. When it's not minimized but hidden behind other windows, it merely activates it (icon blinking in the taskbar) and does not maximize it or bring it to the foreground. Why is that? 2)When no instance exists, it launches the program but the window is not maximized or activated. It gets maximized only during initial startup. Thanks!
BOOL CMyApp::InitInstance()
{
//Run only one instance
m_Instance = ::CreateMutex(NULL, FALSE, AfxGetApp()->m_pszExeName);DWORD dwMutexErr = GetLastError();
if (dwMutexErr == ERROR_ALREADY_EXISTS)
{
CWnd *pPrevWnd = CWnd::GetDesktopWindow()->GetWindow(GW_CHILD);
while (pPrevWnd)
{
if ( ::GetProp(pPrevWnd->GetSafeHwnd(), AfxGetApp()->m_pszExeName) )
{
if ( pPrevWnd->IsIconic() )
pPrevWnd->ShowWindow(SW_RESTORE); //restore window
pPrevWnd->SetForegroundWindow();
pPrevWnd->GetLastActivePopup()->SetForegroundWindow();
return FALSE;
}
pPrevWnd = pPrevWnd->GetWindow(GW_HWNDNEXT);
}
}// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;if (!pMainFrame->LoadFrame(MainFrame))
return FALSE;m_pMainWnd = pMainFrame;
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;//*** Prevent open new window on startup
cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;ParseCommandLine(cmdInfo);
if (cmdInfo.m_bRunEmbedded || cmdInfo.m_bRunAutomated)
return TRUE;_Module.UpdateRegistryFromResource(IDR_APP, TRUE);
_Module.RegisterServer(TRUE);// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;// The main window has been initialized, so show and update it.
pMainFrame->ShowWindow(SW_SHOWMAXIMIZED);
pMainFrame->UpdateWindow();::SetProp(pMainFrame->GetSafeHwnd(), m_pszExeName, (HANDLE)1);
return TRUE;
}-- modified at 13:11 Wednesday 5th October, 2005
-
Only one instance of the program can run at one time. When launching the program from explorer, the code below works just fine. But when I try to launch it from another program, the following occurs: 1)When an instance already exists, it only activates and maximizes the window when the window is in the minimized state. When it's not minimized but hidden behind other windows, it merely activates it (icon blinking in the taskbar) and does not maximize it or bring it to the foreground. Why is that? 2)When no instance exists, it launches the program but the window is not maximized or activated. It gets maximized only during initial startup. Thanks!
BOOL CMyApp::InitInstance()
{
//Run only one instance
m_Instance = ::CreateMutex(NULL, FALSE, AfxGetApp()->m_pszExeName);DWORD dwMutexErr = GetLastError();
if (dwMutexErr == ERROR_ALREADY_EXISTS)
{
CWnd *pPrevWnd = CWnd::GetDesktopWindow()->GetWindow(GW_CHILD);
while (pPrevWnd)
{
if ( ::GetProp(pPrevWnd->GetSafeHwnd(), AfxGetApp()->m_pszExeName) )
{
if ( pPrevWnd->IsIconic() )
pPrevWnd->ShowWindow(SW_RESTORE); //restore window
pPrevWnd->SetForegroundWindow();
pPrevWnd->GetLastActivePopup()->SetForegroundWindow();
return FALSE;
}
pPrevWnd = pPrevWnd->GetWindow(GW_HWNDNEXT);
}
}// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;if (!pMainFrame->LoadFrame(MainFrame))
return FALSE;m_pMainWnd = pMainFrame;
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;//*** Prevent open new window on startup
cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;ParseCommandLine(cmdInfo);
if (cmdInfo.m_bRunEmbedded || cmdInfo.m_bRunAutomated)
return TRUE;_Module.UpdateRegistryFromResource(IDR_APP, TRUE);
_Module.RegisterServer(TRUE);// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;// The main window has been initialized, so show and update it.
pMainFrame->ShowWindow(SW_SHOWMAXIMIZED);
pMainFrame->UpdateWindow();::SetProp(pMainFrame->GetSafeHwnd(), m_pszExeName, (HANDLE)1);
return TRUE;
}-- modified at 13:11 Wednesday 5th October, 2005
Hi elephantstar, > it merely activates it (icon blinking in the taskbar) > and does not maximize it or bring it to the foreground. > Why is that? Moving a window to the foreground behavior is now by design. IIRC, the behavior was changed around Windows 2000 (or maybe XP). Apparently, software developers were being rude about their use of setting active and foreground windows. I don't have a good reference for you. But hte following link may help. Jeff http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowfeatures.asp[^]
-
Only one instance of the program can run at one time. When launching the program from explorer, the code below works just fine. But when I try to launch it from another program, the following occurs: 1)When an instance already exists, it only activates and maximizes the window when the window is in the minimized state. When it's not minimized but hidden behind other windows, it merely activates it (icon blinking in the taskbar) and does not maximize it or bring it to the foreground. Why is that? 2)When no instance exists, it launches the program but the window is not maximized or activated. It gets maximized only during initial startup. Thanks!
BOOL CMyApp::InitInstance()
{
//Run only one instance
m_Instance = ::CreateMutex(NULL, FALSE, AfxGetApp()->m_pszExeName);DWORD dwMutexErr = GetLastError();
if (dwMutexErr == ERROR_ALREADY_EXISTS)
{
CWnd *pPrevWnd = CWnd::GetDesktopWindow()->GetWindow(GW_CHILD);
while (pPrevWnd)
{
if ( ::GetProp(pPrevWnd->GetSafeHwnd(), AfxGetApp()->m_pszExeName) )
{
if ( pPrevWnd->IsIconic() )
pPrevWnd->ShowWindow(SW_RESTORE); //restore window
pPrevWnd->SetForegroundWindow();
pPrevWnd->GetLastActivePopup()->SetForegroundWindow();
return FALSE;
}
pPrevWnd = pPrevWnd->GetWindow(GW_HWNDNEXT);
}
}// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;if (!pMainFrame->LoadFrame(MainFrame))
return FALSE;m_pMainWnd = pMainFrame;
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;//*** Prevent open new window on startup
cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;ParseCommandLine(cmdInfo);
if (cmdInfo.m_bRunEmbedded || cmdInfo.m_bRunAutomated)
return TRUE;_Module.UpdateRegistryFromResource(IDR_APP, TRUE);
_Module.RegisterServer(TRUE);// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;// The main window has been initialized, so show and update it.
pMainFrame->ShowWindow(SW_SHOWMAXIMIZED);
pMainFrame->UpdateWindow();::SetProp(pMainFrame->GetSafeHwnd(), m_pszExeName, (HANDLE)1);
return TRUE;
}-- modified at 13:11 Wednesday 5th October, 2005
1. Try
BringWindowToTop
. 2. ReplaceSW_RESTORE
withSW_MAXIMIZE
in thepPrevWnd->ShowWindow
call. Regards, BB http://spin.bartoszbien.com -
1. Try
BringWindowToTop
. 2. ReplaceSW_RESTORE
withSW_MAXIMIZE
in thepPrevWnd->ShowWindow
call. Regards, BB http://spin.bartoszbien.comThe above proved unsuccessful. What I did try was minimize the window and then restore it if the window was not already minimized. You can see the window minimize and then pop back up for display. Why does minimizing it work? This seems like poor coding to me but it serves as a workaround? How can I make it more efficient? Thanks!
if ( !pPrevWnd->IsIconic() )
{
pPrevWnd->ShowWindow(SW_MINIMIZE);
}
pPrevWnd->ShowWindow(SW_RESTORE);
pPrevWnd->SetForegroundWindow();
pPrevWnd->GetLastActivePopup()->SetForegroundWindow();