2
votes

I have Multi-Byte Char Set MFC windows application. Now I need to display international single byte ASCI characters on windows controls. I can't use ASCI characters directly because to display them correctly it is required windows locale be set to adequate country. I need to display characters in all windows locale cases. For this purpose I must convert ASCI to unicode. I can display required international characters in MessageBoxW, but how to display them on windows MFC controls using SetWindowText?

To show unicode string in MessageBoxW I construct it in wstring

WORD  g  [] = {0x105,0x106,0x107,0x108,0x109,0x110,0x111,0x112,0x113,0x114,0x115,0x116,0x117,0x118,0x119,0x120};
wstring gg (reinterpret_cast<wchar_t*>(g),15);
MessageBoxW(NULL, gg.c_str() , gg.c_str() , MB_ICONEXCLAMATION | MB_OK);

Seting MFC form control text:

class MyFrm: public CDialogEx
{
virtual BOOL OnInitDialog();
}
...
BOOL MyFrm::OnInitDialog()
{
GetDlgItem(IDC_EDIT_TICKET_NUMBER)->SetWindowText( ???);
}

Is it possible somehow convert wstring gg to CString and show unicode chars on window control?

2
Can you be more specific? So you want to set Unicode text into MFC control box while the project target Multi-Byte Char Set? Can you post the code snippet where you try it? - Cplusminus_is_coming
Also note that Unicode is being supported starting from Windows XP. You should use ASCII only if you are targeting an older OS which I don't think is the case. - Cplusminus_is_coming
Yes, I want to set Unicode text into MFC control box while the project target Multi-Byte Char Set. I have added some code in message body - vico
The proper answer is to convert the entire project to Unicode, but I presume you didn't want to hear that. - Mark Ransom

2 Answers

2
votes

You could try casting your CDialogEx 'this' object to HWND and then call explictly Win32 API to set text using wchars. So your code will look something like this:

BOOL MyFrm::OnInitDialog()
{
SetDlgItemTextW((HWND)(*this), IDC_EDIT_TICKET_NUMBER, gg.c_str());
}

But as I mentioned earlier Unicode is supported starting from Windows XP and using ASCII is really not a good idea unless you're targeting those very very old OS'es before it. Using them nowdays will cause ALL ASCII strings you pass to be firstly converted into Unicode by the Win32 API. So it is a better idea to switch your project entirely to UNICODE.

0
votes

First, note that you can simply directly initialize a std::wstring with your Unicode hex character data, without any ugly useless reinterpret_cast<wchar_t*>, etc.

Instead of this:

WORD  g  [] = {0x105,0x106,0x107,0x108,...,0x120};
wstring gg (reinterpret_cast<wchar_t*>(g),15);

just consider that:

wstring text = L"\x0105\x0106\x0108...\0x0120";

The latter seems much cleaner to me.

Second, if you want to pass an instance to std::wstring to an MFC method that expects a const wchar_t* input string pointer, just consider using wstring::c_str() method.

In addition, the best suggestion I can give you is to just port your app to Unicode.
ASCII/MBCS should be considered programming model of the past for MFC; they bring lots of problem when you want to write "international" code.