A subtlety in restoring previous window position

Date:March 14, 2005 / year-entry #63
Tags:other
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20050314-00/?p=36203
Comments:    34
Summary:A common feature for many applications is to record their screen location when they shut down and reopen at that location when relaunched. If implemented naively, a program merely restores from its previous position unconditionally. You run into usability problems with this naive implementation. If a user runs two copies of your program, the two...

A common feature for many applications is to record their screen location when they shut down and reopen at that location when relaunched. If implemented naively, a program merely restores from its previous position unconditionally.

You run into usability problems with this naive implementation. If a user runs two copies of your program, the two windows end up in exactly the same place on the screen. Unless the user paid close attention to their taskbar, it looks like running the second copy had no effect. Now things get interesting.

Depending on what the program does, the second copy may encounter a sharing violation, or it may merely open a second copy of the document for editing, or two copies of the song may start playing, resulting in a strange echo effect since the two copies are out of sync. Even more fun is if the user hits the Stop button and the music keeps playing! Why? Because only the second copy of the playback was stopped. The first copy is still running.

I know one user who not infrequently gets as many as four copies of a multimedia title running, resulting in a horrific cacophany as they all play their attract music simultaneously, followed by mass confusion as the user tries to fix the problem, which usually consists of hammering the "Stop" button on the topmost copy. This stops the topmost instance, but the other three are still running...

If a second copy of the document is opened, the user may switch away from the editor, then switch back to the first instance, and think that all the changes were lost. Or the user may fail to notice this and make a conflicting set of changes to the first instance. Then all sorts of fun things happen when the two copies of the same document are saved.

Moral of the story: If your program saves and restores its screen position, you may want to check if a copy of the program is already running at that screen position. If so, then move your second window somewhere else so that it doesn't occupy exactly the same coordinates.


Comments (34)
  1. Almost Anonymous says:

    There seems to be no solution to this problem if your application last started maximized!

  2. Peter Cooper Jr. says:

    I’ve also noticed some programs doing things like this, and then showing up off the screen if I change my resolution or change the positioning of my multiple monitors. So, it’s probably a good idea to make sure that the coordinates you want to restore at are still within the visible area that the user can see.

  3. Tim Smith says:

    If the previous instance is maxed, you can start the next copy in restored mode using the previous coords. Also, you can restore the previous instance and then continue on with the normal positioning system.

  4. Ben Hutchings says:

    On a vaguely related note, Windows Explorer has/had a bizarre bug which causes it to open windows with a width of zero! I never figured out how this happened or how to fix it – for some reason the default size didn’t get updated even though I kept resizing these zero-width windows using Alt-Space. I can’t imagine how an ordinary user would cope with invisible windows like that.

  5. > I’ve also noticed some programs doing things like this, and then

    > showing up off the screen if I change my resolution or change the

    > positioning of my multiple monitors. So, it’s probably a good idea

    > to make sure that the coordinates you want to restore at are still

    > within the visible area that the user can see.

    I agree.

    Best solution is probably what many applications already do and

    prevent multiple instances from being run.

  6. J.Marsch says:

    Another issue that has bitten me many times:

    The desktop dimensions might have changed.

    I have a dual-monitor workstation at work, but I frequently VPN in from home and use Remote Desktop. (single monitor at home)

    Consider what happens you start an app in remote desktop, and its former location was on the second monitor. The form opens so far to the right that it is completely non-visible. That’s really irritating!! You have to use the move option on the taskbar, and then use the keyboard arrows to drag the form back over.

    Even worse, if the app doesn’t take this into account with its dialog boxes, you can get modal dialogs on the non-existent monitor. (Happens with Visio all the time, because Visio tries to remember which monitor to display its dialogs on).

  7. Dave says:

    Several apps I’ve used will behave strangely if you close them while they are minimized, then reopen them. I assume they’re using something other than GetWindowPlacement to determine where they were.

  8. Scott says:

    I have the same problem switching between my laptop LCD and my desktop. Very different resolutions.

    Is there any way for an open application to realize the screen has been resized, and that it is now off-screen?

  9. > Is there any way for an open application to realize the screen has

    > been resized, and that it is now off-screen?

    I think there is a notification when the resolution is changed while

    an application is running, but beside storing the resolution from the

    last start and comparing this value with the current one I am not

    aware of any way to determine such a change.

  10. mb says:

    Some programs use GetWindowPlacement to save the position and then they use SetWindowPos to set it.

    Try it with the bar on the top, it’s funny…

    http://blogs.msdn.com/oldnewthing/archive/2004/07/07/175285.aspx

  11. Tim Robinson says:

    Is there any way for an open application to realize the screen has been resized, and that it is now off-screen?

    WM_DISPLAYCHANGE will tell you this. Although I believe Windows will resize open windows when the user changes resolution.

    As far as remembering your window position goes: my code handle this by intersecting the saved rectangle with each of the monitor’s rectangles. If the saved position doesn’t overlap any of the monitors, then the program reverts to using CW_USEDEFAULT.

  12. but beside storing the resolution from the last start and comparing

    > this value with the current one I am not aware of any way to

    > determine such a change.

    Just reread my statement and found that it could be confusing. Of course I meant I am not aware of a way to determine whether the resolution was changed between two starts.

  13. Ivo says:

    A pet peeve of mine is the VS.NET + Deskmenu Powertoy combo. I keep a shortcut of Visual Studio .NET on my desktop and use the deskmenu to launch it. Visual Studio always starts in "restored" mode, even if it was maximized when I closed it! Probably Deskmenu uses SW_SHOWNORMAL command and Visual Studio decides to ignore its saved settings. (BTW, Visual Studio 6 did work correctly, this problem started with .NET)

    Whose fault is this? Should Deskmenu use SW_SHOW instead, or Visual Studio should ignore the show command?

  14. HA HA HA says:

    erk, typo! its ‘cacaphony’ — ‘phon’ as in suond.

    bonk bonk bonk…

  15. Mike Dimmick says:

    Am I the only one who finds it funny that the post correcting Raymond’s spelling has two spelling errors – and one of them is the word being corrected?

    ‘Cacophony’.

    Those who can’t really shouldn’t criticise.

  16. HA HA HA says:

    mike good piont! but if u gota be wihtuot sin to cast sttonas then nboody culd cast stoans at all! an nobody wnats 2 lieve in a wrold lik taht.

  17. Yo mihgt be rigt, whu nows!

  18. Cynic says:

    And what if the window completely fills the work area, but it’s not maximized? Sometimes trying to protect the user from himself is just not worth it.

  19. Jaap says:

    Yeah, I see this all the time. It’s a flaw in the windows user interface in my opinion. (Not the position, but the opening of applications twice)

    Naive users, and even me occasionally, click the application icon twice because it just takes too long for the application to show up. And there is hardly any noticable feedback from the windows GUI if a program is lauching.

    And an application should only be allowed to open twice if it load’s two distinct documents, or if the users requests to do so from with the application. And then, yes, you are right, a naive placing policy is not enough.

  20. Jack Mathews says:

    I would absolutely LOVE to see two functions added to Win32 or put in some sort of utility library.

    CascadeWindowPos( WINDOWPOS *out, WINDOWPOS const *in ) – Offsets the window down and to the right, or moves it up to the top leftif it’s out of room. That would help to solve this problem.

    EnsureWindowPosVisible( WINDOWPOS *out, WINDOWPOS const *in, flags ) – Will ensure that the window is partially visible, full visible, not spanning monitors, et cetera depending on the flags parameter.

    That is, if these functions don’t already exist. These would be so wonderfully useful for multimon situations.

  21. Joku says:

    Most of my time goes in maximized windows or fullscreen apps. Atleast when you have the todays standard 1280×1024 display.

    Most of the time if the window is not maximized goes in moving it around. So the default should be to open apps in maximized state. Now that we have everything running maxed, it is good time to think how to make the experience better if you need to do work between the apps. The answer obviously is not to restore them to a smaller size to do some drag drop crap.

    I would not be surprised if Apple has solved this already.

  22. lowercase josh says:

    Note that if a window has wandered off-screen, you don’t have to move it all the way back with the keyboard. Press any arrow key once and then move the mouse. The window will follow the mouse cursor until you click. If it happens a lot, remember alt-space, M, arrow, move mouse, click.

    Windows will move/resize windows that are on the monitor that changed, but not those out in never land.

    Just don’t window movement and prevent your window from going off-screen at all, ever. Starting off-screen is bad. Allowing the user to move the window off-screen is good. (Virtual desktops!)

    And not all corrections are criticism.

  23. Aaargh! says:

    A bit offtopic:

    One of the things I really miss in windows is session management, every *nix desktop out there has it, but at work I’m stuck with win2k and every morning I have to start all the applications I need and move them to the correct monitor, this is quite annoying.

    Will there ever be session management in win32 ?

  24. Goblin says:

    Aaargh!: Microsoft hasn’t changed the functionality of its window manager since 1995. That’s ten years — more than an eternity in the short-lived world of software. What makes you believe that they will change it in the foreseeable future? How long are you willing to wait?

  25. Aaargh! says:

    "How long are you willing to wait?"

    I stopped waiting for a decent windows version shortly after win98 was released, that was the last version of win32 I had installed on my home machine, in a dual-boot config with Linux. I thrashed it when I realized I didn’t actually use it anymore and I needed the disk-space.

    Sadly, I have to use win32 at work, so I would still appreciate it if they would fix their OS, but I’ve given op the hope it might actually happen.

  26. Reuben Harris says:

    I once wrote a desktop utility (a free tool shipped with the drivers for a certain graphics card) that hooked into all desktop apps to provide a few useful things like starting new apps up in their last position. I’m pleased to say it had that exact feature… it would iterate through all top-level windows, and if it found one with the same classname and already in the position, it would move window-caption-height pixels to the right and down.

    Re the zero-window width thing, I had IE6 doing this to me at home. No matter how many times I resized the window (by dragging window borders) new IE windows kept opening with zero width. Finally my non-technical partner found and used the "Size" command on the window menu and of course only then did IE get the idea…. wow was she smug! I don’t know why the WM_SYSCOMMAND made a difference.

  27. Neil says:

    There’s an app whose annoying behaviour in this regard I often run into.

    It’s called "notepad.exe".

    Sigh…

  28. multimon says:

    It’s hard to restore the coords correctly. Several ‘gotchas’ is mentioned already (minimized window, resolution change, multimonitor, …). Usually I want to "bounce" the window against any border (and resize it if it’s larger than the desktop), but it becomes complicated with multimonitor. Function for this should have been included in multimon-api.

    And, applications which doesn’t allow several instances usually do this because it’s hard to manage shared resources. Not because it is userfriendly in any way. I hate those programs.

    The work-around (which works 99% of the time) is to use 2 computers.

  29. BlackTigerX says:

    what kills me is, I have my laptop connected to my 21" monitor and use both monitors for designing

    but if I turn on the laptop with the other monitor disconnected and open Photoshop, it will sometimes open in the ghost monitor!… and what’s worst, I can manage to move that window manually, but when I do "File, Open", or "File Save", etc, those open in the ghost monitor!!… and you can’t move those windows!… arrrrgghh… I have to connect the monitor open every single window in the laptop monitor before I can work in the laptop by itself

  30. RonO says:

    I must be the lone odd egg. I find I *don’t* want my window positions anything but what I last set them at. This includes mutlitple instances of the app displaying over each other. I also want an app to remember its last state (min/max/normal) and display in that state every time (or as many times) I open it.

  31. coords says:

    >I must be the lone odd egg. I find I *don’t* want my window positions anything but what I last set them at.

    Me 2.

    Have never heard any complain about "usability problems" with this approach.

  32. Andy Babiec says:

    Agree with the comments about apps that don’t notice when you move from a multi-monitor set-up to a single screen.

    I now am used to moving all apps back to the primary display before I shut them down as I can’t keep track of which ones do not handle this scenario well.

  33. Walk the template header and do what it says.

  34. tolaris says:

    "So the default should be to open apps in maximized state. Now that we have everything running maxed, it is good time to think how to make the experience better if you need to do work between the apps. The answer obviously is not to restore them to a smaller size to do some drag drop crap.

    I would not be surprised if Apple has solved this already."

    ‘the answer to do some drag drop crap’ is obviously drag-and-drop the crap onto application button on the taskbar, which causes this application to be put in the front and then you can drop the crap in it wherever you like. On Windows, anyway… no idea how Apple has solved this, if at all.

Comments are closed.


*DISCLAIMER: I DO NOT OWN THIS CONTENT. If you are the owner and would like it removed, please contact me. The content herein is an archived reproduction of entries from Raymond Chen's "Old New Thing" Blog (most recent link is here). It may have slight formatting modifications for consistency and to improve readability.

WHY DID I DUPLICATE THIS CONTENT HERE? Let me first say this site has never had anything to sell and has never shown ads of any kind. I have nothing monetarily to gain by duplicating content here. Because I had made my own local copy of this content throughout the years, for ease of using tools like grep, I decided to put it online after I discovered some of the original content previously and publicly available, had disappeared approximately early to mid 2019. At the same time, I present the content in an easily accessible theme-agnostic way.

The information provided by Raymond's blog is, for all practical purposes, more authoritative on Windows Development than Microsoft's own MSDN documentation and should be considered supplemental reading to that documentation. The wealth of missing details provided by this blog that Microsoft could not or did not document about Windows over the years is vital enough, many would agree an online "backup" of these details is a necessary endeavor. Specifics include:

<-- Back to Old New Thing Archive Index