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);
 	}
0
alok's Avatar
Ambitious contributor
Here sample Application!
Attached Files
File Type: zip ChangeEditColor.zip (9.4 KB, 962 views)
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Nice sample.
0
fenomeno83's Avatar
Light Poster
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..
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
0
fenomeno83's Avatar
Light Poster
can I have a simple example on which create a custom background and a font different between static text and edit box text?
thanks
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
0
Arvind22's Avatar, Join Date: Jan 2007
Newbie Member
Thanks for your application. It helped me change the text colors of various controls.
0
phunkydizco's Avatar, Join Date: Apr 2008
Newbie Member
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.
0
Ummar's Avatar
Newbie Member
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.