在Windows中,SetCurrentConsoleFontEx会更改控制台的字体大小吗? [英] In Windows, Does SetCurrentConsoleFontEx change console's font size?

查看:721
本文介绍了在Windows中,SetCurrentConsoleFontEx会更改控制台的字体大小吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其他人推荐 SetCurrentConsoleFontEx函数,但我不知道如何将其应用于我的项目。



我只想更改某些文本的字体大小,而不是全部文本。



SetCurrentConsoleFontEx()是否更改控制台的字体大小?



还是还有其他改变方式吗?



如果有的话,请向我展示控制台功能和一个简单的示例。

解决方案

这里是使用SetCurrentConsoleFontEx更改控制台字体大小的示例。这会影响整个控制台窗口-就像乔亚希姆·皮尔博格(Joachim Pileborg)已经说过的那样,如果您想在单个控制台窗口中混合字体大小,这将无济于事。

p> #define _WIN32_WINNT 0x500
#include< Windows.h>

// PrintChars将ASCII字符发送到控制台输出
//用于演示。
//仅取决于Win32 API
静态void PrintChars(){
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD num_write;
static const char * cr_lf = \r\n;
for(char c =’’; c<’\x7f'; ++ c){
WriteFile(hStdout,& c,1,& num_write,NULL);
if(c%16 == 15)WriteFile(hStdout,cr_lf,2,& num_write,NULL);
}
WriteFile(hStdout,cr_lf,2,& num_writer,NULL);
}

// WaitEnter阻止执行,直到用户
//按下Enter键。
//仅取决于Win32 API
static void WaitEnter(){
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
字符缓冲区;
DWORD num_read;
do {
num_read = 0;
ReadFile(hStdin,& buffer,1,& num_read,NULL);
} while(num_read&& buffer!=‘\n’);
}

int main(){
//显示一些示例字符
PrintChars();

//等待用户查看当前字体的外观
WaitEnter();

//获取当前控制台屏幕缓冲区的句柄
HANDLE hcsb = CreateFileA( CONOUT $,GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL );

CONSOLE_FONT_INFOEX cfi = {sizeof(cfi)};

//用屏幕缓冲区的当前字体信息填充cfi
GetCurrentConsoleFontEx(hcsb,FALSE,& cfi);

//修改cfi中的字体大小
cfi.dwFontSize.X * = 2;
cfi.dwFontSize.Y * = 2;

//使用cfi设置屏幕缓冲区的新字体
SetCurrentConsoleFontEx(hcsb,FALSE,& cfi);

//等待用户看到差异,然后退出
WaitEnter();
CloseHandle(hcsb);
}


Other guys recommend the SetCurrentConsoleFontEx function but I don't know how to apply it to my project.

I want to change the font size of only some texts, not all texts.

Does SetCurrentConsoleFontEx() change the console's font size?

Or are there other ways to change it?

If there is, please show me the console function and a simple example.

解决方案

Here is an example of using SetCurrentConsoleFontEx to change the console's font size. This affects the entire console window -- so like Joachim Pileborg already said, if you want mixed font sizes in a single console window, this won't help you.

#define _WIN32_WINNT 0x500
#include <Windows.h>

// PrintChars sends ASCII characters to console output
// for demonstration purposes.
// depends only on Win32 API
static void PrintChars() {
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    DWORD num_written;
    static const char* cr_lf = "\r\n";
    for(char c=' '; c<'\x7f'; ++c) {
        WriteFile(hStdout, &c, 1, &num_written, NULL);
        if(c % 16 == 15) WriteFile(hStdout, cr_lf, 2, &num_written, NULL);
    }
    WriteFile(hStdout, cr_lf, 2, &num_written, NULL);
}

// WaitEnter blocks execution until the user
// presses the enter key.
// depends only on Win32 API
static void WaitEnter() {
    HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); 
    char buffer;
    DWORD num_read;
    do {
        num_read = 0;
        ReadFile(hStdin, &buffer, 1, &num_read, NULL);
    } while(num_read && buffer != '\n');
}

int main() {
    // Display some example characters
    PrintChars();

    // Wait for the user to see how the current font looks
    WaitEnter();   

    // Get a handle to the current console screen buffer
    HANDLE hcsb = CreateFileA("CONOUT$", GENERIC_WRITE | GENERIC_READ,
        FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 

    CONSOLE_FONT_INFOEX cfi = {sizeof(cfi)};

    // Populate cfi with the screen buffer's current font info
    GetCurrentConsoleFontEx(hcsb, FALSE, &cfi);

    // Modify the font size in cfi
    cfi.dwFontSize.X *= 2;
    cfi.dwFontSize.Y *= 2;

    // Use cfi to set the screen buffer's new font
    SetCurrentConsoleFontEx(hcsb, FALSE, &cfi);

    // Wait for the user to see the difference before exiting
    WaitEnter();
    CloseHandle(hcsb);
}

这篇关于在Windows中,SetCurrentConsoleFontEx会更改控制台的字体大小吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆