Solved

Easy from a beginner

Posted on 1998-10-29
1
168 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASCII Non-Printable characters/codes and their HTML equivalents 6 159
Window placement 17 99
Thin secure Windows 10 5 116
unix example issues 18 113
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: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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