在英文版windows10下的控制台程序如何显示中文?

OK_boom 2017-09-05 12:10:50
mfc界面的中文都可以正常显示, 为什么控制台的怎么都搞不来. 全是??问号.

#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include "StrUtils.h"
using namespace std;
int main() {
setlocale(LC_ALL, ".936");
//system("chcp 65001");
string lvStr("hello,你们好");
cout << lvStr.data() << endl;
/*string lvUtf8 = freestyle::StrUtils::GBK_To_UTF8(lvStr);
cout << " aaa " << endl;
cout << lvUtf8.data() << endl;
cout << " bbb " << endl;
wcout << freestyle::StrUtils::UTF8ToWString(lvUtf8.data()).data() << endl; */
wcout << L"中文" << endl;
getchar();
}

...全文
1473 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
一枝花秋月夜 2019-08-23
  • 打赏
  • 举报
回复
你们发的关于win10看不懂,我是计划装个装个系统搜到大神,可否指教?小白,谢谢1!!
一枝花秋月夜 2019-08-23
  • 打赏
  • 举报
回复
大神可否请教个问题,关于win0
chixiang1111 2019-08-23
  • 打赏
  • 举报
回复
编译时应要求有中文,否则不行。
frequent 2018-08-11
  • 打赏
  • 举报
回复
两个办法:
1,在项目菜单下的属性里,把字符集改为“unicode字符集”

2,在控制面板的点击“Change Data time, or number.......”

再点击Administrative, Change System Locale,再改为简体中文就可以了。


汪宁宇 2018-07-14
  • 打赏
  • 举报
回复
需要用多语言的来做的
赵4老师 2018-07-06
  • 打赏
  • 举报
回复
仅供参考:
//控制台的字体大小是可以改变的。系统为我们提供了许多的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
//);
蒋晟 2018-07-05
  • 打赏
  • 举报
回复
可以在安装程序里上注册表改控制台字体设置和安装中文字体……不过工厂的管理员来做这个容易多了,一个应用来改这个设置不怎么合适。

你确定生产线上的电脑不能由网络管理员定制?那网络管理员不是啥都不能干?
OK_boom 2018-07-03
  • 打赏
  • 举报
回复
引用 21 楼 jiangsheng 的回复:
需要
1 使用WriteConsoleW
2 用户需要设置控制台字体为支持Unicode字符的字体。英文Windows 10上需要安装中文语言包。
对于CRT来说,调用WriteConsoleW需要
1 _setmode(_fileno(stdout), _O_U16TEXT/_O_U8TEXT);+_setmode(_fileno(stderr), _O_U16TEXT);(需要VS2005或更高版本)
2 使用wprintf之类的宽字符支持函数/类
3 将源文件保存为_setmode里面指定的字符编码(Visual Studio默认保存编码不是Unicode)。

GetConsoleOutputCP/SetConsoleOutputCP只影响WriteConsoleA,不影响WriteConsoleW 。使用WriteConsoleA的话,你的程序的输出的编码必须匹配控制台的输出编码,不然就会有乱码。


纯英文windows10系统 ,不能更改任何设置, 按你的建议能否做到?
OK_boom 2018-07-03
  • 打赏
  • 举报
回复
引用 20 楼 weixin_36023497 的回复:
其实只要将win10 “非unicode的程序语言" 改成中文即可。具体步骤如下:

控制面板----时钟 语言 区域----更改位置(在区域栏下)----管理-----更改系统区域设置----将英语或其他语言设置成 中文

重启电脑就可以了。

在线上产品(外销电脑)里面运行的, 不能更改任何设置
lambyxqtr 2018-07-02
  • 打赏
  • 举报
回复
其实只要将win10 “非unicode的程序语言" 改成中文即可。具体步骤如下:

控制面板----时钟 语言 区域----更改位置(在区域栏下)----管理-----更改系统区域设置----将英语或其他语言设置成 中文

重启电脑就可以了。
蒋晟 2018-07-02
  • 打赏
  • 举报
回复
需要
1 使用WriteConsoleW
2 用户需要设置控制台字体为支持Unicode字符的字体。英文Windows 10上需要安装中文语言包。
对于CRT来说,调用WriteConsoleW需要
1 _setmode(_fileno(stdout), _O_U16TEXT/_O_U8TEXT);+_setmode(_fileno(stderr), _O_U16TEXT);(需要VS2005或更高版本)
2 使用wprintf之类的宽字符支持函数/类
3 将源文件保存为_setmode里面指定的字符编码(Visual Studio默认保存编码不是Unicode)。

GetConsoleOutputCP/SetConsoleOutputCP只影响WriteConsoleA,不影响WriteConsoleW 。使用WriteConsoleA的话,你的程序的输出的编码必须匹配控制台的输出编码,不然就会有乱码。
OK_boom 2017-09-13
  • 打赏
  • 举报
回复
引用 13 楼 zhao4zhong1 的回复:
你系统没安装华文楷体字体。
我改为simsun也不行.
OK_boom 2017-09-13
  • 打赏
  • 举报
回复
引用 12 楼 jennyvenus 的回复:
像我这种笨人来解决,肯定直接不用中文。
自家用肯定是了, 但是这个app是给产线上用的, 你指望那些工人会看英文提示?
Eleven 2017-09-13
  • 打赏
  • 举报
回复
WriteConsole试试~
赵4老师 2017-09-13
  • 打赏
  • 举报
回复
hfont = CreateFont(48, 0, 0, 0, 0, 0, 0, 0, GB2312_CHARSET, 0, 0, 0, 0, "宋体");
赵4老师 2017-09-13
  • 打赏
  • 举报
回复
改为“宋体”再试试?
赵4老师 2017-09-10
  • 打赏
  • 举报
回复
你系统没安装华文楷体字体。
zwfgdlc 2017-09-10
  • 打赏
  • 举报
回复
加一行
SetConsoleCP(936)
试下
OK_boom 2017-09-09
  • 打赏
  • 举报
回复
老師,這已經脫離console的本意了,隨便refresh一下就消失, 而且還是亂碼.


引用 10 楼 zhao4zhong1 的回复:
这个总行了吧:
#pragma comment(lib,"user32")
#pragma comment(lib,"gdi32")
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
extern "C" 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;
}

设置项目使用多字节字符集。
用户 昵称 2017-09-09
  • 打赏
  • 举报
回复
像我这种笨人来解决,肯定直接不用中文。
加载更多回复(10)

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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