c - Visual Studio 使用 Consolas 打开 cmd.exe

标签 c character-encoding console visual-studio-2012 cmd

我有一个用 C 编写的程序,它正在 Visual Studio 2012 下开发。
我的程序使用了 Raster 字体中不可用的字符,因此我将 cmd.exe 字体更改为 Consolas,并且我的字符(á、é、í 等)工作正常。

但是,当我打开我的程序时,控制台仍然是光栅字体。我不知道我应该在哪里更改以使 Consolas 被全局接受。

提前致谢。

最佳答案

这里是我从多个来源收集的一些代码。 允许我使用代码页,避免 unicode。 但是,wprintf() 等也应该可以工作。

目前只有 consolas 和 lucida console 可以工作。包括尝试获得相似的尺寸。

检查包括 nFont 索引的有效性检查。 这可能是不需要的,在这种情况下,您可以省略对 kernel32 例程 GetNumberOfConsoleFonts() 的未记录调用。

Hans 说它不能在 XP 上运行,docs假设最小 _WIN32_WINNT 是 0x500,这将包括 XP。还没试过。 简

static void strcpytoW(WCHAR *dst, const char *src)
{
    WCHAR c;
    while ((c=*src++)!=0) *dst++=c;
    *dst=0;
}

/*************************************/

typedef DWORD (WINAPI *FN_NUMCONSOLEFONT)();
FN_NUMCONSOLEFONT GetNumberOfConsoleFonts;
static bool setfont(const HANDLE h, char *facename, CONSOLE_FONT_INFOEX &in,CONSOLE_FONT_INFOEX &out)
{
    in.cbSize=out.cbSize=sizeof(in);
    strcpytoW(in.FaceName,facename);
    if (!SetCurrentConsoleFontEx(h,FALSE,&in)) return false;
    HMODULE hm = ::GetModuleHandleA("KERNEL32.DLL"); 
    if (!hm) return false;
    GetNumberOfConsoleFonts = (FN_NUMCONSOLEFONT) GetProcAddress(hm, "GetNumberOfConsoleFonts");
    if (!GetNumberOfConsoleFonts) return false;
    DWORD numConsoleFonts=GetNumberOfConsoleFonts();
    if (!GetCurrentConsoleFontEx(h,FALSE,&out)) return false;
    return out.nFont>=0 && out.nFont<numConsoleFonts && _wcsicmp(in.FaceName,out.FaceName)==0;
}

/*************************************/

static char *set_console(char *facename,int page)
{
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_FONT_INFOEX fi={sizeof(fi),-1,{0,0},54,700},newfi;
    CONSOLE_FONT_INFO curfi;
    if (!GetCurrentConsoleFont(hStdout,FALSE,&curfi)) return NULL;
    for (fi.dwFontSize=curfi.dwFontSize;;fi.dwFontSize.Y++)
    {
        if (!setfont(hStdout,facename,fi,newfi))
        {
            facename="Lucida Console";
            if (!setfont(hStdout,facename,fi,newfi)) return NULL;
        }
        if (newfi.dwFontSize.X>=curfi.dwFontSize.X) break;
    }
    if (!SetConsoleOutputCP(page)) return NULL;
    return facename;
}

/*************************************/

int main(int argc, char *argv[])
{
    char *faceused;
    if ((faceused=set_console("Consolas",1252))!=NULL)
        printf("Console succesful, using %s\n",faceused);
    else puts("Console failed");
    puts("\x86 \x87 \xa7 \xa9 \xae \xb1 \xbc");

关于c - Visual Studio 使用 Consolas 打开 cmd.exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13673951/

相关文章:

c - libpcap 捕获的信标帧有些奇怪

配置: error: C compiler cannot create executables (macOS: Big Sur)

json - swift : Wrong character in String

javascript - 使用 Ref react ComponentDidMount 生命周期事件

python - 如何在python中的控制台上打印颜色框?

C 宏一次性定义多个变量

c# - 如何在 C# 中轻松排序 XML 文件?

haskell - 如何使用 haskell PDF Easy Render 包对特殊字符进行编码?

mysql - 中文和日文字符不适用于 mysql

c++ - 在 qt widgets 应用程序中显示 cmd 终端