Change the background and foreground color of an MFC Edit Box

shabbir's Avatar author of Change the background and foreground color of an MFC Edit Box
This is an article on Change the background and foreground color of an MFC Edit Box in MFC.
Rated 5.00 By 2 users
Define a CBrush pointer in your dialog Class
Code:
CBrush *brush;
Initialize the brush pointer in the constructor of your Dialog
Code:
brush = new CBrush(RGB(49,49,49));
Add the WM_CTLCOLR Message handler for the dialog and add the following code
Code:
	switch (nCtlColor) {
 
 	case CTLCOLOR_EDIT:
 		pDC->SetTextColor(RGB(0, 255, 0));
 		pDC->SetBkColor(RGB(0, 0, 0));
 		return (HBRUSH)(brush->GetSafeHandle());
 	default:
 		return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
 	}
Ambitious contributor
28May2005,09:57   #2
alok's Avatar
Here sample Application!
Attached Files
File Type: zip ChangeEditColor.zip (9.4 KB, 952 views)
Go4Expert Founder
28May2005,10:04   #3
shabbir's Avatar
Nice sample.
Light Poster
14Apr2006,15:54   #4
fenomeno83's Avatar
how can I insert an image as background of dialog box?
And How I change font of edit box(but static text must be another font)?
thanks

Last edited by fenomeno83; 14Apr2006 at 16:18..
Go4Expert Founder
14Apr2006,18:02   #5
shabbir's Avatar
Making it as a seperate threads will have more responses but I will give you some hints as how you can do that.

Quote:
how can I insert an image as background of dialog box?
There are couple of ways to be doing this.
1. You need to be doing is Derive a class from the CDialog and use that class for your dialog and in the PAINT message just use the bitblt function to render the image.
2. Add the PAINT message handler to the current dialog and draw the image to the DC of the paint method.
Quote:
And How I change font of edit box
Using the SetFont method in the InitDialog function of the dialog box where the control is placed.
Quote:
(but static text must be another font)?
That will remain.
Light Poster
14Apr2006,18:59   #6
fenomeno83's Avatar
can I have a simple example on which create a custom background and a font different between static text and edit box text?
thanks
Go4Expert Founder
14Apr2006,19:12   #7
shabbir's Avatar
Quote:
Originally Posted by fenomeno83
can I have a simple example on which create a custom background and a font different between static text and edit box text?
thanks
If you post it as seperate thread there will be samples coming your way soon.
Newbie Member
17Jan2007,14:58   #8
Arvind22's Avatar
Thanks for your application. It helped me change the text colors of various controls.
Newbie Member
23Apr2008,18:27   #9
phunkydizco's Avatar
Is it possible to change the foreground color of a specific part of the text inside an edit box? For example "Error" should be red and the rest black.
Newbie Member
24Jul2008,14:10   #10
Ummar's Avatar
Quote:
Originally Posted by shabbir
Define a CBrush pointer in your dialog Class
Code:
CBrush *brush;
Initialize the brush pointer in the constructor of your Dialog
Code:
brush = new CBrush(RGB(49,49,49));
Add the WM_CTLCOLR Message handler for the dialog and add the following code
Code:
	switch (nCtlColor) {
 
 	case CTLCOLOR_EDIT:
 		pDC->SetTextColor(RGB(0, 255, 0));
 		pDC->SetBkColor(RGB(0, 0, 0));
 		return (HBRUSH)(brush->GetSafeHandle());
 	default:
 		return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
 	}


Fine piece of code , very simple yet powerful.