我有一个程序,在win8显示正常,但是在Win10发生错位,程序里使用了一些特殊字符来显示,比如方块,应该是占两个格的,但是在Win10会错位,所以请教一下大家win8和Win10的控制台有什么区别。
仅供参考:
#pragma warning(disable:4996) //开头加这句或项目、属性、配置属性、C/C++、预处理器、预处理器定义中添加“_CRT_SECURE_NO_WARNINGS”
#pragma comment(lib,"user32")
#pragma comment(lib,"gdi32")
#pragma comment(lib,"shell32")
#include <windows.h>
#include <Shlwapi.h>
#include <shlobj.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
int X=8,Y=16,IconIndex=0;
char *IconFile=NULL;
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]);
}
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);//更新任务栏上的图标
}
}
}
CONSOLE_FONT_INFOEX cfi;
cfi.cbSize =sizeof(CONSOLE_FONT_INFOEX);
cfi.nFont =0;
cfi.dwFontSize.X=X;
cfi.dwFontSize.Y=Y;
cfi.FontFamily =48;
cfi.FontWeight =400;
if (X==8 && Y==16)
swprintf(cfi.FaceName,L"%s",L"Fixedsys");
else
swprintf(cfi.FaceName,L"%s",L"Terminal");
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetCurrentConsoleFontEx(hConsole,FALSE,&cfi);
return 0;
}
//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
//);
编码可能不同,可以在控制台的设置中查看