如何获得 window 的客户端 rect 相对于 window 的非客户端 rect 的偏移量?

How to get the client rect offsets of a window relative to the non-client rect of that window?

我需要获取 window 的客户端 rect 相对于 window 的非客户端 rect 的偏移量。

我试过 GetClientRect 但这只会让客户端 rect 相对于客户端 rect 本身,意思是:rect.left = 0rect.top = 0rect.right = clientWidthrect.bottom = clientHeight.

那么,怎么做呢?

提前致谢。

正如 Jonathan Potter 评论的那样,我建议您使用 MapWindowPoints

可以将GetClientRect得到的rect传入MapWindowPoints,并将cPoints参数设置为2,得到坐标相对于你想要的window。

示例如下:

#include <Windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    static TCHAR szAppName[] = TEXT("hello windows");
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;
    if (!RegisterClass(&wndclass))
    {
        MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
    }

    hwnd = CreateWindow(szAppName,
        TEXT("the hello program"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL);
    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);

    while (GetMessageW(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessageW(&msg);
    }
    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    RECT rect;
    switch (message)
    {
    case WM_CREATE:
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        GetClientRect(hwnd, &rect);
        DrawTextW(hdc, (L"Hello,Windows"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
        MapWindowPoints(hwnd, HWND_DESKTOP, (LPPOINT)&rect, 2);
        EndPaint(hwnd, &ps);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}

调用MapWindowPoints后可以观察到rect的结果。它将原始坐标转换为相对于桌面的坐标。

听起来您希望 non-client 从 Window 的边缘偏移到客户端矩形中。即,左侧和顶部的内部前导像素以及右侧和底部的尾随像素。在这些偏移量(大小)内,如果您为对话框等 top-level window 这样做,您可以拥有标题栏、菜单栏和装饰客户端矩形的边缘。 Windows 不会为您计算这个,但它提供了您自己计算所需的一切。下面是一些示例代码,您可以使用它们从给定的 window 句柄执行此计算:

RECT rcNonCli = {0,0,0,0}; //calculate non-client offsets here
RECT rcWin = {0,0,0,0};
RECT rcClient = {0,0};
POINT ptClient = {0,0};
if (GetWindowRect(hwnd,&rcWin) && GetClientRect(hwnd,&rcClient) && ClientToScreen(hwnd,&ptClient))
{
    rcNonCli.left = ptClient.x - rcWin.left;
    rcNonCli.top = ptClient.y - rcWin.top;
    rcNonCli.right = rcWin.right - ptClient.x - rcClient.right;
    rcNonCli.bottom = rcWin.bottom - ptClient.y - rcClient.bottom;

    printf("nonclient offsets={%d,%d,%d,%d}\n",rcNonCli.left,rcNonCli.top,rcNonCli.right,rcNonCli.bottom);
}

说明: ClientToScreen() 为我们提供了客户区的起始坐标,提供了顶部和左侧的偏移量。一旦我们知道这一点,我们就可以从完整的 Window 大小(来自 GetWindowRect())、客户端大小(来自 GetClientRect())和 non-client 顶部和左侧偏移量中减去给我们剩下的就是底部和右边的偏移量。