Solved

Easy from a beginner

Posted on 1998-10-29
1
163 Views
Last Modified: 2013-11-20
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
Comment
Question by:PedroMVGomes
1 Comment
 
LVL 8

Accepted Solution

by:
Answers2000 earned 50 total points
ID: 1324012
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
deburging in oracle form 12 77
matchUp  challenge 6 49
Not needed 13 58
Making an alias 7 54
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now