MFC issues with Unicode and fonts in CListCtrl using Windows 7

I'm developing an application in C++ using MFC that needs to be able to handle Unicode characters. Whenever I try to view Unicode characters in a CListCtrl, they always show up as squares, but I can see them just fine in CEdit's and CRichEdit's. I saved a list of characters I was trying to see out to a file using this code:

bool CFileIO::writeStringToFile(CString str, CString filepath)
{
    FILE* outFile = _tfopen(filepath, _T("wb"));
    if(outFile == NULL)
    {
        return false;
    }

#ifdef _UNICODE
    int markerBits = 0xFEFF;
    fwrite(&markerBits, 2, 1, outFile);
#endif
    fwrite(str.GetBuffer(), sizeof(TCHAR), str.GetLength(), outFile);
    fclose(outFile);

    return true;
}

where _UNICODE is defined and the CString is just a list of characters separated by newline characters. When I opened this file in Notepad++, I was unable to view the Unicode characters (they showed up as squares), but the characters that were also ASCII characters showed up just fine (this is the same behavior I was getting from the CListCtrl). Since I was able to copy-paste the squares into Google Chrome's search bar and they would show up correctly there, I figured that Notepad++ must be reading the characters correctly, it just couldn't display them. After some internet sleuthing, I thought the issue might be a font problem, so in Notepad++ I changed the font to Arial Unicode MS and voila! It worked! I could see all the Unicode characters (and ASCII characters) just fine.

So I decided to change the font of my CListCtrl to Arial Unicode MS just to see if that would fix the problem (since it worked for Notepad++), but I had my doubts since all my CListCtrl's and CEdit's/CRichEdit's use the same font. As I feared, changing the font did not change the Unicode characters from being squares (they were now just slightly different squares).

Here's the kicker: I develop on Windows 7 and my client uses Windows XP. He says he has never had any of these problems that I've been describing. So, I set up an XP virtual machine on my computer, and low-and-behold, it works just fine. The only difference between XP and 7 when it comes to Unicode that I can think of is that on XP you have to explicitly enable Unicode (and I think install some extra content), whereas on 7 its supposed to be enabled by default.

One final thing: When I said it worked in XP, that wasn't entirely accurate. Even though most Unicode characters show up correctly, there were a few that still show up as squares in the CListCtrl (but in Notepad++ in XP and in 7 they show up correctly). I printed out their unsigned character values, just to see if maybe they were overflowing the TCHAR variable or something. They all had values in the range of 8212-8226 (not necessarily comprehensive, just the range that I tried to print). I have no idea why they don't show up.

I have been searching the internet for quite some time, and I have no idea what is going on here. The only thing I can think of is it might be a similar issue to the one I had with CRichEdit's awhile back (they also didn't display Unicode characters as anything but squares, so I had to go into the .rc file and change the control from "RichEdit" to "RichEdit20W"). But I haven't been able to find anything related to that for CListCtrl's. Does anyone have any insight about MFC and how it handles Unicode that might point me in the right direction?

UPDATE: Following DavidK's advice (in the comments below), I created a test MFC app that was just a dialog with a CListCtrl in it. Upon starting, it read in the aforementioned file with the mix of Unicode and ASCII characters and everything displayed correctly (no boxes at all). I went back and ran the original program and everything almost worked correctly (the characters that would not display in XP also showed up as black boxes, but everything else showed up correctly). I copied the program to a separate Windows 7 machine (not my development machine) and ran it. I had the same problem as before (all Unicode characters in CListCtrl's were boxes). I should mention that the test app is in the same solution as the main project, I'm not sure if that matters though.

I copied the test app to my non-development machine and when I ran it, it displayed boxes for all the Unicode characters, the same way the main program did on that machine. I am quite confused. My only guess is that the apps are linking to different DLL's on the different machines and/or there's some issue with the fonts on the machines.

链接地址: http://www.djcxy.com/p/88672.html

上一篇: Json将&字符串转换为\ u0026

下一篇: MFC使用Windows 7在CListCtrl中使用Unicode和字体问题