Re: CListCtrl unicode doesn't display korean characters correctly
On Wed, 04 Apr 2007 13:54:08 +0200, Dansk <dansk@laouilest.com> wrote:
Thank you *very* much for your time and deep analysis.
You're welcome.
- The prototype for LoadString is
bool LoadString(CString &String, DWORD StringId);
I have not written it, I have to trust it.
Maybe you could test it as a black-box in another context, to see if
it is returning correct data.
- strsafe.h is not in my VC6 installation, Where can I find it? I'll try
to include it, but do you *really* think it could change something? In
which way is it safe?
You should find StrSafe in latest Platform SDK (I think that latest
Platform SDK officially supported for VC6 is February 2003 - you can
Google for it and download and install it.).
Or at least, use lstrcpyn
lstrcpyn(pItem->pszText, yourText, pItem->cchTextMax);
It would just protect your code from buffer overruns and malicious
attacks.
I don't think the font itself is a problem as I use it on the CEdit
which works perfectly, and the tooltips on the ListCtrl are displayed
correctrly too.
I just wrote a simple MFC Dialog-based app with a CListCtrl to do a
test:
I used the MS Mincho font to display some (unknown to me) kanjis, in
the 4th row of the list control:
The first two kanji's are something like this:
* **********
* * * *
**** * *
* * *
* ** * *
********* **
I insert this Unicode data in a byte array, and then passed it casting
via LPWSTR (Person.Name is a CString):
<CODE>
// Build some Chinese characters...
BYTE kanjis[] =
{
//0x4E, 0x0A, 0x4E, 0x0C, 0x4E, 0xB6,
//0x00, 0x00
0x0A, 0x4E, 0x0C, 0x4E, 0xB6, 0x4E,
0x00, 0x00
};
Person p;
p.Name = (LPWSTR)kanjis;
...
</CODE>
I first entered kanji Unicode data as showed by Character Map Windows
utility.
e.g. 4E0A (hex) for the first kanji. But I got the undesidered
rectangle displayed.
So, I inverted data as 0A4E, and the character is painted correctly,
as shown in Character Map. I think it is due to Intel CPU architecture
endian-ness.
Also, I changed in LOGFONT also the font height, to make it bigger and
more readable.
So, my guess is that:
1. The font you are using does not have the characters that you want
do display (or maybe the height is too small, you should make it
taller)
2. You are having problmes with loading characters data, maybe you are
loading text from an external source which uses Unicode UTF-8 encoding
and not Unicode UTF-16 (the Windows Unicode format), or there are
endian-ness problems.
Below there is part of my code:
<CODE>
//
// Init the CListCtrl variable.
// Database is stored in "std::vector<Person> m_people" member
variable.
//
void CTestListKorDlg::InitList()
{
//
// Populate m_people database
//
CString data[] =
{
_T("John"), _T("Smith"), _T(""),
_T("Bob"), _T("Taylor"), _T("A friend"),
_T("Chung"), _T("Wong"), _T("He is from far China"),
_T(""), _T(""), _T("")
};
m_people.clear();
int i = 0;
while ( ! data[i].IsEmpty() )
{
m_people.push_back( Person(data[i], data[i+1], data[i+2]) );
i += 3;
}
// Build some Chinese characters...
BYTE kanjis[] =
{
//0x4E, 0x0A, 0x4E, 0x0C, 0x4E, 0xB6,
//0x00, 0x00
0x0A, 0x4E, 0x0C, 0x4E, 0x4E, 0xB6,
0x00, 0x00
};
Person p;
p.Name = (LPWSTR)kanjis;
p.Surname = _T("Kung");
p.Notes = _T("Let's try Kanjis...");
m_people.push_back(p);
//
// Create List control columns
//
m_list.InsertColumn(0, _T("Name"), LVCFMT_LEFT, 100);
m_list.InsertColumn(1, _T("Surname"), LVCFMT_LEFT, 100);
m_list.InsertColumn(2, _T("Notes"), LVCFMT_LEFT, 200);
m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_INFOTIP|LVS_EX_GRIDLINES);
//
// Adjust font (use MS Mincho)
//
CFont * font = m_list.GetFont();
ASSERT(font != NULL);
LOGFONT lf;
font->GetLogFont(&lf);
lf.lfHeight = -14;
StringCbCopy(lf.lfFaceName, sizeof(lf.lfFaceName), _T("MS
Mincho"));
m_listFont.CreateFontIndirect(&lf);
m_list.SetFont(&m_listFont);
// The list control will ask data to us
m_list.SetItemCount((int)m_people.size());
}
//
// Give back the list-control information it is asking to us
//
void CTestListKorDlg::OnListGetDispInfo(NMHDR* pNMHDR, LRESULT*
pResult)
{
LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
// Create a pointer to the item
LV_ITEM* pItem= &(pDispInfo)->item;
// Which item number?
int itemid = pItem->iItem;
// Do the list need text information?
if (pItem->mask & LVIF_TEXT)
{
CString text;
// Which column?
if(pItem->iSubItem == 0)
{
// Text is name
text = m_people[itemid].Name;
}
else if (pItem->iSubItem == 1)
{
// Text is surname
text = m_people[itemid].Surname;
}
else if (pItem->iSubItem == 2)
{
// Text is notes
text = m_people[itemid].Notes;
}
// Copy the text to the LV_ITEM structure
// Maximum number of characters is in pItem->cchTextMax
StringCchCopy(pItem->pszText, pItem->cchTextMax, text);
}
*pResult = 0;
}
</CODE>
MrAsm