IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
logo
Sommaire > Contrôles > Gestion des Fontes
        Comment créer une fonte d'après une fonte système ?
        Comment créer une fonte pour écrire verticalement ?
        Comment personnaliser la police de caractères d'un contrôle ?
        Comment utiliser une fonte intégrée dans les ressources ?



Comment créer une fonte d'après une fonte système ?
auteur : Farscape
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,                          // handle to DC
  LPLOGFONT lpLogfont,              // font information
  FONTENUMPROC lpEnumFontFamExProc, // callback function
  LPARAM lParam,                    // additional data
  DWORD dwFlags                     // not used; must be 0
);
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* /*lpntm*/, 
                                                         int FontType, LPVOID pThis);
};

BOOL CALLBACK AFX_EXPORT 
CFontEx::EnumFontFamiliesCallBack(ENUMLOGFONTEX* pelf,N
                                  EWTEXTMETRICEX* /*lpntm*/,
                                  int FontType, LPVOID pThis)
{
    CString k=pelf->elfLogFont.lfFaceName;
    CFontEx *pFont=(CFontEx *)pThis;

    // recherche d'une fonte specifique
    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 /*=NULL*/)
{

// 
    return GetCFont(*this,
                    nNbPoints,
                    szFontName,
                    szFontFullName,
                    pDC);
}

bool CFontEx::GetCFont(CFont &refCFont,
                       int nNbPoints,
                       const char *szFontName,
                       const char *szFontFullName /*=""*/,
                       CDC *pDC /*=NULL*/)
{

// 
    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 /*=NULL*/)

{ 

    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");  

Comment créer une fonte pour écrire verticalement ?
auteur : Farscape
En manipulant les valeurs lfEscapement et lfOrientation de la logfont.

// reglage de la fonte en mode vertical:
LOGFONT lf;
//1 récupération des paramètres d'une CFont ou d'une fenêtre  
VERIFY(pCurrentFont->GetLogFont(&lf));
// 2 changement des valeurs
lf.lfEscapement=900;
lf.lfOrientation=900;
// Création de la nouvelle fonte
CFont FontVert;
VERIFY(FontVert.CreateFontIndirect(&lf));
CFont *pOldFont=dc.SelectObject(&FontVert);

// Affichage final d'une chaîne stext, dans le rectangle rectText calculé au préalable bien sur.
UINT nFormat=DT_LEFT|DT_SINGLELINE;
dc.DrawText(sText,&rectText,nFormat|DT_CALCRECT);
Pour de plus amples détails voir l'aide sur la structure LOGFONT


Comment personnaliser la police de caractères d'un contrôle ?
auteur : Farscape
Exemple modification de la fonte d'un static en gras.

   CWnd *pWnd = GetDlgItem(IDC_STATIC1);
   CFont* pFont = pWnd->GetFont();
   if(pFont!=NULL)
   {
      LOGFONT lf;
      pFont->GetLogFont(&lf);
      lf.lfWeight=FW_BOLD;//mettre l'attribut en gras par exemple.
      // CFont m_font;  // objet local à la classe parent fenetre CDialog,CFormView

      m_font.DeleteObject();  // destruction GDI de l'ancien objet eventuellement
      m_font.CreateFontIndirect(&lf); // creation de la fonte d'apres la LOGFONT
    }
    pWnd->SetFont(&m_font);  // affectation de la nouvelle fonte.

Comment utiliser une fonte intégrée dans les ressources ?
Créé le 17/09/2007[haut]
auteur : Farscape
On Procédera comme suit :

    // Installe la police 
    HRSRC hRes   = FindResource(NULL, MAKEINTRESOURCE(IDR_FONT), _T("TTF"));
    PVOID lpFont = LockResource(LoadResource(NULL, hRes)); 
    DWORD dwSize = SizeofResource(NULL, hRes), cFonts = 0;
    HANDLE hFont = AddFontMemResourceEx(lpFont, dwSize, NULL, &cFonts);
    ASSERT(cFonts > 0);

    // Crée la police

    CFont Font;
    VERIFY(Font.CreatePointFont(110, _T("NomDeLaPolice")));

    // ... Utilisation

    // Désinstalle la police

    VERIFY(RemoveFontMemResourceEx(hFont));
Pour insérer la fonte dans les ressources on procédera comme indiqué dans ce post de la faq :faq Comment émettre un son ?



Consultez les autres F.A.Q.


Valid XHTML 1.0 TransitionalValid CSS!

Les sources présentées sur cette page sont libres de droits et vous pouvez les utiliser à votre convenance. Par contre, la page de présentation constitue une œuvre intellectuelle protégée par les droits d'auteur. Copyright © 2004 Developpez Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site ni de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon vous encourez selon la loi jusqu'à trois ans de prison et jusqu'à 300 000 € de dommages et intérêts.