【发布时间】:2021-04-25 14:03:33
【问题描述】:
使用console functions 来控制控制台程序的布局,但我无法更改其大小。
目前,我可以做的是禁用调整大小、删除按钮和更改缓冲区大小,但如果我尝试调整窗口本身的大小,所有尝试都会失败;虽然,有些函数确实会调整窗口大小,但以像素为单位,因此比例大幅下降。
到目前为止我所做的(不是 C++ 人,只是摆弄,在语法上放轻松):
#include <iostream>
#include <Windows.h>
using namespace std;
COORD conBufferSize = { 150, 40 };
SMALL_RECT conScreen = { 0, 0, 150, 40 };
CONSOLE_SCREEN_BUFFER_INFOEX csbie;
int main()
{
// console opened for application
HWND hwConsole = GetConsoleWindow();
// hide it
ShowWindow(hwConsole, SW_HIDE);
// get the style for it
DWORD style = GetWindowLong(hwConsole, GWL_STYLE);
// disable maximizing and minimizing and resizing
style &= ~(WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SIZEBOX);
SetWindowLong(hwConsole, GWL_STYLE, style);
HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
SetConsoleScreenBufferSize(hConsole, conBufferSize);
// this does nothing to the window itself as best I can tell
// if by "window" it means what portion of the display window you view "into"
// commented out here for functionality
// SetConsoleWindowInfo(hConsole, TRUE, &conScreen);
SetConsoleActiveScreenBuffer(hConsole);
// this sequence works, but seems by accident
// surely there is another method?
csbie.cbSize = sizeof(csbie);
GetConsoleScreenBufferInfoEx(hConsole, &csbie);
csbie.srWindow = conScreen;
SetConsoleScreenBufferInfoEx(hConsole, &csbie);
// required to update styles
// using the cx/cy parameters sets size in pixels
// that is much smaller than buffer size which accounts for font size
// therefore this "doesn't" work
SetWindowPos(hwConsole, HWND_TOP, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE| SWP_SHOWWINDOW);
// to look at the console until close
while (1) {
}
return 0;
}
现在,据我所知,如果我有一个 100 列乘 40 行的屏幕缓冲区,则不会直接转换为容纳缓冲区的窗口的大小。所以我的下一个想法是我需要确定当前控制台字体使用了多少像素,然后将缓冲区尺寸乘以确定像素大小并使用SetWindowPos 或SetConsoleScreenBufferInfoEx 方法。
我不确定的一件事是为什么srWindow 属性能够修改显示窗口,其描述与SetConsoleWindowInfo 的描述相似,但不会产生明显的变化。
【问题讨论】:
标签: c++ windows winapi console