Internationalization Cookbook
This is my personal blog. The views expressed on these pages are mine alone and not those of my employer.

Problems using Unicode characters with RTF controls in dialogs

Q. I have an Unicode application, and a dialog with a RichText control. Although I can type and paste Japanese/Thai/Russian into it, when I change the text using the Windows API I get question marks.

A. This is a problem with the Visual Resource Editor. The RichEdit control is added with the "RichEdit20A" class instead of "RichEdit20W".

Let’s make a small experiment to show what the problem is:

  • Start Visual Studio
  • Select from the menu File -> New -> Project (or press Ctrl+Shift+N)
  • In the wizard dialog select Visual C++ Projects -> MFC -> MFC Application
  • Enter a project name and click Next
  • For Application Type select Dialog based and click Finish

Ok, now we have a dialog based application using MFC. Let’s add some meat:

  • Open the main dialog for editing in Visual Resource Editor.
  • Delete the “TODO” string
  • Add an Edit Control named IDC_EDIT
  • Add a Rich Edit 2.0 Control named IDC_RICHEDIT
  • Add a Button named IDC_BUTTON
  • Delete the “Ok” and “Cancel” buttons (you can quit by pressing Escape)
  • Double-click on the button to create the OnBnClickedButton() method, then add this code:
            CString	str;
            GetDlgItemText( IDC_EDIT, str );
            SetDlgItemText( IDC_RICHEDIT, str );
            SendDlgItemMessage( IDC_RICHEDIT, EM_REPLACESEL, TRUE, (LPARAM)(LPCTSTR)str);
  • Find the application’s InitInstance and add AfxInitRichEdit() or AfxInitRichEdit2() after AfxEnableControlContainer().
  • Go to the project Properties and in the General section change Character Set to Use Unicode Character Set.

Now we have everything we need in order to show what the problem is.

Compile the application and run it. Now, type anything in the edit control, then click the button. Everything works fine and two copies of the text in the edit control are copied to the RichEdit control.

But try to type some Japanese text, or any other text that does not match the system locale (if you cannot type, you can copy paste from some web site).

You get question marks. Why? The application is Unicode! You can type directly or you can paste in the RichEdit, everything is fine. But using the API does not seem to work!

First time I have seen this I could not believe it.

But the solution is simple. Open the .rc file in Notepad (or any other text editor) and find IDC_RICHEDIT in the dialog. You will notice that the class name is "RichEdit20A". Aha! The ANSI version of the class, although the application is Unicode. Change it to "RichEdit20W", save the file and recompile.

Try it again, now everything works fine.

Problem solved!

Leave a comment