返回列表 发帖

[其他] FontSize命令行字体设置工具

FontSize是一个命令行字体设置工具,无需修改注册表,减少杀软报毒率
链接: https://pan.baidu.com/s/1K0vC3YQE3Xyj4NUJYptdRw 提取码: e6s9

制作原因:Cfont不能在新版本控制台下使用(巨坑)

注意:旧版控制台用户 请使用 chcp 437 修改代码页 以获得最佳的使用体验!


例如:
chcp 437
FontSize 8 8COPY
源码开放是必须的(实在太弱了。。。)
#include <stdio.h>
#include <string.h>
#include <windows.h>
// #ref: https://docs.microsoft.com/en-us/windows/console/setcurrentconsolefontex
typedef struct _CONSOLE_FONT_INFOEX
{
        ULONG        cbSize;
        DWORD        nFont;
        COORD        dwFontSize;
        UINT        FontFamily;
        UINT        FontWeight;
        WCHAR        FaceName[LF_FACESIZE];
} CONSOLE_FONT_INFOEX, *PCONSOLE_FONT_INFOEX;
BOOL WINAPI SetCurrentConsoleFontEx
        (
                HANDLE                        hConsoleOutput,
                BOOL                        bMaximumWindow,
                PCONSOLE_FONT_INFOEX        lpConsoleCurrentFontEx
        );
// #ref: https://blog.csdn.net/hilavergil/article/details/78449291
WCHAR *charToWCHAR(char *s)
{
        int        w_nlen = MultiByteToWideChar(CP_ACP, 0, s, -1, NULL, 0);
        WCHAR        *ret = (WCHAR *) malloc(sizeof(WCHAR) * w_nlen);
        memset(ret, 0, sizeof(ret));
        MultiByteToWideChar(CP_ACP, 0, s, -1, ret, w_nlen);
        return ret;
}
int main(int argc, char *argv[])
{
        CONSOLE_FONT_INFOEX        cfi;
        HANDLE                        soh;
        int                        wid, hei;
        WCHAR                        *fac = L"Terminal";
        if(argc <= 2 || argc > 4) goto help;
        wid = atoi(argv[1]), hei = atoi(argv[2]);
        if(wid <= 0 || hei <= 0) goto help;
        if(argc == 4) fac = charToWCHAR(argv[3]);
        memset(&cfi, 0, sizeof cfi);
        cfi.cbSize = sizeof cfi;
        cfi.dwFontSize.X = wid;
        cfi.dwFontSize.Y = hei;
        if(fac != NULL) wcscpy(cfi.FaceName, fac);
        soh = GetStdHandle(STD_OUTPUT_HANDLE);
        if(!SetCurrentConsoleFontEx(soh, FALSE, &cfi))
        {
                fprintf(stderr, "Failed to set console font.\n");
                return 1;
        }
        else
                return 0;
help:
        printf("Usage: FontSize <Width> <Height> [FontName(default:Terminal)]\n");
        printf("用法: FontSize <宽度> <高度> [字体名称(默认字体:Terminal)]\n");
        return 0;
}COPY
如果无法下载附件,github链接
https://github.com/gtr-0000/FontSize
2

评分人数

本帖最后由 happy886rr 于 2018-11-17 22:21 编辑

好样的,虽然用了goto,但有模有样。前面就应该return,早点斩断。
1

评分人数

    • 0000: 感谢提醒!技术 + 1

TOP

40,43行的两个if不好合并
主要是不敢这样写
        if(argc <= 2 || argc > 4 || (wid = atoi(argv[1])) <= 0 || (hei = atoi(argv[2])) <= 0){
                printf(
                        "Usage: FontSize <Width> <Height> [FontName(default:Terminal)]\n"
                        "用法: FontSize <宽度> <高度> [字体名称(默认字体:Terminal)]\n"
                );
                return 0;
        }COPY

TOP

回复 3# 0000
逻辑要精练
if(
    (argc != 3 && argc != 4)     ||
    argv[1][0] == '-'            ||
    argv[2][0] == '-'
){
                printf("帮助说明");
                exit(1);
}COPY
1

评分人数

TOP

返回列表