C++ 从Window Rect计算客户端大小和位置

C++ 从Window Rect计算客户端大小和位置,c++,winapi,C++,Winapi,如何使用window rect获取客户端大小和位置?这可能吗?如果您有一个窗口矩形,左上角就是窗口位置,您也可以使用窗口的左上角和右下角计算大小 不确定您到底想了解什么。也许可以试试这样: #include <iostream> #include <windows.h> int main() { RECT r; HWND h = GetConsoleWindow(); // or whatever window needed GetWindow

如何使用window rect获取客户端大小和位置?这可能吗?

如果您有一个窗口矩形,左上角就是窗口位置,您也可以使用窗口的左上角和右下角计算大小


不确定您到底想了解什么。也许可以试试这样:

#include <iostream>
#include <windows.h>

int main()
{
    RECT r;
    HWND h = GetConsoleWindow(); // or whatever window needed

    GetWindowRect(h, &r);
    std::cout << "Relative Client X,Y: " << r.left << "," << r.top << std::endl;
    std::cout << "Relative Client W,H: " << r.right - r.left << "," << r.bottom - r.top << std::endl;

    GetClientRect(h, &r);
    std::cout << "Client X,Y: " << r.left << "," << r.top << std::endl;
    std::cout << "Client W,H: " << r.right - r.left << "," << r.bottom - r.top << std::endl;
}
和/或如果您想要获得相对于屏幕的客户端区域位置,可以使用函数。例如:

Relative Client X,Y: 100,100
Relative Client W,H: 947,594
Client X,Y: 0,0
Client W,H: 910,552
#include <windows.h>

int main()
{
    HWND h = GetConsoleWindow(); // or provided HWND
    POINT p{}; // defaulted to 0,0 which is always left and top of client area

    ClientToScreen(h, &p);
    SetCursorPos(p.x, p.y); // places cursor to the 0,0 of the client
}
#包括
int main()
{
HWND h=GetConsoleWindow();//或提供的HWND
点p{};//默认为0,0,始终位于客户区的左侧和顶部
客户端到屏幕(h和p);
SetCursorPos(p.x,p.y);//将光标放在客户端的0,0处
}
我找到了一个解决方案:

RECT GetClientRectFromWindowRect(HWND hWnd, RECT rect)
{
    RECT v = { 0 };
    AdjustWindowRectEx(&v, 
        GetWindowLong(hWnd, GWL_STYLE), 
        !!GetMenu(hWnd), 
        GetWindowLong(hWnd, GWL_EXSTYLE)
        );
    RECT ret = { 0 };
    ret.bottom = rect.bottom - v.bottom;
    ret.left = rect.left - v.left;
    ret.right = rect.right - v.right;
    ret.top = rect.top - v.top;
    return ret;
}

矩形只是左、右、上、下,它与任何特定的东西都没有连接。你能解释一下你想做什么吗?这个rect是从哪里来的?我需要从rect结构(如反向调整windowrectex)获取窗口客户端大小和位置,rect结构来自WM_WINDOWPOSCHANGING消息和LPARAM。该消息提供了一个包含hwnd的结构。你为什么不用它来获取你需要的额外数据呢?虽然你可以从结构中的x,y,w,h数据构造一个rect,但它也没有真正给你一个rect。它给了一个Window rect,我需要从Window rect构造一个“client”rect。向窗口发送一个
WM\NCCALCSIZE
消息。我需要一个client矩形,而不是Window矩形。如果你有Window句柄,您可以使用GetClientRect获得它。易碎的这不适用于所有的窗口样式/组合,并且无法解释包装的菜单栏。但是看到您正在将一个矩形
v
初始化为全部零,然后继续将所有坐标再次设置为零,您将面临更大的问题。同样完全不清楚的是,为什么我们要做最晦涩的杂技而不是简单地否定所有坐标。因为这是来自