输出字的大小

qq_40609422 2017-10-13 11:29:14
就像一个简单的Hellow world,我怎么改变他输出的字的大小?
...全文
238 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-12-03
  • 打赏
  • 举报
回复
我贴的代码,无特殊说明,都是VC6下使用多字节字符集。
李弘宇 2017-12-02
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
仅供参考:
#pragma comment(lib,"user32")
#pragma comment(lib,"gdi32")
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
HWND WINAPI GetConsoleWindow();
void HideTheCursor() {
    CONSOLE_CURSOR_INFO cciCursor;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

    if(GetConsoleCursorInfo(hStdOut, &cciCursor)) {
        cciCursor.bVisible = FALSE;
        SetConsoleCursorInfo(hStdOut, &cciCursor);
    }
}
void ShowTheCursor() {
    CONSOLE_CURSOR_INFO cciCursor;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

    if(GetConsoleCursorInfo(hStdOut, &cciCursor)) {
        cciCursor.bVisible = TRUE;
        SetConsoleCursorInfo(hStdOut, &cciCursor);
    }
}
int main() {
    HWND  hwnd;
    HDC   hdc;
    HFONT hfont;

    system("color F0");
    system("cls");
    HideTheCursor();
    hwnd  = GetConsoleWindow();
    hdc   = GetDC(hwnd);
    hfont = CreateFont(48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "华文楷体");
    SelectObject(hdc,hfont);
    TextOut(hdc,10,10,"地球人都知道!",14);
    MoveToEx(hdc,5,5,NULL);
    LineTo(hdc,300,  5);
    LineTo(hdc,300, 60);
    LineTo(hdc,  5, 60);
    LineTo(hdc,  5,  5);
    DeleteObject(hfont);
    ReleaseDC(hwnd,hdc);
    getchar();
    system("color 07");
    system("cls");
    ShowTheCursor();
    return 0;
}
谢谢赵老师! 友情提示:在VS2017中,如果遇到char无法转化为lpcwstr的情况,可以用 —T()来处理
赵4老师 2017-10-13
  • 打赏
  • 举报
回复
再供参考:
//控制台的字体大小是可以改变的。系统为我们提供了许多的API,能更改字体大小的API正隐身其中。网上没有控制台字体大小的相关资料,
//关于控制台字体大小的教程是本博客独家放出。本文将教会你如何使用这些API,达到修改控制台字体大小的目的。
//一般控制台中每个输出缓冲区都有一个固定的字体。系统中的字体,每一种都有对应的序号,每种字体大小不同。想要修改为一种新的字体,
//就将这种字体的序号赋值给控制台缓冲区即可。一般来说,系统所有的字体总数不超过40个,因此循环获取信息时可以以这个为上限。
//首先放出需要用到的API列表:
//(如下大部分API都没有公布,至最近才公布了其中几个的详细信息)

//bool SetConsoleFont(HANDLE,DWORD)
//注意了,这个函数一旦调用,所有文字字体大小全部变化,窗口大小也变了。这个是用于设置字体大小
//第一个参数为输出句柄,第二个参数为新字体大小序号

//bool GetConsoleFontInfo(HANDLE,BOOL,DWORD,CONSOLE_FONT*)
//这个是用于获取字体信息
//第一个参数为输出句柄,第二个参数应设为0,第三个参数为欲获取信息的字体序号,第四个参数为CONSOLE_FONT结构体的指针,API将返回的信息放置在这个结构体里

//COORD GetConsoleFontSize(HANDLE,DWORD)
//这个是用于获取字体大小
//第一个参数为输出句柄,第二个参数为欲获取大小的字体序号,返回的COORD的两个成员就是字体的大小

//DWORD GetNumberOfConsoleFonts()
//这个是用于获取可用字体数
//返回可用字体总数

//bool GetCurrentConsoleFont(HANDLE,BOOL,CONSOLE_FONT*)
//这个是用于获取当前字体的信息
//第一个参数为输出句柄,第二个参数应设为0,第三个参数为CONSOLE_FONT结构体的指针,API将返回的信息放置在这个结构体里

//结构体CONSOLE_FONT:
//struct CONSOLE_FONT{ DWORD index; COORD dim;};
//第一个成员是字体的序号,第二个成员是字体的大小
//这是些未公布的API,所以需要动态载入,首先在全局声明:
#pragma comment(lib,"user32")
#pragma comment(lib,"gdi32")
#pragma comment(lib,"shell32")
#include <windows.h>
#include <Shlwapi.h>
#include <shlobj.h>
#include <stdio.h>
struct CONSOLE_FONT{ DWORD index; COORD dim;};
typedef BOOL  (WINAPI *PROCSETCONSOLEFONT         )(HANDLE, DWORD);
typedef BOOL  (WINAPI *PROCGETCONSOLEFONTINFO     )(HANDLE,BOOL,DWORD,CONSOLE_FONT*);
typedef COORD (WINAPI *PROCGETCONSOLEFONTSIZE     )(HANDLE,DWORD);
typedef DWORD (WINAPI *PROCGETNUMBEROFCONSOLEFONTS)();
typedef BOOL  (WINAPI *PROCGETCURRENTCONSOLEFONT  )(HANDLE,BOOL,CONSOLE_FONT*);
PROCSETCONSOLEFONT          SetConsoleFont;
PROCGETCONSOLEFONTINFO      GetConsoleFontInfo;
PROCGETCONSOLEFONTSIZE      GetConsoleFontSize;
PROCGETNUMBEROFCONSOLEFONTS GetNumberOfConsoleFonts;
PROCGETCURRENTCONSOLEFONT   GetCurrentConsoleFont;
extern "C" HWND WINAPI GetConsoleWindow();
int main(int argc, char *argv[]) {
    //然后在程序启动的时候进行载入:
    HMODULE hKernel32 = GetModuleHandle("kernel32");
    SetConsoleFont          = (PROCSETCONSOLEFONT         )GetProcAddress(hKernel32,"SetConsoleFont"         );
    GetConsoleFontInfo      = (PROCGETCONSOLEFONTINFO     )GetProcAddress(hKernel32,"GetConsoleFontInfo"     );
    GetConsoleFontSize      = (PROCGETCONSOLEFONTSIZE     )GetProcAddress(hKernel32,"GetConsoleFontSize"     );
    GetNumberOfConsoleFonts = (PROCGETNUMBEROFCONSOLEFONTS)GetProcAddress(hKernel32,"GetNumberOfConsoleFonts");
    GetCurrentConsoleFont   = (PROCGETCURRENTCONSOLEFONT  )GetProcAddress(hKernel32,"GetCurrentConsoleFont"  );
    //载入完毕之后,就可以进行编程对字体大小控制了。

    int NumOfFont,i,X=0,Y=0,IconIndex=0;
    char *IconFile=NULL;
    NumOfFont = GetNumberOfConsoleFonts();
    CONSOLE_FONT *fonts = new CONSOLE_FONT[NumOfFont];
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleFontInfo(hConsole, 0, NumOfFont, fonts);
    if (argc==1) {
        printf("Set Console FontSize and Icon. Designed by zhao4zhong1@163.com 2015-07-10\n");
        printf("Usage: %s [XxY] [IconFile] [IconIndex]\n",argv[0]);
        printf("May availible Font Size:\n X x  Y\n-- x --\n");
    }
    if (argc==2) {
        if (2!=sscanf(argv[1],"%dx%d",&X,&Y))
        IconFile=argv[1];
    } else if (argc==3) {
        if (2!=sscanf(argv[1],"%dx%d",&X,&Y)) {
            IconFile=argv[1];
            sscanf(argv[2],"%d",&IconIndex);
        } else {
            IconFile=argv[2];
        }
    } else if (argc==4) {
        sscanf(argv[1],"%dx%d",&X,&Y);
        IconFile=argv[2];
        sscanf(argv[3],"%d",&IconIndex);
    }
    if (IconFile) {
        HWND hwnd=GetConsoleWindow();
        if (hwnd) {
            HICON hIconS=NULL;
            if (1<=ExtractIconEx(IconFile,IconIndex,NULL,&hIconS,1)) {
                if (hIconS) {
                    SendMessage(hwnd,WM_SETICON,ICON_SMALL,(LPARAM)hIconS);
//                  DestroyIcon(hIconS);
                }
                SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_FLUSH, NULL, NULL);//更新任务栏上的图标
            }
        }
    }
    for (i=0;i<NumOfFont;i++) {
        fonts[i].dim = GetConsoleFontSize(hConsole, fonts[i].index);
//      if (argc==1) printf("%2d x %2d %d %d\n",fonts[i].dim.X,fonts[i].dim.Y,i,fonts[i].index);
        if (argc==1) printf("%2d x %2d\n",fonts[i].dim.X,fonts[i].dim.Y);
        if (fonts[i].dim.X == X && fonts[i].dim.Y == Y) {
            SetConsoleFont(hConsole, fonts[i].index);
            break;
        }
    }
    delete[] fonts;
    return 0;
}
//BOOL SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttributes);//设置前景背景颜色

//SetCurrentConsoleFontEx function
//
//Sets extended information about the current console font.
//
//BOOL WINAPI SetCurrentConsoleFontEx(
//  _In_ HANDLE               hConsoleOutput,
//  _In_ BOOL                 bMaximumWindow,
//  _In_ PCONSOLE_FONT_INFOEX lpConsoleCurrentFontEx
//);
//
//Parameters
//hConsoleOutput [in]
//    A handle to the console screen buffer. The handle must have the GENERIC_WRITE access right. For more information, see Console Buffer Security and Access Rights.
//bMaximumWindow [in]
//    If this parameter is TRUE, font information is set for the maximum window size. If this parameter is FALSE, font information is set for the current window size.
//lpConsoleCurrentFontEx [in]
//    A pointer to a CONSOLE_FONT_INFOEX structure that contains the font information.
//Return value
//    If the function succeeds, the return value is nonzero.
//    If the function fails, the return value is zero. To get extended error information, call GetLastError.
//Remarks
//    To compile an application that uses this function, define _WIN32_WINNT as 0x0500 or later. For more information, see Using the Windows Headers.
//Requirements
//    Minimum supported client
//    Windows Vista [desktop apps only]
//    Minimum supported server
//    Windows Server 2008 [desktop apps only]
//Header
//    Wincon.h (include Windows.h)
//Library
//    Kernel32.lib
//DLL
//    Kernel32.dll
//See also
//    Console Functions
//    CONSOLE_FONT_INFOEX

//CONSOLE_FONT_INFOEX structure
//
//Contains extended information for a console font.
//
//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;
//
//Members
//cbSize
//    The size of this structure, in bytes.
//nFont
//    The index of the font in the system's console font table.
//dwFontSize
//    A COORD structure that contains the width and height of each character in the font, in logical units. The X member contains the width, while the Y member contains the height.
//FontFamily
//    The font pitch and family. For information about the possible values for this member, see the description of the tmPitchAndFamily member of the TEXTMETRIC structure.
//FontWeight
//    The font weight. The weight can range from 100 to 1000, in multiples of 100. For example, the normal weight is 400, while 700 is bold.
//FaceName
//    The name of the typeface (such as Courier or Arial).
//Remarks
//    To obtain the size of the font, pass the font index to the GetConsoleFontSize function.
//Requirements
//    Minimum supported client
//    Windows Vista [desktop apps only]
//    Minimum supported server
//    Windows Server 2008 [desktop apps only]
//Header
//    Wincon.h (include Windows.h)

//GetCurrentConsoleFontEx function
//
//Retrieves extended information about the current console font.
//
//BOOL WINAPI GetCurrentConsoleFontEx(
//  _In_  HANDLE               hConsoleOutput,
//  _In_  BOOL                 bMaximumWindow,
//  _Out_ PCONSOLE_FONT_INFOEX lpConsoleCurrentFontEx
//);
赵4老师 2017-10-13
  • 打赏
  • 举报
回复
仅供参考:
#pragma comment(lib,"user32")
#pragma comment(lib,"gdi32")
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
HWND WINAPI GetConsoleWindow();
void HideTheCursor() {
    CONSOLE_CURSOR_INFO cciCursor;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

    if(GetConsoleCursorInfo(hStdOut, &cciCursor)) {
        cciCursor.bVisible = FALSE;
        SetConsoleCursorInfo(hStdOut, &cciCursor);
    }
}
void ShowTheCursor() {
    CONSOLE_CURSOR_INFO cciCursor;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

    if(GetConsoleCursorInfo(hStdOut, &cciCursor)) {
        cciCursor.bVisible = TRUE;
        SetConsoleCursorInfo(hStdOut, &cciCursor);
    }
}
int main() {
    HWND  hwnd;
    HDC   hdc;
    HFONT hfont;

    system("color F0");
    system("cls");
    HideTheCursor();
    hwnd  = GetConsoleWindow();
    hdc   = GetDC(hwnd);
    hfont = CreateFont(48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "华文楷体");
    SelectObject(hdc,hfont);
    TextOut(hdc,10,10,"地球人都知道!",14);
    MoveToEx(hdc,5,5,NULL);
    LineTo(hdc,300,  5);
    LineTo(hdc,300, 60);
    LineTo(hdc,  5, 60);
    LineTo(hdc,  5,  5);
    DeleteObject(hfont);
    ReleaseDC(hwnd,hdc);
    getchar();
    system("color 07");
    system("cls");
    ShowTheCursor();
    return 0;
}
das白 2017-10-13
  • 打赏
  • 举报
回复
输出到控制台的话 改变控制台字体的大小
qq_40609422 2017-10-13
  • 打赏
  • 举报
回复
非常感谢,虽然不是太懂

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧