【问题标题】:Console Resolution program not accurate控制台解决方案不准确
【发布时间】:2019-09-09 22:10:33
【问题描述】:

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

在下面的代码中,首先我设置了我希望控制台在屏幕上启动的位置(at x100, y100),然后我尝试使用 r.right 和 @987654322 @ 来跟踪分辨率,我设置了新值,其中r.right = 700r.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 值,有人可以告诉我我在这里做错了什么吗?

【问题讨论】:

  • "resolution" 与 r.bottom/right 关系不大,这些变量只是告诉你窗口的右下角在哪里。窗口的宽度为 r.right - r.left。在显示控制台窗口 > 属性 > 布局选项卡时键入 Alt+Space。您不太关心“让系统定位窗口”复选框。
  • 天哪,我没注意到,谢谢汉斯!

标签: c++ winapi console-application


【解决方案1】:

你想要:

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

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

【讨论】:

  • 嗨@paulsaunders,这似乎有效,我之前尝试过这样做,但我认为我的计算可能有误,谢谢!
  • 试试MoveWindow(console, r.left, r.top, 700, 400, TRUE);
  • 抱歉,我刚刚编辑了我的评论,非常感谢!
猜你喜欢
  • 1970-01-01
  • 2014-12-02
  • 2011-10-18
  • 2022-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-20
  • 2012-03-28
相关资源
最近更新 更多