Solved

Changing the Edit Control Fonts and Foreground Color

Posted on 1998-10-08
7
477 Views
Last Modified: 2013-12-03
I want to change Edit control font and it foreground color.

0
Comment
Question by:aasif
[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
  • 4
  • 3
7 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 50 total points
ID: 1323097
1. To change font use WM_SETFONT message.  Send it to the control.

2. To change foreground color handle WM_CTLCOLOR message

More details on this in my comment below (typing it now)
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1323098
1. 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) ) ;


CHANGING THE COLOR

Edit control's send a WM_CTLCOLOREDIT message to the parent of the control (e.g. the dialog containing it) which you can handle to change the color of the control.

In MFC this will show in class wiz as WM_CTLCOLOR in the parent class, or if you derive your edit control class from CEdit as =WM_CTLCOLOR (message reflection).  See TN062 for more details on this.

(I) For example to handle in the dialog/window containing the edit
(a) over-ride WM_CTLCOLOR, check it's the control you're interested in by checking the control id against the one you want (this is passed as a param in OnCtlColor)
(b) use SetTextColor to set the foreground color
(c) use SetBkColor to set the background color (you should also return an HBRUSH handle to a solid brush created using CreateSolidBrush to change the color)

OR (II) To create a self contained color-edit control
(a) Use Class Wiz to create a class derived from CEdit, say CTColorEdit
(b) Add =WM_CTLCOLOR handling to this
(c) In dialog add a CEdit member variable to the control using class wiz, then modify the source to change CEdit to CTColorEdit.

Here's a CTColorEdit I did earlier

The .h
#if !defined(AFX_COLEDIT_H__181F26C2_FFA0_11D1_A899_E46B7D4B7F46__INCLUDED_)
#define AFX_COLEDIT_H__181F26C2_FFA0_11D1_A899_E46B7D4B7F46__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
// coledit.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CTColorEdit window

class CTColorEdit : public CEdit
{
// Construction
public:
      CTColorEdit();

// Attributes
public:

// Operations
public:

// Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CTColorEdit)
      //}}AFX_VIRTUAL

// Implementation
public:
      virtual ~CTColorEdit();

      // Generated message map functions
protected:
      //{{AFX_MSG(CTColorEdit)
      afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
      //}}AFX_MSG

      DECLARE_MESSAGE_MAP()
private:
      HBRUSH m_hbr ;
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_COLEDIT_H__181F26C2_FFA0_11D1_A899_E46B7D4B7F46__INCLUDED_)


The .cpp
// coledit.cpp : implementation file
//

#include "stdafx.h"
#include "comply2k.h"
#include "coledit.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

const COLORREF crBack = RGB(0,0,255) ;
const COLORREF crFore = RGB(255,255,255) ;

/////////////////////////////////////////////////////////////////////////////
// CTColorEdit

CTColorEdit::CTColorEdit()
{
      m_hbr = CreateSolidBrush( crBack ) ;
}

CTColorEdit::~CTColorEdit()
{
      DeleteObject( m_hbr ) ;
}


BEGIN_MESSAGE_MAP(CTColorEdit, CEdit)
      //{{AFX_MSG_MAP(CTColorEdit)
      ON_WM_CTLCOLOR_REFLECT()
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTColorEdit message handlers

HBRUSH CTColorEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
      pDC->SetBkColor( crBack ) ;
      pDC->SetTextColor( crFore ) ;

      return m_hbr ;
}


In this example crBack is the background, crFore is the foreground.  In this I choose a blue background with white text foreground.  Modify the RGB values to make your own colors.
0
 

Author Comment

by:aasif
ID: 1323099
Since I am new in MFC therefore, I couldn't find this is enough for me, Could you plz give me some code example. I am using the Edit control in a Dialog application.

I am explaining little bit more the requirement,

1) How I set the a particular font to my Edit control.
2) What should I do for changing the foreground color with WM_CTLCOLOR message.

TIA


0
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!

 

Author Comment

by:aasif
ID: 1323100
Just one more clearification,

I have a font installed in my Fonts directory, having name,

urdu web

Plz tell me with code, how can I select it with my Edit control,

TIA

0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1323101
1) How I set the a particular font to my Edit control.

(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)

2) What should I do for changing the foreground color with WM_CTLCOLOR message.

I thought I had explained this in 6:03 comment.  Here are steps again
(a) Put my header into a new .h file, say coledit.h
(b) Put my cpp into new .cpp file, say coledit.cpp (remove #include comply2k.h line)
(c) Add this to your project, and compile
(d) In dialog .cpp #'include coledit.h before the dialog includes its own header
(e) Use class wiz (Ctrl-W) / member variables tab to associate a CEdit control with the req'd control, say m_edit
(f) edit the dialog's .h file, and change the line that says something like:
CEdit m_edit
into
CTColorEdit m_edit
Be careful to preserve any punctuation on this line

0
 

Author Comment

by:aasif
ID: 1323102
I lookup the LOGFONT structure in the help file. Should I place

lf.lfFaceName = "urdu web"

for a particluar font, which is present in my fonts directory?

TIA


0
 

Author Comment

by:aasif
ID: 1323103
Although Answers2000 didn't answer my last query. I have solve it by myself. But I am giving him points.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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 …
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 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