我正在开发一个应用程序,它接收以UTF-8编码的文本,并需要在一些MFC控件上显示它。应用程序是使用MultiByte字符集(MBCS)构建的,让我们假设这是不能更改的。
我希望如果我将从UTF-8收到的文本转换为宽字符字符串,我将能够使用SetWindowTextW
方法正确地显示它。为了尝试这一点,我使用了一个玩具应用程序,它从文件中读取输入并设置控件的文本。
std::wstring utf8_decode(const std::string &str)
{
if (str.empty()) return std::wstring();
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
std::wstring wstrTo(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
return wstrTo;
}
BOOL CAboutDlg::OnInitDialog()
{
std::vector<std::string> texts;
texts.resize(6);
std::fstream f("D:\\code\\sample-utf8.txt", std::ios::in);
for (size_t i=0;i<6;++i)
std::getline(f, texts[i]);
::SetWindowTextW(GetDlgItem(IDC_BUTTON1)->m_hWnd, utf8_decode(texts[0]).c_str());
::SetWindowTextW(GetDlgItem(IDC_BUTTON2)->m_hWnd, utf8_decode(texts[1]).c_str());
::SetWindowTextW(GetDlgItem(IDC_BUTTON3)->m_hWnd, utf8_decode(texts[2]).c_str());
::SetWindowTextW(GetDlgItem(IDC_BUTTON4)->m_hWnd, utf8_decode(texts[3]).c_str());
::SetWindowTextW(GetDlgItem(IDC_BUTTON5)->m_hWnd, utf8_decode(texts[4]).c_str());
::SetWindowTextW(GetDlgItem(IDC_BUTTON6)->m_hWnd, utf8_decode(texts[5]).c_str());
return TRUE;
}
在使用MBCS构建玩具应用程序后,我没有得到我想要的东西。
一旦我使用unicode构建了应用程序,它就能正常工作
这是否意味着当我使用MBCS构建时,不可能对单个控件使用unicode文本?如果可能的话,你能给我一些建议吗?谢谢。
发布于 2019-02-02 05:43:19
MBCS应用程序创建MBCS窗口,一般来说,即使您使用宽字符串接口,这些窗口也只能显示单个代码页中的文本。
对于MBCS应用程序,宽字符串版本的SetWindowTextW本质上是将宽字符串转换为MBCS,使用用户的当前语言环境(具有默认代码页),然后将其传递给函数的-A版本。
正如您在"Unicode“体验中所看到的,总体上您正在做正确的事情,但是您受到应用程序是MBCS的事实的限制。
发布于 2019-02-05 02:15:24
多亏了Adrian McCarthy和Joseph Willcoxson的建议,我让它工作起来了。我使用CreateWindowExW
方法创建控件,然后使用SetWindowTextW
设置文本。下面是示例代码,以防有任何帮助:
std::wstring utf8_decode(const std::string &str)
{
if (str.empty()) return std::wstring();
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
std::wstring wstrTo(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
return wstrTo;
}
HWND CreateButtonW(int x, int y, int width, int height, HWND parent)
{
HWND hwndButton = ::CreateWindowExW(
WS_EX_CLIENTEDGE,
L"BUTTON", // Predefined class; Unicode assumed
L"", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
x, y, width, height, parent,
NULL, // No menu.
(HINSTANCE)GetWindowLong(parent, GWL_HINSTANCE),
NULL); // Pointer not needed.
return hwndButton;
}
BOOL CAboutDlg::OnInitDialog()
{
std::vector<std::string> texts;
texts.resize(6);
std::fstream f("D:\\code\\sample-utf8.txt", std::ios::in);
for (size_t i=0;i<6;++i)
std::getline(f, texts[i]);
::SetWindowTextW(GetDlgItem(IDC_BUTTON1)->m_hWnd, utf8_decode(texts[0]).c_str());
::SetWindowTextW(GetDlgItem(IDC_BUTTON2)->m_hWnd, utf8_decode(texts[1]).c_str());
::SetWindowTextW(GetDlgItem(IDC_BUTTON3)->m_hWnd, utf8_decode(texts[2]).c_str());
::SetWindowTextW(GetDlgItem(IDC_BUTTON4)->m_hWnd, utf8_decode(texts[3]).c_str());
::SetWindowTextW(GetDlgItem(IDC_BUTTON5)->m_hWnd, utf8_decode(texts[4]).c_str());
::SetWindowTextW(GetDlgItem(IDC_BUTTON6)->m_hWnd, utf8_decode(texts[5]).c_str());
auto width = [](RECT& r) { return r.right - r.left; };
auto height = [](RECT& r) { return r.bottom - r.right; };
RECT r;
GetDlgItem(IDC_BUTTON1)->GetWindowRect(&r); ScreenToClient(&r);
HWND hBtnWnd = CreateButtonW(r.right+20, r.top, width(r), height(r), m_hWnd);
::SetWindowTextW(hBtnWnd, utf8_decode(texts[0]).c_str());
GetDlgItem(IDC_BUTTON2)->GetWindowRect(&r); ScreenToClient(&r);
hBtnWnd = CreateButtonW(r.right+20, r.top, width(r), height(r), m_hWnd);
::SetWindowTextW(hBtnWnd, utf8_decode(texts[1]).c_str());
GetDlgItem(IDC_BUTTON3)->GetWindowRect(&r); ScreenToClient(&r);
hBtnWnd = CreateButtonW(r.right+20, r.top, width(r), height(r), m_hWnd);
::SetWindowTextW(hBtnWnd, utf8_decode(texts[2]).c_str());
GetDlgItem(IDC_BUTTON4)->GetWindowRect(&r); ScreenToClient(&r);
hBtnWnd = CreateButtonW(r.right+20, r.top, width(r), height(r), m_hWnd);
::SetWindowTextW(hBtnWnd, utf8_decode(texts[3]).c_str());
GetDlgItem(IDC_BUTTON5)->GetWindowRect(&r); ScreenToClient(&r);
hBtnWnd = CreateButtonW(r.right+20, r.top, width(r), height(r), m_hWnd);
::SetWindowTextW(hBtnWnd, utf8_decode(texts[4]).c_str());
GetDlgItem(IDC_BUTTON6)->GetWindowRect(&r); ScreenToClient(&r);
hBtnWnd = CreateButtonW(r.right+20, r.top, width(r), height(r), m_hWnd);
::SetWindowTextW(hBtnWnd, utf8_decode(texts[5]).c_str());
return TRUE;
}
以及结果-在左边是默认创建的按钮,在右边是使用CreateWindowExW
创建的按钮
https://stackoverflow.com/questions/54484799
复制相似问题