控制台解析程序不准确
Console Resolution program not accurate
好的,我在编写程序时遇到了一些问题。当我 运行 我的程序时,我希望能够显示控制台 window 的精确分辨率。虽然我的程序是 运行ning,但如果我手动移动 window 的边缘之一使其成为 smaller/bigger,我希望更新分辨率。目前,我快完成了,但我遇到了一些问题。
在下面的代码中,首先我要设置控制台在屏幕上的启动位置(在 x100,y100),然后我尝试使用 r.right
和 r.bottom
来跟踪分辨率,我设置了新值,其中 r.right = 700
和 r.bottom = 400
,但是当我 运行 我的程序时:
相反,当我 运行 我的程序时,r.right
似乎具有 1249 的值,然后如果我退出并 运行,它的值将是 1119?与 r.bottom
相同,它的值为 775,然后为 645.
当我简单地移动 window 时,这些相同的值也会更新,但我不希望它们更新,因为分辨率甚至没有改变。
这是我的代码:
#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';
此外,缩进代码会很好。
好的,我在编写程序时遇到了一些问题。当我 运行 我的程序时,我希望能够显示控制台 window 的精确分辨率。虽然我的程序是 运行ning,但如果我手动移动 window 的边缘之一使其成为 smaller/bigger,我希望更新分辨率。目前,我快完成了,但我遇到了一些问题。
在下面的代码中,首先我要设置控制台在屏幕上的启动位置(在 x100,y100),然后我尝试使用 r.right
和 r.bottom
来跟踪分辨率,我设置了新值,其中 r.right = 700
和 r.bottom = 400
,但是当我 运行 我的程序时:
相反,当我 运行 我的程序时,
r.right
似乎具有 1249 的值,然后如果我退出并 运行,它的值将是 1119?与r.bottom
相同,它的值为 775,然后为 645.当我简单地移动 window 时,这些相同的值也会更新,但我不希望它们更新,因为分辨率甚至没有改变。
这是我的代码:
#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';
此外,缩进代码会很好。