[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 172
  • Last Modified:

Easy from a beginner

I'm new to Vc and MFC, this is a simple question that i don't know how to solve it.

How can i change the font, and colors of a static, and text edit control?
0
PedroMVGomes
Asked:
PedroMVGomes
1 Solution
 
Answers2000Commented:
Sorry this is a bit disjointed (cut and pasting from my previous answers and my notes)

COLORS


   You can do this in the dialog, or derive a class from CEdit (and override the reflection
   version which has an = sign before the name).

   When you override this message you
   1. Can set the text color using
   pDC->SetTextColor( RGB(whatever) ) ;

   2. Can set the background using
   pDC->SetBkColor( RGB(whatever) ) ;
   and returning an HBRUSH wish a solid color


   Therefore the steps are
   A. Add an HBRUSH to the dialog class as a member variable
   B. In your constructor create a brush or brushes

m_hBrush = ::CreateSolidBrush( RGB(whatever) ) ;

//     example

//     m_hBrushWhite = ::CreateSolidBrush( RGB(255,255,255) ) ;

   C. In your destructor destroy the brush or brushes

   DeleteObject( m_hBrush ) ;

   D. Override the WM_CTLCOLOR message.  You can check it's the control you're
   interesting in by checking the pWnd and nCtlColor values

   example
   HBRUSH CMyDialog::OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor )
   {
      if ( _red_ )
      {
        pDC->SetTextColor( RGB(255,0,0) ) ;
        pDC->SetTextColor( RGB(255,255,255) ) ;
        return m_hBrush ;
      }

      // etc
   }

   E. When you want to force a repaint of the particular control set a flag indicating if it's red,
   green or whatever, then force a repaint as

   GetDlgItem(ID_MYEDIT)->InvalidateRect( NULL, TRUE ) ;
   GetDlgItem(ID_MYEDIT)->UpdateWindow() ;

   F. So you know RGB values you  might find useful

   RGB(255,0,0) =  bright red
   RGB(0,0,255) = bright green
   RGB(255,255,255) = white
   RGB(0,0,0) = black

   RGB(128,0,0) = dark red
   RGB(0,0,128) = dark green

Procedure is the same for static & edit controls



CHANGING FONT

         Send a WM_SETFONT message to the control to set the font.

         You will need an HFONT handle to a font.  You can get one use GetStockObject or by creating a font using CreateFont/CreateFontIndirect

         The WM_SETFONT message includes 2 params :
         (i) WPARAM = the HFONT of the new font
         (ii) LPARAM = contains a flag indicating if control should redraw on immediately as a result of the font change.

         Say m_edit is your CEdit associated with the control

         // set to fixed pitch font
         m_edit.SendMessage( WM_SETFONT, (WPARAM)(HFONT)::GetStockObject(ANSI_FIXED_FONT), MAKELPARAM(TRUE,0) ) ;

Here's how to do custom fonts  :-

         (a) Add HFONT member varaible to the dialog, say m_hFont ;
         (b) Look up LOGFONT structure in the help.  Declare one and fill in your constructor for the dialog class, then create font using this
         LOGFONT lf ;
         lf.lfHeight = ...etc.
         //etc
         m_hFont = ::CreateFontIndirect(&lf) ;
         (c) In OnInitDialog use WM_SETFONT message to set this font as I showed in my prev comment
         (d) In your destructor delete the font
         DeleteObject(m_hFont)

The procedure for statics & edits is again the same
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now