Win32客户端大小和不正确的大小

时间:2014-09-16 21:52:43

标签: c++ windows winapi

所以对于一个游戏项目我正在创建一个游戏窗口(当然),但我只知道窗口的客户端大小而不是实际大小。我知道如何计算大小,但是当我测试查看客户区的大小(应该与输入相同)时,我得到的结果不正确。

void NewWindow(width, height)
{
    // Code setting things up here.
        if (!fullscreen) // Convert client size to window size.
        {
            style = WS_OVERLAPPEDWINDOW;

            RECT r = RECT();
            r.top = 0;
            r.bottom = height;
            r.left = 0;
            r.right = width;

            AdjustWindowRect(&r, WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX |     WS_MAXIMIZEBOX, FALSE);

            w = r.right;
            h = r.bottom;
        }

        hWnd = CreateWindow(CLASS_NAME, title, style, CW_USEDEFAULT, CW_USEDEFAULT, w, h, NULL, NULL, GetModuleHandle(NULL), (LPVOID)NULL);
}

获取客户端大小的代码是:

Size Game::GetClientSize()
{
    RECT r = RECT();
    GetClientRect(hWnd, &r);

    return Size(r.right - r.left, r.bottom - r.top);
}

我不确定问题出在哪里,因为AdjustWindowRect似乎输出的值大于我输入的值...

谢谢, 菲利普

3 个答案:

答案 0 :(得分:4)

传递给AdjustWindowRect的样式应与传递给CreateWindow的样式相同。否则他们会不匹配。您还需要将宽度计算为右 - 左,高度作为底部 - 顶部,因为调整后的矩形通常相对于客户区域的原点具有负原点。

答案 1 :(得分:3)

当然可以; AdjustWindowRect()对矩形进行膨胀,为您指定的边框和标题栏添加空间;在您的情况下,左侧和顶部值(可能)变为负值,因此右侧和底部不表示宽度和高度。使用r.right - r.left和r.bottom - r.top计算宽度和高度,就像在第二个示例函数中一样。

答案 2 :(得分:-1)

HWND windowHandle = NULL;
WNDCLASS wndClass;
wndclassex.style = CS_HREDRAW | CS_VREDRAW;
wndclassex.lpfnWndProc = WndProc;
wndclassex.cbClsExtra = 0;
wndclassex.cbWndExtra = 0;
wndclassex.hInstance = hInstance;
wndclassex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclassex.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclassex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclassex.lpszMenuName = NULL;
wndclassex.lpszClassName = L"Wnd";
RegisterClass(&wndClass);
RECT rt = {0, 0, 640, 480};
AdjustWindowRect(&rt, WS_OVERLAPPEDWINDOW, FALSE);
windowHandle = CreateWindow(L"Wnd", L"Wnd", WS_OVERLAPPEDWINDOW, 10, 10, rt.right - rt.left, rt.bottom - rt.top, NULL, NULL, hInstance, NULL); 
ShowWindow(windowHandle, SW_SHOW);