【发布时间】:2019-09-09 22:10:33
【问题描述】:
好的,我正在编写的程序有一些问题。我希望能够在运行程序时显示控制台窗口的精确分辨率。在我的程序运行时,如果我手动移动窗口的一个边缘以使其更小/更大,我希望更新分辨率。目前,我快到了,但我遇到了一些问题。
在下面的代码中,首先我设置了我希望控制台在屏幕上启动的位置(at x100, y100),然后我尝试使用 r.right 和 @987654322 @ 来跟踪分辨率,我设置了新值,其中r.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 值,有人可以告诉我我在这里做错了什么吗?
【问题讨论】:
-
"resolution" 与 r.bottom/right 关系不大,这些变量只是告诉你窗口的右下角在哪里。窗口的宽度为 r.right - r.left。在显示控制台窗口 > 属性 > 布局选项卡时键入 Alt+Space。您不太关心“让系统定位窗口”复选框。
-
天哪,我没注意到,谢谢汉斯!
标签: c++ winapi console-application