如何从 CreateWindowEx() 窗口获取宽度和高度?C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/615551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get width and height from CreateWindowEx() window? C++
提问by Brian R. Bondy
I have made a window with CreateWindowEx() function, now how do i get the width and height from that window i created? This sounds very basic thing to do, but i just couldnt find any answer ;_;
我已经用 CreateWindowEx() 函数创建了一个窗口,现在如何从我创建的那个窗口获取宽度和高度?这听起来很基本的事情,但我找不到任何答案;_;
This is needed because the window height is created automatically depending on how the Windows wants to create it.
这是必需的,因为窗口高度是根据 Windows 想要创建它的方式自动创建的。
Language C or C++
语言 C 或 C++
回答by Brian R. Bondy
Use GetWindowRect. Subtract the right from the left to get the width and the bottom from the top to get the height.
使用GetWindowRect。从左边减去右边得到宽度,从顶部减去底部得到高度。
RECT rect;
if(GetWindowRect(hwnd, &rect))
{
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
}
As a side note, if you'd like the client area instead of the entire window. You can use GetClientRect. For other information about the window you can use GetWindowInfo.
作为旁注,如果您想要客户区而不是整个窗口。您可以使用GetClientRect。有关窗口的其他信息,您可以使用GetWindowInfo。
回答by JaredPar
I believe you're looking for GetWindowInfo
我相信您正在寻找GetWindowInfo
Example:
例子:
HWND window = ::CreateWindowEx(...);
WINDOWINFO info;
if ( ::GetWindowInfo(window, &info) ) {
...
}
回答by dirkgently
Have you tried GetWindowRect()or GetWindowInfo()which returns a WINDOWINFOstructure?
您是否尝试过GetWindowRect()或GetWindowInfo()返回WINDOWINFO结构?
回答by SAMills
Given there's no indication why you need the size, and that the size can change if the window style is set to include resizable attributes [and the user resizes the window using minimize/maximize/restore or drags a window edge], your safest choice is to include a message handler for WM_SIZE and use the wparam and lparam parameter values to determine window dimensions. This way, you'll always know the current size. WM_SIZE is called in the sequence of messages post window creation.
鉴于没有指示您为什么需要大小,并且如果窗口样式设置为包含可调整大小的属性[并且用户使用最小化/最大化/恢复或拖动窗口边缘调整窗口大小],则大小可以更改,您最安全的选择是包含 WM_SIZE 的消息处理程序并使用 wparam 和 lparam 参数值来确定窗口尺寸。这样,您将始终知道当前的大小。WM_SIZE 在窗口创建后的消息序列中被调用。

