C ++ string full guide - Win32 character encoding (2)

xiaoxiao2021-03-06  70

C string Complete Guide - Win32 character encoding (b) of: Translation: Ripple Category: VC / VC.NET Date: 2003-1-6 14:41:02

The two character sets of MBCS and Unicodeapi in Win32 API http://www.zdnet.com.cn/developer/tech/story/0,2000081602,39098306-3,00.htm Maybe you didn't notice, Win32 API There are two string processing functions in the message, one for the MCBS string, and the United States is a Unicode string. For example, there is no interface such as setWindowText () in Win32, but use the setWindowTexta () and setWindowTextW () functions. The suffix A indicates that the MBCS function is indicated by the suffix W (representing a wide character) indicated a Unicode function. When writing a Windows program, you can choose to use the MBCS or Unicode API interface function. When using the VC AppWizard Wizard, if the pre-processor setting is not modified, the MBCS function is used by default. But there is no setWindowText () function in the API interface, what is called? In fact, winuser.h header file do the following definition: BOOL WINAPI SetWindowTextA (HWND hWnd, LPCSTR lpString); BOOL WINAPI SetWindowTextW (HWND hWnd, LPCWSTR lpString); # ifdef UNICODE #define SetWindowText SetWindowTextW # else #define SetWindowText SetWindowTextA #ENDIF When writing MBCS applications, there is no need to define unicode, preprocessing to: #define setWindowText setWindowTexta then process setWindowText () to process as a real API interface function setWindowTexta () (if you want, you can call SetWindowTexta () or setWindowTextw () function. However, there are few things that have this need). If you want to change the default application interface to Unicode, remove the _mbcs tag to the pre-processing flag of the pre-processing setting, join Unicode and _unicode (two tags to join, different header files use different tags). However, it is necessary to handle a normal string at this time. If there is code: hwnd hwnd = getSomewindowHandle (); char sznewtext [] = "we love bo Bob!"; SetWindowText (hwnd, sznewtext); After the compiler is replaced with "setWindowText", the code becomes: hwnd hwnd = GetSomewindowHandle (); char sznewtext [] = "We LoveTextw (hwnd, sznewtext); seeing a problem, here is using a Unicode string handler to handle single-byte strings. The first solution is to use macro definition: hwnd hWnd = getSomewindowHandle (); # ifdef unicode wchar_t sznewtext [] = l "We love bob!"; # Else char sznewtext [] = "we love bob!"; # EndifsetWindowText HWND, SZNewtext; to do such a macro definition for every string, it is obviously a headache.

So use TCHAR to solve this problem: TCHAR's fire fire role TCHAR is a character type for MBCS and Unicode two codes. There is no need to use macro definitions everywhere. The macro of Tchar is as follows: #ifdef unicode typef wchar_t tchar; #ELSE TYPEDEF CHAR TCHAR; #ENDIF So in tchar is a char type in the MBCS program, which is a Wchar_t type in Unicode. For Unicode strings, there is a _t () macro, used to solve the L prefix: #ifdef unicode #define _t (x) l ## x # else #define _t (x) x # endif ## is a pre-processing calculation The child is attached to the two variables. No matter when the character string is used as a _T macro, you can add a l prefix in the Unicode encoding, such as tchar sznewtext [] = _t ("We love Bob!"); SETWINDOWTEXTA / W Function There are other hidden macros that can be used instead of strXxx () and _MBsxxx () string functions. For example, you can use the _TCSRCHR macro to Strrchr (), _ mbsrchr (), or WCSRCHR () function. _tcsrchr uses the right form function according to the code marked as _mbcs or Unicode, and the right function is correspondingly extended. Macro definition method is similar to SetWindowText. More than Strxxx () Functions have TCHAR macro definitions, there are also some other functions. For example, _stprintf (replace sprintf () and swprintf ()), and _TFOpen (replaces fopen () and _WFOpen ()). All macros of MSDN are defined under the "Generic-Text Routine Mappings" section. String and TCHAR Type Define the function names listed in the Win32 API file are common names (such as "SETWINDOTEXT"), all strings are processed in TCHAR type. (Except for XP, XP only uses Unicode types).

转载请注明原文地址:https://www.9cbs.com/read-90881.html

New Post(0)