Welcome, Guest. Please login or register.
Did you miss your activation email?


Login with username, password and session length

News

Welcome to Yet Another Programming Community!

 
Pages: [1]   Go Down Print
Author Topic: CreateFont() fails with Courier New - SOLVED  (Read 136 times)
PlasticineGuy
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 19


View Profile
« on: January 31, 2010, 04:29:39 AM »

I'm making a Notepad-style program in C++ and have stumbled into a problem. Here is my font changing code; it works perfectly:
Code:
{
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:
Code:
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:
Code:
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.
« Last Edit: January 31, 2010, 05:14:52 AM by PlasticineGuy » Logged
Fire Lancer
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 1


View Profile Email
« Reply #1 on: February 01, 2010, 02:05:52 AM »

Quote
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:

"const std::string temp = pFileText;" would stop at any null characters.

Your not telling std::string how long pFileText is, so it assumes it's null terminated (basically calling strlen to get the length of it). To assign a c-string containing nulls to an std::string you should also provide the length ie:

"const std::string temp(pFileText, dwszFile);"


Not sure whats wrong with your std::ifstream code, however I wouldn't do it that way personally.
Code:
bool LoadTextFile(HWND hEdit, const char *fileName)
{
std::ifstream file(fileName);
//check the file was opened
if(!file.good())return false;
//files are seekable, so you can get the length
file.seekg(0, std::ios::end);
size_t len = file.tellg();
//go back to start
file.seekg(0, std::ios::beg);

char *read = new char[len+1];//+1 to guarantee space for null

file.read(read, len);
//std::ifstream does things like convert \r\n to just \n in text mode. So the amoutn of text is read may be less than len.
len = file.gcount();
//null terminate
read[len] = 0;
SetWindowText(hEdit, read);
delete[] read;
return true;
}
Logged
PlasticineGuy
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 19


View Profile
« Reply #2 on: February 03, 2010, 12:00:51 AM »

The issue was that I was reading in text mode, and it was stopping at \x1A (EOF character). Switching to binary solved it:
Code:
BOOL LoadTextFile(HWND hEdit, LPCSTR pFileName) {
std::string read(""), line("");
std::ifstream file;
file.open(pFileName, std::ios::binary | std::ios::in);
while(!file.eof()) {
line = "";
std::getline(file, line);
read += line;
}
file.close();
std::replace(read.begin(), read.end(), (char)0x1A, '.');
std::replace(read.begin(), read.end(), (char)0, '.');
gscHash = md5(read);
SetWindowText(hEdit, (LPSTR)read.c_str());
return true;
}
Still trying to figure out the font issue.
Logged
Pages: [1]   Go Up Print
 
Jump to:  

 
     
Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC
Theme by Roach
Valid XHTML 1.0! Valid CSS!