我有一系列的纸牌套装。
char *suits[4] = {"♥","♦","♣","♠"};为了能够在Windows控制台中看到它们,我必须这样写:
SetConsoleOutputCP(65001);有没有办法设置这些西装的颜色?
具体来说,红色代表♥和♦,黑色代表♣和♠。
发布于 2020-09-30 04:35:16
由于PowerShell已标记,因此您可以使用Write-Host将这些带有颜色的符号打印到控制台
$suits = "♥","♦","♣","♠"
Write-Host $($suits[0..1]) -ForegroundColor Red; Write-Host $($suits[2..3]) -ForegroundColor Black在cmd shell中运行它,就意味着调用PowerShell.exe来运行以下代码:
powershell.exe -command "$suits = '♥','♦','♣','♠'; Write-Host $($suits[0..1]) -ForegroundColor Red; Write-Host $($suits[2..3]) -ForegroundColor Black"发布于 2020-09-30 05:06:52
尝尝这个!
您看不到它们的原因是,通常cmd控制台不支持杂项符号(这是Unicode标准中的特殊符号)。但是,如果直接在命令提示符下运行,则可以看到它们。您可以尝试使用此代码,也可以在此处阅读_setmode && wprintf文档!♥
// crt_setmodeunicode.c
// This program uses _setmode to change
// stdout to Unicode. Cyrillic and Ideographic
// characters will appear on the console (if
// your console font supports those character sets).
#include <fcntl.h> //file control library
#include <io.h> //IO parameter
#include <stdio.h>
int main(void) {
_setmode(_fileno(stdout), _O_U16TEXT); //We're recibing the stdout file descriptor and then we change
//it to the Unicode translation.
wprintf(L" CLUB: \x2663 \n\n DIAMOND: \x2666 \n\n HEART: \x2665 \n\n SPADES: \x2660\n");
return 0;
}https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setmode?view=vs-2019
发布于 2020-09-30 06:04:00

用于Powershell的
$suits = [char]0x2665,[char]0x2666,[char]0x2663,[char]0x2660;
0..4|% {if ($_ -le 1){
write-host $suits[$_] -ForegroundColor Red –NoNewline;
} else { write-host $suits[$_] -ForegroundColor Black –NoNewline}}

用于cmd的
powershell -nop -c $suits=[char]0x2665,[char]0x2666,[char]0x2663,[char]0x2660;0..4^|? ^{if ($_ -le 1) ^{write-host $suits[$_] -ForegroundColor Red -NoNewline^} else ^{write-host $suits[$_] -ForegroundColor Black -NoNewline^}^}https://stackoverflow.com/questions/64126796
复制相似问题