问题
RECT rec;
::GetClientRect(hWnd, &rec);
int windowWidth = rec.right - rec.left, windowHeight = rec.bottom - rec.top;
::printToDebugWindow(windowWidth,windowHeight); //prints 2 numbers
MoveWindow(hWnd,100,100,windowWidth,windowHeight,FALSE);
The problem is that the windowWidth and windowHeight are changing for some reason. MoveWindow seems to be changing the windows dimensions. and setting repaint to TRUE changes nothing.
outPut:
x: 560, y: 178
x: 544, y: 140
x: 528, y: 102
x: 512, y: 64
x: 496, y: 26
why are the dimensions changing every iteration?
i also tryed: No change
int windowWidth = rec.right, windowHeight = rec.bottom;
回答1:
You're getting the size of the client area, not the window. Change:
GetClientRect(hWnd, &rec);
to
GetWindowRect(hWnd, &rec);
Stolen from MSDN, this picture shows the client area:
Now I would suggest just forgetting about that and using SetWindowPos:
SetWindowPos(hWnd, nullptr, 100, 100, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
回答2:
Use SetWindowPos() instead. It has flags that allow you to tell the system to not change the size. http://msdn.microsoft.com/en-us/library/windows/desktop/ms633545(v=vs.85).aspx
来源:https://stackoverflow.com/questions/16907649/c-moving-a-window-with-out-changing-its-size