c++ - 控制台解决方案不准确
问题描述
好的,所以我正在编写的程序有一些问题。我希望能够在运行程序时显示控制台窗口的精确分辨率。在我的程序运行时,如果我手动移动窗口的边缘之一以使其更小/更大,我希望更新分辨率。目前,我快到了,但我遇到了一些问题。
在下面的代码中,首先我设置我希望控制台在屏幕上启动的位置(在 x100, y100 处),然后我尝试使用r.right
并r.bottom
跟踪分辨率,并设置新值 wherer.right = 700
和r.bottom = 400
,但是当我运行我的程序时:
相反,当我运行我的程序时,它
r.right
的值似乎是1249,然后如果我退出并再次运行,它的值是1119?与 相同r.bottom
,它的值是775,然后是645。当我只是移动窗口时,这些相同的值也会更新,我不希望它们更新,因为分辨率甚至没有改变。
这是我的代码:
#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';
此外,缩进你的代码会很好。
推荐阅读
- c# - 尝试创建抽象层以支持多个第三方作为文档管理系统
- r - 如何使用 felm() 指定工具变量模型?
- mysql - 填空 ID MySQL
- php - 如何从附件描述wordpress中获取peragraph的关键字?
- python - Pandas 强制对包含重复键的列进行一对一合并
- mysql - 如何在 AFTER UPDATE 触发器中引用最近更新的行
- c# - 如何在发布模式下激活时在调试模式下关闭 Visual Studio 中的 DocFX?
- javascript - 编辑记录时显示所选值
- php - 添加取消 URL - Mollie 付款
- android - 当我们使用 Firebase 创建 GTM 容器时,我们可以在 android 应用程序中使用多个 GTM 容器吗?