控制台解析程序不准确

Console Resolution program not accurate

好的,我在编写程序时遇到了一些问题。当我 运行 我的程序时,我希望能够显示控制台 window 的精确分辨率。虽然我的程序是 运行ning,但如果我手动移动 window 的边缘之一使其成为 smaller/bigger,我希望更新分辨率。目前,我快完成了,但我遇到了一些问题。

在下面的代码中,首先我要设置控制台在屏幕上的启动位置(在 x100,y100),然后我尝试使用 r.rightr.bottom 来跟踪分辨率,我设置了新值,其中 r.right = 700r.bottom = 400,但是当我 运行 我的程序时:

这是我的代码:

#include <iostream>
#include <Windows.h>
#include <thread>

using namespace std;

int main() {

HWND console = GetConsoleWindow();
RECT r;

int counter = 0;


GetWindowRect(console, &r);

//                  pos(x)  pos(y)     width    height 
MoveWindow(console, 100, 100, r.right, r.bottom, TRUE);

r.right = 700;
r.bottom = 400;

while (1) {

    GetWindowRect(console, &r);

    std::cout << "Loops: " << counter << '\n';
    std::cout << "Resolution (x): " << r.right << '\n';
    std::cout << "Resolution (y): " << r.bottom << '\n';
    std::cout << "Position (x): " << r.left << '\n';
    std::cout << "Position (y): " << r.top << '\n';

    std::cout << "---------" << '\n';
    std::this_thread::sleep_for(chrono::seconds(1));

    ++counter;
}

} 

我觉得我使用这个 r 值不正确,有人可以告诉我我在这里做错了什么吗?

你想要:

std::cout << "Resolution (x): " << r.right - r.left << '\n';
std::cout << "Resolution (y): " << r.bottom - r.top << '\n';

此外,缩进代码会很好。