Anthony Raimondo
Anthony Raimondo

Reputation: 1721

c++ Moving a Window with out changing its size

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;

Upvotes: 2

Views: 5732

Answers (2)

Qaz
Qaz

Reputation: 61910

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:

client area

Now I would suggest just forgetting about that and using SetWindowPos:

SetWindowPos(hWnd, nullptr, 100, 100, 0, 0, SWP_NOSIZE | SWP_NOZORDER);

Upvotes: 5

David
David

Reputation: 4873

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

Upvotes: 4

Related Questions