Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Window does not maximize when trying to run one instance of program

Window does not maximize when trying to run one instance of program

Scheduled Pinned Locked Moved C / C++ / MFC
linuxhelpquestionannouncement
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    elephantstar
    wrote on last edited by
    #1

    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

    J B 2 Replies Last reply
    0
    • E elephantstar

      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

      J Offline
      J Offline
      Jeffrey Walton
      wrote on last edited by
      #2

      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[^]

      1 Reply Last reply
      0
      • E elephantstar

        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

        B Offline
        B Offline
        Bartosz Bien
        wrote on last edited by
        #3

        1. Try BringWindowToTop. 2. Replace SW_RESTORE with SW_MAXIMIZE in the pPrevWnd->ShowWindow call. Regards, BB http://spin.bartoszbien.com

        E 1 Reply Last reply
        0
        • B Bartosz Bien

          1. Try BringWindowToTop. 2. Replace SW_RESTORE with SW_MAXIMIZE in the pPrevWnd->ShowWindow call. Regards, BB http://spin.bartoszbien.com

          E Offline
          E Offline
          elephantstar
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups