How do I find out what size the window manager would have chosen for my window?


We saw some time ago how the window manager decides where to place a newly-created window if you pass the CW_USE­DEFAULT values when creating the window. But what if you want to filter the values first? If you pass an explicit upper left corner but pass CW_USE­DEFAULT for the width and height, then the bottom right corner will be down near the bottom right corner of the screen.

Given that your CW_USE­DEFAULT code is buggy, is there a way I can ask how big you would have made the window, so I can go in and fix your mistake? If I try to resize it after the fact, there's an ugly flicker. I'm kind of disgusted that something this simple still doesn't work after all these years. This is why Apple is eating your lunch. This code was obviously never tested because it doesn't even work.

There appears to be some elevation of opinion to fact here, because the customer thinks that when you say "I will let you decide where to put the bottom right corner of the window", and when the window manager decides to put the bottom right corner the window near the bottom right corner of the screen, then that is obviously a bug.

Yeesh, if you don't want to let the window manager choose the bottom right corner of the window, then don't pass the value that says "I will let you choose the bottom right corner of the window."

Anyway, back to the original question.

If you want to get a peek at the coordinates the window manager chose when you said, "You go ahead and choose," you can just create the window without the WS_VISIBLE flag. And then look at the rectangle, and if you don't like it, resize your window while it is still hidden. When you're finally happy: Show it.

Comments (16)
  1. ErikF says:

    If this person goes to a restaurant and tells the waiter, "Surprise me!", does he then complain when he gets a cheeseburger instead of a steak? By using CW_USEDEFAULT, you've explicitly said "Surprise me!" to the window manager so should expect anything (hopefully something sensible, but that's not in the contract.)

  2. Christian says:

    If he wants to check the returned values for CW_USEDEFAULT he seems to have some criteria to decide if they are correct or not. Why doesn't he use this criteria to calculate the wanted size on its own instead of relying on the window manager?

  3. dave says:

    I note your correspondent uses the popular internet technique of (1) asking you for help, (2) insulting you.

  4. alv says:

    If you pass an explicit size but CW_USEDEFAULT as the upper left corner, Windows will in many cases put the window in a position where the lower and/or right part is only partly visible because it is off the work area. Unlike the issue raised by the moron who wrote the "bug report" above, shouldn't this be considered as a bug?

    [Arguably. Handling of CW_USE­DEFAULT is not a sophisticated feedback system. It's "if (x == CW_USEDEFAULT) x = CalculateDefaultLeft(); if (cx == CW_USEDEFAULT) cx = CalculateDefaultWidth(x);" It chooses the top left, then it chooses the size. It doesn't try to adjust the top left based on the future size. Arguably a bug, but it's not going to change at this point for compatibility reasons. -Raymond]
  5. Joshua says:

    [Arguably. Handling of CW_USEĀ­DEFAULT is not a sophisticated feedback system. It's "if (x == CW_USEDEFAULT) x = CalculateDefaultLeft(); if (cx == CW_USEDEFAULT) cx = CalculateDefaultWidth(x);" It chooses the top left, then it chooses the size. It doesn't try to adjust the top left based on the future size. Arguably a bug, but it's not going to change at this point for compatibility reasons. -Raymond]

    Windows 95 would punt it back on screen if it ended up partially off screen.

  6. Clipboarder Gadget says:

    Windows also liked to place windows with CW_USEDEFAULT partially under the taskbar when the taskbar was on the left. But they seem to have actually fixed this in Windows 8.1.

  7. ErikF: Maybe your analogy can be worked out fully: the person went to a restaurant and told the waiter "Surprise me with a _tiny_ _cake_!". Then he complains that the cake is tiny. (so the window manager puts the bottom right to the bottom right (ideally)).

  8. can't have it both ways says:

    if (windows_managers_size == what_i_want)

     let_window_manager_decide();

    else

     let_me_decide();

  9. bdcrazy says:

    Kinda wonder if they want a recommendation instead.

    If ( what do you recommend? == something reasonable)

     use it

    else

     use this.

  10. Well-designed programs just save their WINDOWPLACEMENT in the registry and restore it on startup.

  11. Aatch says:

    > This is why Apple is eating your lunch. This code was obviously never tested because it doesn't even work.

    Given that Microsoft make a point of extensive testing for features, this is kinda annoying. Sure, MS software isn't some bastion of bug-free code, but I appreciate the fact that normally when they say something works, it works.

  12. Azarien says:

    @alegr1: …aaand then they end up somewhere far off-screen, because the application didn't notice that screen resolution (or even the number of monitors) has changed.

  13. ChrisR says:

    @Azarien: Or, the developers realize these things happen and use a handy function like ClipOrCenterRectToMonitor [1].

    [1] msdn.microsoft.com/…/dd162826(v=vs.85).aspx

  14. Daniel Rose says:

    @Azarien: If they restore the WINDOWPLACEMENT using SetWindowPlacement(), then the system adjusts it automatically.

    "If the information specified in WINDOWPLACEMENT would result in a window that is completely off the screen, the system will automatically adjust the coordinates so that the window is visible, taking into account changes in screen resolution and multiple monitor configuration."

  15. JDT says:

    Retort: Apple know Microsoft will eat them for lunch; hence their name.

Comments are closed.