【发布时间】:2019-09-08 22:19:41
【问题描述】:
尝试将文本添加到列表视图。 尽管多次尝试正确使用编码,但代码仍显示为中文字符。 json文件到listview的代码总结:
// json file (UTF8 without BOM)
"name": "abcdefghijklmnop" // shows as Chinese
// "name": "ササササササササササ" // Japanese does not show correctly neither
// declare structure and array; malloc array later
struct user {
char* name;
// char[16] name;
};
struct user *users;
// read from json
cJSON* json_name = cJSON_GetObjectItemCaseSensitive(json_user, "name");
// set name (in a loop)
users[i].name = json_name->valuestring;
// create listview
HWND hWndListView = CreateWindowExW(NULL,
WC_LISTVIEW,
L"Test Listview",
WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_EDITLABELS,
// ...
// add column definitions
LVCOLUMN lvc = { 0 };
lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH | LVCF_FMT;
lvc.fmt = LVCFMT_LEFT;
// column example
lvc.iSubItem = 0;
lvc.cx = 100;
lvc.pszText = TEXT("A");
ListView_InsertColumn(hWndListView, 0, &lvc);
// debug: check it's unicode
BOOL what = IsWindowUnicode(hWndListView); // 1
// debug
#ifdef UNICODE
int i = 0; // this is executed
#endif
我尝试过的:
ListView_SetItemText(hWndListView, 2, 1, servers[0].name); // chinese
ListView_SetItemText(hWndListView, 2, 1, *servers[0].name); // access violation
ListView_SetItemText(hWndListView, 2, 1, (LPWSTR)servers[0].name); // chinese
ListView_SetItemText(hWndListView, 2, 1, L"%s", servers[0].name); // %s
ListView_SetItemText(hWndListView, 3, 0, TEXT(servers[0].name)); // does not compile
在 VS2019 调试器中,添加一个带有“s8”参数的手表以查看结果,因为 UTF8 正确显示了文本。 只有在控件中添加时,它才会正确显示。 请注意,这是有效的:
ListView_SetItemText(hWndListView, 2, 2, TEXT("ササ"));
问题:我缺少什么才能正确显示我的数据?
【问题讨论】:
标签: c listview winapi encoding