我試圖編寫一個通用的文本編輯器,它可以在EditControl中打開並顯示ANSI和Unicode。如果我確定文本是ANSI,是否需要重複撥打ReadFile()
?無法弄清楚如何執行此任務。我的下面的嘗試不起作用,它顯示'?' EditControl中的字符。MultiByteToWideChar或WideCharToMultiByte和txt文件
LARGE_INTEGER fSize;
GetFileSizeEx(hFile,&fSize);
int bufferLen = fSize.QuadPart/sizeof(TCHAR)+1;
TCHAR* buffer = new TCHAR[bufferLen];
buffer[0] = _T('\0');
DWORD wasRead = 0;
ReadFile(hFile,buffer,fSize.QuadPart,&wasRead,NULL);
buffer[wasRead/sizeof(TCHAR)] = _T('\0');
if(!IsTextUnicode(buffer,bufferLen,NULL))
{
CHAR* ansiBuffer = new CHAR[bufferLen];
ansiBuffer[0] = '\0';
WideCharToMultiByte(CP_ACP,0,buffer,bufferLen,ansiBuffer,bufferLen,NULL,NULL);
SetWindowTextA(edit,ansiBuffer);
delete[]ansiBuffer;
}
else
SetWindowText(edit,buffer);
CloseHandle(hFile);
delete[]buffer;
記事本++的源代碼可能是相關的作爲參考:http://notepad-plus-plus.org/download/ –