首页 > 解决方案 > 控制台解决方案不准确

问题描述

好的,所以我正在编写的程序有一些问题。我希望能够在运行程序时显示控制台窗口的精确分辨率。在我的程序运行时,如果我手动移动窗口的边缘之一以使其更小/更大,我希望更新分辨率。目前,我快到了,但我遇到了一些问题。

在下面的代码中,首先我设置我希望控制台在屏幕上启动的位置(在 x100, y100 处),然后我尝试使用r.rightr.bottom跟踪分辨率,并设置新值 wherer.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错误地使用了这个值,有人可以告诉我我在这里做错了什么吗?

标签: c++winapiconsole-application

解决方案


你要:

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

此外,缩进你的代码会很好。


推荐阅读