Console Cursor,Font

控制台游標與字體

名詞解釋

COORD

windows.h裡的一個結構,在主控台螢幕緩衝區中定義字元資料格的座標。 座標系統的原點 (0,0) 位於緩衝區的左上角儲存格。

HANDLE

HANDLE「句柄」:能夠從一個數據拎起一大堆東西稱之為HANDLE,是Windows使用32位元無號整數對應的對象。

(可否視為一個物件?)

Cursor

#include:

<windows.h>


語法:黑色為保留字、紫色為自定名稱

使用:HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //HANDLE賦值為GetStdHandle

函數:GetStdHandle(STD_OUTPUT_HANDLE) 為標準輸出句。※

宣告:COORD pos = {3, 6};

設定:SetConsoleCursorPosition(hConsole, pos);

屬性、給值:

pos.X = 整數;

pos.Y = 整數;

pos = {整數,整數};


範例一

#include<iostream>

#include<windows.h>


main(){

char name[10]={""};

int n;

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

COORD pos = {3, 6};//跳至(3,6)

SetConsoleCursorPosition(hConsole, pos);//設置游標

WriteConsole(hConsole, "Hello,Your name please:", 23, NULL, NULL);

scanf("%s",name);

pos = {3,2};//跳至(3,2)

SetConsoleCursorPosition(hConsole, pos);//設置游標

WriteConsole(hConsole, "Jump to (3,2):", 14, NULL, NULL);

scanf("%d",&n);

return 0;

}

同場加映:輸出使用方式 WriteConsole(hConsole, "文字", 文字長度, NULL, NULL);

範例一執行結果

範例二

#include <iostream>

#include <windows.h>

#include <string>


struct userInfo //輸入結構

{

char name[20]; //姓名

int age; //年齡

char gender; //性別

};


void placeCursor(HANDLE, int, int);

void displayPrompts(HANDLE);


int main()

{

userInfo input;//宣告一個輸入結構

HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);

displayPrompts(screen);

//input

placeCursor(screen, 5, 31);

scanf("%s",input.name);

placeCursor(screen, 7, 31);

scanf("%d",&input.age);

getchar();//濾除Enter

placeCursor(screen, 7, 48);

scanf("%c",&input.gender);

//display

placeCursor(screen, 11,0);

printf("Here is the data you entered.\n");

printf("Name : %s\n",input.name);

printf("Age : %d\n",input.age);

printf("Gender : %c\n",input.gender);

//

return 0;

}


void placeCursor(HANDLE screen, int row, int col)

{

COORD position; // holds a pair of X and Y coordinates

position.Y = row;

position.X = col;

SetConsoleCursorPosition(screen, position);

}


void displayPrompts(HANDLE screen) //顯示輸入表單

{

placeCursor(screen, 3, 25);

printf("******* scanf() 輸入表單 *******\n");

placeCursor(screen, 5, 25);

printf("姓名: \n");

placeCursor(screen, 7, 25);

printf("年齡: 性別(M/F):\n");

placeCursor(screen, 9, 25);

printf("******* 張庭禎編輯修改版 *******\n");

}

範例二執行畫面

範例執行結果

控制游標停於「姓名」、「年齡」、「性別」之後。

呼叫Windows.hsystem()的差別:system()相較於Windows.h耗資源且不安全,所以建議使用Windows.h

參考網址:https://stackoverflow.com/questions/54250401/how-to-control-a-cursor-position-in-c-console-application

Font

#include:

<windows.h>

<cwchar>

語法:

使用:CONSOLE_FONT_INFOEX

宣告:CONSOLE_FONT_INFOEX 自訂名稱;

設置SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);

函數:GetStdHandle(STD_OUTPUT_HANDLE) 為標準輸出句

屬性:

.cbSize

.nFont

.dwFontSize.X

.dwFontSize.Y

.FontFamily

.FontWeight


範例三

CONSOLE_FONT_INFOEX cfi;

cfi.cbSize = sizeof(cfi);

cfi.nFont = 0;

cfi.dwFontSize.X = 0; //每一個字元的寬度

cfi.dwFontSize.Y = 24; //高度

cfi.FontFamily = FF_DONTCARE;

cfi.FontWeight = FW_NORMAL;

std::wcscpy(cfi.FaceName, L"Consolas"); //字型選擇

SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);

std::cout << "Font: Consolas, Size: 24\n";

std::wcscpy:

※字體大小不會影響上面Cursor的COORD.X、COORD.Y位置計算。

範例執行結果

Console字體變大、螢幕大小也會變大。