I'm making a Notepad-style program in C++ and have stumbled into a problem. Here is my font changing code; it works perfectly:
{
HDC hdc;
LONG lfHeight;
hdc = GetDC(0);
lfHeight = -MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 72);
ReleaseDC(0, hdc);
DeleteObject((HGDIOBJ)hfDefault);
hfDefault = CreateFont(lfHeight, 0, 0, 0, FW_DONTCARE, 0, 0, 0, 0, 0, 0, DEFAULT_QUALITY, FF_MODERN, "Courier New");
if(!hfDefault) {
MessageBox(hWnd, "An error occurred.", "ERROR 6", MB_ICONERROR | MB_OK);
}
SendDlgItemMessage(hWnd, IDC_MAIN_EDIT, WM_SETFONT, (WPARAM)hfDefault, 1);
}
It works... but it is quite clearly Courier, NOT Courier New. Is there something you need to do to use font names with spaces in them? I already tried using underscores but it didn't work.
EDIT: This problem solved itself.Also, quite unrelated: when opening a file it stops reading once it reaches a null character; I'd like some explanation. I am using the following code to load a file into my edit control:
BOOL LoadTextFile(HWND hEdit, LPCSTR pFileName) {
HANDLE hFile;
BOOL bSuccess = 0;
hFile = CreateFile(pFileName, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
if(hFile != INVALID_HANDLE_VALUE) {
DWORD dwszFile;
dwszFile = GetFileSize(hFile, 0);
if(dwszFile != 0xffffffff) {
LPSTR pFileText;
pFileText = (LPSTR)GlobalAlloc(GPTR, dwszFile + 1);
if(pFileText) {
DWORD dwRead;
if(ReadFile(hFile, pFileText, dwszFile, &dwRead, 0)) {
pFileText[dwszFile] = 0;
if(SetWindowText(hEdit, pFileText)) {
bSuccess = 1;
}
const std::string temp = pFileText;
gscHash = md5(temp);
}
GlobalFree((HGLOBAL)pFileText);
}
}
CloseHandle(hFile);
}
return bSuccess;
}
EDIT: Replacing it with:
BOOL LoadTextFile(HWND hEdit, LPCSTR pFileName) {
std::string read(""), line("");
std::ifstream file;
file.open(pFileName);
while(!file.eof()) {
line = "";
std::getline(file, line);
read += line;
}
file.close();
std::replace(read.begin(), read.end(), 0, 1);
SetWindowText(hEdit, (LPSTR)read.c_str());
return true;
}}
fixed it. Unfortunately, it seems to stop after a lot of control characters.