locked
Font and Color CEdit RRS feed

  • Question

  •  

    I like modification the Fonte and Back Color of Cedit. Please, Help.

     

    Thanks.

    Wednesday, September 19, 2007 6:00 PM

Answers

  • Now, let's go tu edit your CEdit control

     

    CFont* pOldFont = m_cnsts.GetFont();

    CFont* pNewFont = new CFont;

    LOGFONT logfont;

    pOldFont->GetObject(sizeof(logfont),&logfont);

    logfont.lfHeight = 50;

    logfont.lfWidth = 10;

    logfont.lfWeight = FW_HEAVY;

    logfont.lfPitchAndFamily = DEFAULT_PITCH | FF_ROMAN;

    logfont.lfFaceName[0] = '\0';    //Can be changed with other fonts by use copy function to set data for ltFaceName

    logfont.lfEscapement = logfont.lfOrientation = 0;

    pNewFont->CreateFontIndirect(&logfont);

    m_cnsts.SetFont(pNewFont);

     

    Where: m_cnsts has type is CEdit

     

    Apply ! And.....

     

    Goodluck !

    Thursday, September 20, 2007 7:46 AM

All replies

  • Hi,

     

    Please review the documentation:

     

    To change the font: WM_SETFONT.

    To change the back color: WM_CTLCOLOREDIT.

     

    Hope this helps.

    Wednesday, September 19, 2007 6:05 PM
  • Now, let's go tu edit your CEdit control

     

    CFont* pOldFont = m_cnsts.GetFont();

    CFont* pNewFont = new CFont;

    LOGFONT logfont;

    pOldFont->GetObject(sizeof(logfont),&logfont);

    logfont.lfHeight = 50;

    logfont.lfWidth = 10;

    logfont.lfWeight = FW_HEAVY;

    logfont.lfPitchAndFamily = DEFAULT_PITCH | FF_ROMAN;

    logfont.lfFaceName[0] = '\0';    //Can be changed with other fonts by use copy function to set data for ltFaceName

    logfont.lfEscapement = logfont.lfOrientation = 0;

    pNewFont->CreateFontIndirect(&logfont);

    m_cnsts.SetFont(pNewFont);

     

    Where: m_cnsts has type is CEdit

     

    Apply ! And.....

     

    Goodluck !

    Thursday, September 20, 2007 7:46 AM
  • The very best way to do this is by deriving a custom edit control using the "add MFC class" feature of visual studio and setting the base class to CEdit.

    Use the studio to add an event handler for WM_CTLCOLOR

    Here is an example that I use in my own code

    class CTextEdit : public CEdit
    {
     DECLARE_DYNAMIC(CTextEdit)

    public:
     CTextEdit();
     virtual ~CTextEdit();
     void UpdateFont(CString csFontName,int iPtSize,BOOL bBold,BOOL bBoldNormal,BOOL bItalics);
     void SetTextColorRGB(COLORREF crTextColorRGB);
     void SetBackColorRGB(COLORREF crBackColorRGB);

     COLORREF m_crTextColorRGB,m_crBackColorRGB;
     CBrush m_BackGround;
     CFont m_Font;
     int m_iPtSize;
     CString m_csFontName;
     BOOL m_bBold,m_bBoldNormal,m_bItalics;

    protected:
     DECLARE_MESSAGE_MAP()

     void TextEditFont();
     bool m_bSetFont;

    public:
     afx_msg HBRUSH CtlColor(CDC * pDC,UINT nCtlColor);
    };

    IMPLEMENT_DYNAMIC(CTextEdit, CEdit)

    CTextEdit::CTextEdit()
    {
     // Set the default color information
     m_crTextColorRGB = RGB(255,255,255);
     m_crBackColorRGB = RGB(0,0,0);
     m_BackGround.CreateSolidBrush(m_crBackColorRGB);

     // Set the default font information
     m_iPtSize = 12 * 10;
     m_bBold = FALSE;
     m_bBoldNormal = TRUE;
     m_bItalics = FALSE;
     m_csFontName = L"Calibri";

     // Create the default font
     TextEditFont();
     m_bSetFont = false;
    }

    // Change the font
    void CTextEdit::UpdateFont(CString csFontName,int iPtSize,BOOL bBold,BOOL bBoldNormal,BOOL bItalics)
    {
     m_iPtSize = iPtSize * 10;
     m_bBold = bBold;
     m_bBoldNormal = bBoldNormal;
     m_bItalics = bItalics;
     m_csFontName = csFontName;

     // Create the default font
     TextEditFont();
     m_bSetFont = false;
    }

    void CTextEdit::SetTextColorRGB(COLORREF crTextColorRGB)
    {
     m_crTextColorRGB = crTextColorRGB;
    }

    void CTextEdit::SetBackColorRGB(COLORREF crBackColorRGB)
    {
     m_crBackColorRGB = crBackColorRGB;
     m_BackGround.DeleteObject();
     m_BackGround.CreateSolidBrush(m_crBackColorRGB);
    }

    CTextEdit::~CTextEdit()
    {
    }

    // Create the font for use in the edit control
    void CTextEdit::TextEditFont()
    {
     // Create the font for the edit control
     CDC DCDisplay;
     DCDisplay.CreateCompatibleDC(NULL);
     m_Font.DeleteObject();
     int iLogPixelsY = GetDeviceCaps(DCDisplay,LOGPIXELSY);
     LONG lfHeight = MulDiv(m_iPtSize,iLogPixelsY,72);
     LOGFONT LogFont;
     memset(&LogFont,0,sizeof(LOGFONT));
     LogFont.lfWeight = FW_NORMAL;
     if (m_bBold)
     {
      if (m_bBoldNormal)
       LogFont.lfWeight = FW_BOLD;
      else
       LogFont.lfWeight = FW_HEAVY;
     }
      
     LogFont.lfItalic = m_bItalics;
     LogFont.lfHeight = lfHeight;
     LogFont.lfCharSet = DEFAULT_CHARSET;
     LogFont.lfQuality = PROOF_QUALITY;
     _tcsncpy_s(LogFont.lfFaceName,LF_FACESIZE,m_csFontName.GetBuffer(),min(m_csFontName.GetLength(),LF_FACESIZE));
     const LOGFONT * lpLogFont = &LogFont;
     m_Font.CreatePointFontIndirect(lpLogFont);
    }

    BEGIN_MESSAGE_MAP(CTextEdit, CEdit)
     ON_WM_CTLCOLOR_REFLECT()
    END_MESSAGE_MAP()

    // CTextEdit message handlers
    HBRUSH CTextEdit::CtlColor(CDC* pDC, UINT nCtlColor)
    {
     // Change any attributes of the DC here
     pDC->SetTextColor(m_crTextColorRGB);
     pDC->SetBkColor(m_crBackColorRGB);
     pDC->SetBkMode(OPAQUE);
     if (!m_bSetFont)
     {
      SetFont(&m_Font);
      m_bSetFont = true;
     }

     // Return a non-NULL brush if the parent's handler should not be called
     return m_BackGround;
    }

    Wednesday, November 7, 2012 4:13 PM