Il faut utiliser la fonction EnumFontFamiliesEx qui permet de parcourir les fontes en fonctions des éléments définis dans la structure LOGFONT conjointement avec une fonction callback
int EnumFontFamiliesEx(
HDC hdc,
LPLOGFONT lpLogfont,
FONTENUMPROC lpEnumFontFamExProc,
LPARAM lParam,
DWORD dwFlags
);
|
Voici un exemple d'implémentation qui permet de créer une fonte d'après les infos d'une fonte système ainsi que son dimensionnement en nombre de points.
Plusieurs choses sont à remarquer dans cette classe :
- La création d'un DC pour l'écran ;
- La définition et l'utilisation d'une fonction callback et sa communication avec l'instance de la classe.
class CFontEx : public CFont
{
public:
bool GetFont(LOGFONT *plogfont,
int nbpoints,
const char *szFontName,
const char *szFontFullName="",
CDC *pDC=NULL);
bool GetCFont(CFont &refCFont ,
int nNbPoints,
const char *szFontName,
const char *szFontFullName="",
CDC *pDC=NULL);
bool GetFont(int nNbPoints,
const char *szFontName,
const char *szFontFullName="",
CDC *pDC=NULL);
CDC *CreateScreenDC(void);
private:
CString m_strFontName,m_strFontFullName;
LOGFONT *m_pCurrentLogFont;
bool m_bFind;
static BOOL CALLBACK AFX_EXPORT EnumFontFamiliesCallBack(ENUMLOGFONTEX* pelf,
NEWTEXTMETRICEX* ,
int FontType, LPVOID pThis);
};
|
BOOL CALLBACK AFX_EXPORT
CFontEx::EnumFontFamiliesCallBack(ENUMLOGFONTEX* pelf,N
EWTEXTMETRICEX* ,
int FontType, LPVOID pThis)
{
CString k=pelf->elfLogFont.lfFaceName;
CFontEx *pFont=(CFontEx *)pThis;
if(pFont->m_strFontName!="" &&
pFont->m_strFontName!=pelf->elfLogFont.lfFaceName) return(1);
if(pFont->m_strFontFullName!="")
if(pFont->m_strFontFullName!=pelf->elfFullName) return(1);
if(pFont->m_strFontFullName=="")
if(pelf->elfFullName!=pFont->m_strFontName) return(1);
memmove(pFont->m_pCurrentLogFont,&pelf->elfLogFont,sizeof(LOGFONT));
pFont->m_bFind=true;
return(0);
}
CDC *CFontEx::CreateScreenDC(void)
{
static CDC *pDC=NULL;
HDC hDC;
hDC = ::GetDC(NULL);
pDC = CDC::FromHandle(hDC);
return (pDC);
}
bool CFontEx::GetFont(int nNbPoints,
const char *szFontName,
const char *szFontFullName ,
CDC *pDC )
{
return GetCFont(*this,
nNbPoints,
szFontName,
szFontFullName,
pDC);
}
bool CFontEx::GetCFont(CFont &refCFont,
int nNbPoints,
const char *szFontName,
const char *szFontFullName ,
CDC *pDC )
{
LOGFONT lf;
if(refCFont.m_hObject) refCFont.DeleteObject();
if(GetFont(&lf,nNbPoints,szFontName,szFontFullName,pDC))
return (refCFont.CreateFontIndirect(&lf)==TRUE);
return false;
}
bool CFontEx::GetFont(LOGFONT *plogfont,
int nbpoints,
const char *szFontName,
const char *szFontFullName ,
CDC *pDC )
{
bool bAutoDC=false;
nbpoints*=10;
m_strFontName=szFontName;
m_strFontFullName=szFontFullName;
m_pCurrentLogFont=plogfont;
m_bFind=false;
if(pDC==NULL)
{
pDC=CreateScreenDC();
bAutoDC=true;
}
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT));
lf.lfCharSet = DEFAULT_CHARSET;
if(!m_strFontName.IsEmpty())
strcpy(lf.lfFaceName,m_strFontName);
else m_strFontName="Courier New";
::EnumFontFamiliesEx(pDC->m_hDC, &lf,
(FONTENUMPROC) CFontEx::EnumFontFamiliesCallBack,
(LPARAM)this ,0);
if(m_bFind)
{
plogfont->lfHeight =-MulDiv(nbpoints,pDC->GetDeviceCaps(LOGPIXELSY),72);
plogfont->lfHeight/=10;
if(nbpoints<0) plogfont->lfHeight=abs(plogfont->lfHeight);
plogfont->lfWidth=0;
}
if(bAutoDC) pDC->DeleteDC();
return(m_bFind);
}
|
exemple d'utilisation demande d'une fonte de type Arial en 10
points .
CFontEx Font;
Font.GetFont(10,"Arial");
|
|