问题出现在WM_PAINT里,具体代码如下:
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
SelectObject(hdc, GetStockObject(BLACK_PEN));
SelectObject(hdc, GetStockObject(WHITE_BRUSH));
//BLOCK是宏定义的常量
Rectangle(hdc, 0, 0, 25 * BLOCK, 50 * BLOCK);
TextOut(hdc, 26 * BLOCK, 5 * BLOCK, text1, sizeof(text1));
TextOut(hdc, 26 * BLOCK, 15 * BLOCK, text2, sizeof(text2));
EndPaint(hwnd, &ps);
break;
其中
const char *text1 = _T("下一个:");
const char *text2 = _T("得分:");
运行结果如下
原本text1后面也出现上图中的乱码,我把TextOut()最后一个参数的值+1再运行就没了,可是text2用同样方法就不行,把sizeof改为lstrlen就不会出现乱码了,求解其中原因.
黄舟2017-04-17 14:28:34
TextOut
The last parameter is the length of the string. The macro constant you define is added with _T
. If you choose the UNICODE
character set to compile, the corresponding string length must be "how many" UNICODE characters" to describe, so it must be lstrlen
to calculate correctly.
You can use sizeof
to calculate the number of bytes. The length of a UNICODE
character is not 1 byte, so the value you calculate using sizeof will be greater than the actual number of UNICODE
characters. TextOut
got this error The length of the string will display the data after the string as characters - that is the garbled code you see.
Another: If you do not specify the UNICODE
character set for compilation and encode according to ANSI
, 1 character is exactly equal to 1 byte. The displayed content may be correct, but the calculation of sizeof
is still not the number of characters, because the following There is also a