C++ WinAPI:创建具有指定客户区大小的窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4843806/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:43:43  来源:igfitidea点击:

WinAPI: Create a window with a specified client area size

c++winapiwindowcreatewindowex

提问by GeReV

I was wondering how can I create a window using Win32 API with a specific client area size.

我想知道如何使用具有特定客户区大小的Win32 API 创建窗口。

When trying to create a window using the following piece of code, the entire window is 640x480, with the window's chrome taking some of the client area:

尝试使用以下代码创建窗口时,整个窗口为 640x480,窗口的镶边占据了一些客户区:

HWND       hWnd;
WNDCLASSEX WndClsEx;
ZeroMemory(&WndClsEx, sizeof(WNDCLASSEX));

WndClsEx.cbSize        = sizeof(WNDCLASSEX);
WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
WndClsEx.lpfnWndProc   = DefWindowProc;
WndClsEx.cbClsExtra    = 0;
WndClsEx.cbWndExtra    = 0;
WndClsEx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
WndClsEx.hCursor       = LoadCursor(NULL, IDC_ARROW);
WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClsEx.lpszMenuName  = NULL;
WndClsEx.lpszClassName = TEXT("Title");
WndClsEx.hInstance     = hInstance;
WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

RegisterClassEx(&WndClsEx);

hWnd = CreateWindowEx(  NULL,
            TEXT("Title"),
            TEXT("Title"),
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            640,
            480,
            NULL,
            NULL,
            hInstance,
            NULL);

Assuming simple math won't quite solve the problem, how do I take the chrome size into account?

假设简单的数学不能完全解决问题,我该如何考虑镀铬尺寸?

Note:I'm using SDL after creating the window, but I'm guessing it's bound to the window size and makes no difference to its size.

注意:我在创建窗口后使用 SDL,但我猜它绑定到窗口大小并且对其大小没有影响。

回答by Ferruccio

You can use the AdjustWindowRector AdjustWindowRectExfunction to calculate the window size given a desired client area size.

您可以使用AdjustWindowRectAdjustWindowRectEx函数来计算给定所需客户区大小的窗口大小。