|
|
Title |
Building a UNICODE application.
|
Summary |
Working with languages other than English can present some problem challenges when the character size is 16 bits rather than 8 bits. This is the case with some Asian languages such as Chinese. Using wide charactors resolves this problem. |
Contributor |
John McTainsh
|
Published |
30-Jan-2001 |
Last updated |
30-Jan-2001 |
|
|
Introduction
The document discusses the use of UNICODE to produce international (I18N)
applications. This type of application is necessary when using 16 bit character
sets such as those used in Chinese languages.
Getting started
To take advantage of the MFC and C run-time support for Unicode, you need to:
Define _UNICODE
Under C/C++ tab in Project settings, Category: General. Remove _MBCS and add
_UNICODE to the list of Preprocessor Definitions.
Specify entry point
In the Output category of the Link tab in the Project Settings dialog box,
set the Entry Point Symbol to wWinMainCRTStartup .
Install correct librarys
The default installtion of VC++ does not install the unicode librarys. To get
these librarys to must run the Visual Studio setup and drill down into VC
installed components to install the correct libraries.
- Microsoft Visual C++
- VC++ MFC and Template Libraries
- MS Foundation Class Libraries
- (** Get the lot **)
Use ¢Îortable¢Îrun-time functions and types
Use the proper C run-time functions for Unicode string handling. You
can use the wcs family of functions, but you SHOULD use the fully ¢Îortable¢Î
(internationally enabled) _TCHAR macros. These macros are all prefixed with
_tcs ; they substitute, one for one, for the str family of functions.
Handle literal strings properly
The Visual C++ compiler interprets a literal string coded as L"this is a
literal string" to mean a string of Unicode characters. You can use the same
prefix for literal characters. Use the _T macro to code literal strings
generically, so they compile as Unicode strings under Unicode or as ANSI strings
(including MBCS) without Unicode. For example, instead of:
pWnd->SetWindowText( ¢Îello¢Î);
use:
pWnd->SetWindowText( _T(¢Îello¢Î );
With _UNICODE defined, _T translates the literal string to the L-prefixed form;
otherwise, _T translates the string without the L prefix.
Tip The _T macro is identical to the _TEXT macro.
Displaying chinese charactors
To display chinese charactors requires the correct font to be installed. Two
methods are to be used depending on what operating system you are using.
Running Chinese WinNT
Under Chinese NT you simply create a UNICODE application
is display a character such as;
WCHAR szOut[255];
szOut[0] = 0x58C6;
szOut[1] = 0x6A4E;
szOut[2] = 0x6FB8;
szOut[3] = 0x5673;
szOut[4] = NULL;
SetDlgItemText( IDC_EDIT, szOut );
Running non Chinese WinNT
Under this operating system it is necessary to specifiy a
font to display the chararctors as follows;
//Get the default font height and Define new font
CFont* pfont = GetDlgItem( IDC_EDIT_IN )->GetFont();
LOGFONT LogFont;pfont->GetLogFont( &LogFont );
bool b = m_fontChinese.CreatePointFont(
LogFont.lfHeight*(10),
_T("MingLiU") );
GetDlgItem( IDC_EDIT )->SetFont( &m_fontChinese );
//^^^^ Above code in InitDialog^^^^^
//Some where in the application..
WCHAR szOut[255];
szOut[0] = 0x58C6;
szOut[1] = 0x6A4E;
szOut[2] = 0x6FB8;
szOut[3] = 0x5673;
szOut[4] = NULL;
SetDlgItemText( IDC_EDIT, szOut );
Using conversion Macros
When using WCHAR or TCHAR and
char strings together in the same application it is often necessary to
convert between these types. This is often the case when data comes from
a socket connection or an ASCI file.
USES_CONVERSION;
char szSmall[] = "small string";
TCHAR* szWide = A2T( szSmall );
The above code takes an ANSI string and converts it into a char
if compiled with _MBCS and into a wchar if compiled with
_UNICODE . The USES_CONVERSIONS macro ensures
the created string is cleaned up is necessary.
Handy referances
The following articles have some good information on this topic
Article ID |
Title |
Q138813 |
How to Convert from ANSI to Unicode & Unicode to ANSI for OLE
|
TN059 |
Using MFC MBCS/Unicode Conversion Macros |
|