Click to See Complete Forum and Search --> : createfont help


g3RC4n
October 24th, 2008, 10:42 AM
i'm writing a (ansii) small text rending engine and i'm trying to get fonts to work, now i know how to create one font ("time new roman"), but i don't know the names of all the other fonts


#pragma once

#include <string>

#include <windows.h>

enum text_rend_font{
font_times_new_roman
};

class text_rend{
const int font_height;
const int font_width;
public:
text_rend(HDC hdc):
font_height(16),
font_width(8),
m_hdc(hdc){
}
void draw_text(int x, int y, const std::string& text){
::TextOutA(m_hdc,x,y,(LPSTR)text.c_str(),text.size());
}
void foo_font(){
HFONT font = ::CreateFontA(font_height,font_width,0, 0,
FW_NORMAL, FALSE, FALSE, FALSE,
ANSI_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_ROMAN,
"Times New Roman");
::SelectObject(m_hdc,font);
}
private:
HDC m_hdc;
};


now i understand that something like EnumFontFamilies can be used to get the names but i can't find any examples or anything, because what i would like to do is out all the names in a map associated with an enum or something

so any help or links provided i would be very thankful for

thanks :)

zkidkid
October 24th, 2008, 11:41 AM
yes, EnumFontFamilies() will help u solve ur problem.

I create a SDI program and in OnDraw :

CxxxView::OnDraw(CDC* pDC){
EnumFontFamilies(pDC->GetSafeHdc(),NULL,
(FONTENUMPROC)listFont,
(LPARAM) pDC);
}
and I create a global or static funtion name listFont:

int CALLBACK CxxxView::listFont(ENUMLOGFONT *lpelf, NEWTEXTMETRIC *lpntm, DWORD FontType, LPARAM lParam)
{
aFont.push_back(lpelf->elfFullName);
return 1;
}

so when it's done, I will have name of all fonts in vector<CString> aFont

g3RC4n
October 24th, 2008, 12:43 PM
cheers, i got it to work as functions, really appreciate it

but of course the vector had to be global so it won't do

now i just got to stick it all in a class, but i can't get the member function to be used as a callback, i've tryed std::mem_fun and std::mem_fun_ref


class system_fonts{
typedef std::vector<std::wstring> font_vec;
public:
system_fonts(HDC hdc){
EnumFontFamilies(hdc,NULL,
reinterpret_cast<FONTENUMPROCW>(&system_fonts::listFont),
0);
}
const font_vec get_fonts()const{
return m_fonts;
}
private:
int CALLBACK listFont(ENUMLOGFONT *lpelf, NEWTEXTMETRIC *lpntm, DWORD FontType, LPARAM lParam){
m_fonts.push_back(lpelf->elfFullName);
return 1;
}
private:
font_vec m_fonts;
};


any help?

g3RC4n
October 24th, 2008, 12:52 PM
done it, just make the buffer and callback static


class system_fonts{
typedef std::vector<std::wstring> font_vec;
public:
const font_vec get_fonts(HDC hdc){
m_fonts.clear();
EnumFontFamilies(hdc,NULL,
reinterpret_cast<FONTENUMPROC>(&system_fonts::listFont),
0);
return m_fonts;
}
private:
static int CALLBACK listFont(ENUMLOGFONT *lpelf, NEWTEXTMETRIC *lpntm, DWORD FontType, LPARAM lParam){
m_fonts.push_back(lpelf->elfFullName);
return 1;
}
static font_vec m_fonts;
};
system_fonts::font_vec system_fonts::m_fonts;


works just as well, thanks again zkidkid