When (and how) to lay out the children of a Win32

2019-02-11 00:37发布

问题:

Windows sends several messages when a window is resized:

  • WM_GETMINMAXINFO
  • WM_ENTERSIZEMOVE
  • WM_EXITSIZEMOVE
  • WM_NCCALCSIZE
  • WM_SIZING
  • WM_SIZE
  • WM_WINDOWPOSCHANGING
  • WM_WINDOWPOSCHANGED

and possibly more.

If I would like to re-position the children when my window is resized, where and how should I do so?
I'm looking for the "best" method -- i.e. the method with the fewest gotcha's and the least flicker.

My current method is to perform all the repositioning inside WM_NCCALCSIZE, using DeferWindowPos.
However, I've also tried handling it inside WM_WINDOWPOSCHANGED... but no matter where I handle it, it seems like there is always at least one "moment" when the window is painted in an in-between state, where the window's size has changed, but its contents have yet to be resized.

Another effect I would also like to avoid: moving a child after it has already moved. I don't want the user to see a control sliding down and then back up because of my change -- it should have as few transient effects as possible.

Am I doing this correctly? Is there a better place I can lay out the window's children?

回答1:

You should re-position the window in the WM_SIZE message, because that is the last one that the window recieves before complete it's task...To re-position the window you can use MoveWindow