c++ Moving a Window with out changing its size

别等时光非礼了梦想. 提交于 2020-01-24 12:54:26

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!