Re: Code Page problem in SetWindowText
"Marco Hung" <marco.hungmarco@gmail.com> ha scritto nel messaggio
news:ucAsGC37HHA.2208@TK2MSFTNGP06.phx.gbl...
I've created a MFC project in MBCS. I need to show some set special
characters
( ASCII code > 128) in a CStatic controls. It shows correctly in all
English locale window.
However all those special character becames "?" in non-English window.
How to solve this problem?
Hi,
This is a classical example of the importance of using *Unicode* to store
characters and strings.
IMHO, you should forget about ANSI (or MBCS), and consider *Unicode* as the
type for characters and strings (like modern programming languages like
Java, Python, C#, etc. do).
Basically, Unicode provides *unique* number for every character, no matter
what the programming language, or the operating system, etc.
I don't know what character you want to display, but e.g. suppose that you
want to display a lower-case Greek "omega" (kind of "w").
In Unicode UTF-16 encoding, the "unique number" associated to this character
is 0x03C9 (hex, note that its 16 bits, not 8 bits like for ANSI).
The C++ code to display that character in a message-box is like so:
// Build a string of Unicode UTF-16 characters:
// "omega" (0x03C9), end-of-string (0x0000)
wchar_t omega[] = { 0x03C9, 0x0000 };
// Display Unicode text (note the W and the L)
MessageBoxW( NULL, omega, L"Unicode Test", MB_OK );
The L before "Unicode Test" string literal identifies this string as Unicode
and not ANSI.
The W after MessageBox is a Win32 API naming convention to identify the
Unicode (and not the ANSI) version of MessageBox API.
If you compile in Unicode mode, you can avoid the W and just write
MessageBox; the C/C++ preprocessor will expand MessageBox as MessageBoxW.
You might find the Unicode FAQ http://unicode.org/faq/ and Mihai's blog
http://www.mihai-nita.net/ to be both interesting.
Giovanni