如何解决如何在Windows控制台中以红色和黑色显示这些卡牌西装符号?
我有这一系列的西服套装。
char *suits[4] = {"♥","♦","♣","♠"};
为了能够在Windows控制台中看到它们,我必须编写:
SetConsoleOutputCP(65001);
有没有办法为这些西装设置颜色?
具体来说,红色代表♥和♦,黑色代表♣和♠。
解决方法
由于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"
,
尝试一下!
之所以看不到它们,是因为通常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;
}
- _setmode文档:https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setmode?view=vs-2019
- wprintf文档:http://www.cplusplus.com/reference/cwchar/wprintf/
- 对于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]0x2660;0..4^|? ^{if ($_ -le 1) ^{write-host $suits[$_] -ForegroundColor Red -NoNewline^} else ^{write-host $suits[$_] -ForegroundColor Black -NoNewline^}^}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。