c ++移动窗口而不改变其大小 [英] c++ Moving a Window with out changing its size

查看:72
本文介绍了c ++移动窗口而不改变其大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RECT rec;::GetClientRect(hWnd, &rec);int windowWidth = rec.right - rec.left, windowHeight = rec.bottom - rec.top;::printToDebugWindow(windowWidth,windowHeight);//打印2个数字MoveWindow(hWnd,100,100,windowWidth,windowHeight,FALSE);

问题是 windowWidth 和 windowHeight 由于某种原因正在改变.MoveWindow 似乎正在改变窗口尺寸.并将重绘设置为 TRUE 没有任何改变.

输出:

x:560,y:178
x: 544, y: 140
x: 528, y: 102
x: 512, y: 64
x: 496, y: 26

为什么每次迭代时尺寸都会改变?

我也试过:没有变化

 int windowWidth = rec.right, windowHeight = rec.bottom;

解决方案

您得到的是客户区的大小,而不是窗口的大小.更改:

GetClientRect(hWnd, &rec);

GetWindowRect(hWnd, &rec);

从 :

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

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;

解决方案

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);

这篇关于c ++移动窗口而不改变其大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆