mfc 中的_T

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.

Example
CString 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);

Output
If 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.12
Left-justified integer: 000035
Penguins: 5

3.更改編碼格式

Project Properties - General - Project Defaults - Character Set
發佈了56 篇原創文章 · 獲贊 53 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章