mfc 中的_T

来源:互联网 发布:侠盗猎车几windows xp 编辑:程序博客网 时间:2024/05/22 10:16

1.工业编程中字符串处理和编码一直是个大问题,最近刚好做一点工业编程的事,需要用到usb通信,接受字节数组,对于字符串处理,MFC有较好的处理机制,整理一下_T的用法。

#define _T(x) __T(x)#define _Text(x) __T(x)

2.mfc 中的字符串表示常用_T,意为text,定义为宏定义,可以方便的定义所有字符串为UNICODE或者ANSI

例如_T(“HELLO”);
字符串既可以表示为8位的ANSI也可以表示16位的Unicode。
如果对于所有的字符串定义了_T并且定义了预处理标志“_UNICODE”,所有的字符串便按照UNICODE编码,如果不定义,则按照_ANSI.

ExampleCString str;str.Format(_T("Floating point: %.2f\n"), 12345.12345);_tprintf("%s", (LPCTSTR) str);str.Format(_T("Left-justified integer: %.6d\n"), 35);_tprintf("%s", (LPCTSTR) str);str.Format(IDS_SCORE, 5, 3);_tprintf("%s", (LPCTSTR) str);OutputIf the application has a string resource with the identifier IDS_SCORE that contains the string "Penguins: %d\nFlyers  : %d\n", the above code fragment produces this output:Floating point: 12345.12Left-justified integer: 000035Penguins: 5

3.更改编码格式

Project Properties - General - Project Defaults - Character Set
0 0