Weird TextOut Unicode problems

After porting Easy2Sync for Files to Unicode I experienced some really weird problems with some foreign characters. Unicode in general worked, Japanese characters were displayed nicely (not that I could read them). But Cyrillic characters failed when printed with TextOut and were displayed as small black rectangles.

Even weirder: Adding just 1 Japanese character to a string with Cyrillic characters caused the entire string to be printed right.

It seems that TextOut is only 50% Unicode compatible (of course Microsoft doesn’t mention this). So one has to use DrawText instead, which doesn’t seem to have these limitations. Since DrawText behaves differently regarding the position and background drawing, here’s a wrapper which fixes this and makes it behave like ExtTextOut:

void FixedExtTextOut (CDC *pDC, int x, int y, UINT nOptions, LPCRECT lpRect, LPCTSTR lpszString, UINT nCount, LPINT lpDxWidths)
{
 CRect rr(x, y, lpRect->right, lpRect->bottom);

 pDC->FillSolidRect(lpRect, pDC->GetBkColor());
 pDC->DrawText(lpszString, nCount, &rr, 0);
}

Comments are closed.