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. ATL / WTL / STL
  4. window placement and restoration query

window placement and restoration query

Scheduled Pinned Locked Moved ATL / WTL / STL
c++databasecomquestion
3 Posts 2 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.
  • J Offline
    J Offline
    Jonathan Davies
    wrote on last edited by
    #1

    I have a WTL SDI App which contains a couple of splitter bars and am trying to save and restore the position and size of the main window and the position of the splitter bars. I started using the classes in the 2001 CP article: 'Saving and restoring window appearance in WTL'[^] which, for the Main-Frame wrap GetWindowPlacement and SetWindowPlacement. For the splitters the classes simply get/set the splitter position. These classes rely on creating the window then restoring the windows position etc after creation. Initially with this implementation I found the vertical and horizontal splitters crept to the right and down respectively on each restoration but this was solved this by altering my call to CreateEx to pass in the size of the window from my saved WINDOWPLACEMENT:

    wndMain.CreateEx(0,ws.m_WindowPlacement.rcNormalPosition)

    The handler for WM_CREATE was now able to create and position the CSplitterWindow and CHorSplitterWindow splitter windows properly as it could obtain the correct size of the (restored) main window using GetClientRect. This works well unless the window is exited whilst maximised. On restoration, calling CreateEx as above now passes in the saved, non-maximised, size of the main window. In the OnCreate handler, calling GetClientRect to use in positioning the splitter windows now comes back with, not the maximized size of the window as I expected, but the size the window will become if it's Restored Down. Can I get the saved WINDOWPLACEMENT structure 'into' the Main-Frame window before or during my call to CreateEx in a way so that a call to GetClientRect within the OnCreate handler will obtain the size, even if the window is maximized? Common sense seems to tell me that I should'nt have to shoehorn in part of the WINDOWPLACEMENT struct during CreateEx and mess about with maximised/not-maximised and that there is a 'correct' way to get it all in before my OnCreate handler runs.

    S 1 Reply Last reply
    0
    • J Jonathan Davies

      I have a WTL SDI App which contains a couple of splitter bars and am trying to save and restore the position and size of the main window and the position of the splitter bars. I started using the classes in the 2001 CP article: 'Saving and restoring window appearance in WTL'[^] which, for the Main-Frame wrap GetWindowPlacement and SetWindowPlacement. For the splitters the classes simply get/set the splitter position. These classes rely on creating the window then restoring the windows position etc after creation. Initially with this implementation I found the vertical and horizontal splitters crept to the right and down respectively on each restoration but this was solved this by altering my call to CreateEx to pass in the size of the window from my saved WINDOWPLACEMENT:

      wndMain.CreateEx(0,ws.m_WindowPlacement.rcNormalPosition)

      The handler for WM_CREATE was now able to create and position the CSplitterWindow and CHorSplitterWindow splitter windows properly as it could obtain the correct size of the (restored) main window using GetClientRect. This works well unless the window is exited whilst maximised. On restoration, calling CreateEx as above now passes in the saved, non-maximised, size of the main window. In the OnCreate handler, calling GetClientRect to use in positioning the splitter windows now comes back with, not the maximized size of the window as I expected, but the size the window will become if it's Restored Down. Can I get the saved WINDOWPLACEMENT structure 'into' the Main-Frame window before or during my call to CreateEx in a way so that a call to GetClientRect within the OnCreate handler will obtain the size, even if the window is maximized? Common sense seems to tell me that I should'nt have to shoehorn in part of the WINDOWPLACEMENT struct during CreateEx and mess about with maximised/not-maximised and that there is a 'correct' way to get it all in before my OnCreate handler runs.

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      Assuming wndMain is of type CMainFrame.... Add a WINDOWPLACEMENT member to the CMainFrame class, add a WINDOWPLACEMENT parameter to the CMainFrame constructor that you us to initialise the WINDOWPLACEMENT member of CMainFrame. Use that member to set the window's placement (with SetWindowPlacement) in the WM_CREATE handler? Like this:

      // In your app's Run function:
      WINDOWPLACEMENT wp;
      // Get the stored window placement
      CMainFrame wndMain(wp);

      // In your CMainFrame class:
      CMainFrame(WINDOWPLACEMENT const& wp) : wp_(wp) {}
      CMainFrame()
      {
      WINDOWPLACEMENT wp = {0};
      wp.length = sizeof WINDOWPLACEMENT;
      wp.showCmd = SW_SHOWNORMAL;
      wp.rcNormalPosition.top = 100;
      wp.rcNormalPosition.left = 100;
      wp.rcNormalPosition.right = 200;
      wp.rcNormalPosition.bottom = 200;
      wp_ = wp;
      }

      WINDOWPLACEMENT wp_;

      LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
      {
      SetWindowPlacement(&wp_);
      // Rest of your OnCreate
      }

      Yes, it works and you get the maximized client rect back in OnCreate.

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      J 1 Reply Last reply
      0
      • S Stuart Dootson

        Assuming wndMain is of type CMainFrame.... Add a WINDOWPLACEMENT member to the CMainFrame class, add a WINDOWPLACEMENT parameter to the CMainFrame constructor that you us to initialise the WINDOWPLACEMENT member of CMainFrame. Use that member to set the window's placement (with SetWindowPlacement) in the WM_CREATE handler? Like this:

        // In your app's Run function:
        WINDOWPLACEMENT wp;
        // Get the stored window placement
        CMainFrame wndMain(wp);

        // In your CMainFrame class:
        CMainFrame(WINDOWPLACEMENT const& wp) : wp_(wp) {}
        CMainFrame()
        {
        WINDOWPLACEMENT wp = {0};
        wp.length = sizeof WINDOWPLACEMENT;
        wp.showCmd = SW_SHOWNORMAL;
        wp.rcNormalPosition.top = 100;
        wp.rcNormalPosition.left = 100;
        wp.rcNormalPosition.right = 200;
        wp.rcNormalPosition.bottom = 200;
        wp_ = wp;
        }

        WINDOWPLACEMENT wp_;

        LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
        {
        SetWindowPlacement(&wp_);
        // Rest of your OnCreate
        }

        Yes, it works and you get the maximized client rect back in OnCreate.

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        J Offline
        J Offline
        Jonathan Davies
        wrote on last edited by
        #3

        Hi Stuart, I never thought about passing the WINDOWPLACEMENT in to the CMainFrame prior to calling CreateEx(...). In the handler for WM_CREATE GetClientRect(...) now returns the expected size so I can set splitters etc up fine. Thanks. Odd behaviour though in the case where the application was either closed from a minimized or maximized state. On restarting, the retrieved prior state is indicated by the WINDOWPLACEMENT.showCmd. When previously minimized or maximized is the case, in between the OnCreate(...) handler exiting and the following call to ShowWindow(...) something changes the WINDOWPLACEMENT.rcNormalPosition information to either the size and position of a minimized or a maximized window respectively. This then means when the User Restores, it 'restores' to exactly the same minimized or maximized size! To the eye the window stays as it was. I had to work around this by resetting the WINDOWPLACEMENT again after CreateEx returns. I found I also had to call ShowWindow with the retreived showCmd rather than nCmdShow and remove any saved WPF_RESTORETOMAXIMIZED flag.

        int Run(LPTSTR /*lpstrCmdLine*/ = NULL, int nCmdShow = SW_SHOWDEFAULT)
        {
        // Saved WINDOWPLACEMENT is retreived here

        ...
        
        **if(WindowPlacement.flags & WPF\_RESTORETOMAXIMIZED)
        {
        	WindowPlacement.flags &= WPF\_SETMINPOSITION;
        }**
        
        CMainFrame wndMain(WindowPlacement);
        
        wndMain.CreateEx();
        **wndMain.SetWindowPlacement(&WindowPlacement);**
        wndMain.ShowWindow(WindowPlacement.showCmd);		
        
        ...
        

        Why this is necessary I don't know. Anyway it all seems OK now so thanks.

        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