CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 1999
    Location
    NH, USA
    Posts
    47

    Using CFontDialog to change font on CRichEditCtrl

    I'm writing an app that uses a CRichEditCtrl. I want to be able to use a CFontDialog to let the users change the Rich control's font. The problem is that the Rich controls use a CHARFORMAT to hold the font data and CFontDialogs use CHOOSEFONT.

    Can anyone shed some light on this?


  2. #2
    Join Date
    May 1999
    Posts
    8

    Re: Using CFontDialog to change font on CRichEditCtrl

    some light!! I will give you the whole sun , but it's a hack.
    if you look at the MFC code , CFontdialog has a constructor that will take CHARFORMAT( this fact is undocumented , though..) as it's paramater , though I dont remember how you'll get it back , so just see the source.
    hope that helps
    jingles


  3. #3
    Join Date
    Aug 1999
    Posts
    6

    Re: Using CFontDialog to change font on CRichEditCtrl

    Here is the code snippet to do your job.
    Assume m_EditArea is your CRichEditCtrl Variable


    LOGFONT lf;
    CFont* pFont;
    CHARFORMAT cf, newcf;
    DWORD mask;
    long start, end;
    CDC *ClientDC;

    ClientDC = this->GetDC();
    memset(&cf, 0, sizeof(CHARFORMAT));
    memset(&newcf, 0, sizeof(CHARFORMAT));
    cf.cbSize = sizeof(CHARFORMAT);
    newcf.cbSize = sizeof(CHARFORMAT);
    memset(&lf, 0, sizeof(LOGFONT));
    pFont = m_EditArea.GetFont();
    mask = m_EditArea.GetSelectionCharFormat(cf);
    if (!mask)
    mask = cf.dwMask;
    if (!pFont)
    pFont = GetFont();
    pFont->GetLogFont(&lf);
    strcpy(lf.lfFaceName, cf.szFaceName);
    if (cf.dwEffects & CFE_BOLD)
    lf.lfWeight = FW_BOLD;
    else
    lf.lfWeight = FW_NORMAL;
    if (cf.dwEffects & CFE_ITALIC)
    lf.lfItalic = true;
    if (cf.dwEffects & CFE_UNDERLINE)
    lf.lfUnderline = true;
    if (cf.dwEffects & CFE_STRIKEOUT)
    lf.lfStrikeOut = true;
    // convert twips into point size
    int PtHeight = (cf.yHeight/20);
    // convert pointsize into logical units
    lf.lfHeight = -MulDiv (PtHeight, GetDeviceCaps (ClientDC->m_hDC, LOGPIXELSY), 72);
    CFontDialog dlg(&lf);
    dlg.m_cf.Flags |= CF_EFFECTS;
    dlg.m_cf.rgbColors = cf.crTextColor;
    if (dlg.DoModal() == IDOK)
    {
    strcpy(newcf.szFaceName, dlg.GetFaceName());
    newcf.dwMask |= CFM_FACE;
    newcf.yHeight = (dlg.GetSize()/10) * 20; // convert into twips
    newcf.dwMask |= CFM_SIZE;
    newcf.crTextColor = dlg.GetColor();
    newcf.dwMask |= CFM_COLOR;
    newcf.dwMask |= CFM_BOLD;
    if (dlg.IsBold()) {
    newcf.dwEffects |= CFE_BOLD;
    }
    newcf.dwMask |= CFM_ITALIC;
    if (dlg.IsItalic()) {
    newcf.dwEffects |= CFE_ITALIC;
    }
    newcf.dwMask |= CFM_UNDERLINE;
    if (dlg.IsUnderline()) {
    newcf.dwEffects |= CFE_UNDERLINE;
    }
    newcf.dwMask |= CFM_STRIKEOUT;
    if (dlg.IsStrikeOut()) {
    newcf.dwEffects |= CFE_STRIKEOUT;
    }
    m_ruleArea.SetSelectionCharFormat(newcf);
    }





    Hope that helps
    mfcUser


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured