1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Change the background and foreground color of an MFC Edit Box

Discussion in 'MFC' started by shabbir, May 25, 2005.

  1. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    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);
     	}
     
  2. alok

    alok New Member

    Joined:
    Jul 24, 2004
    Messages:
    127
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    S/W Engg
    Home Page:
    Here sample Application!
     

    Attached Files:

  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    Nice sample.
     
  4. fenomeno83

    fenomeno83 New Member

    Joined:
    Apr 14, 2006
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    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: Apr 14, 2006
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    Making it as a seperate threads will have more responses but I will give you some hints as how you can do that.

    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.
    Using the SetFont method in the InitDialog function of the dialog box where the control is placed.
    That will remain.
     
  6. fenomeno83

    fenomeno83 New Member

    Joined:
    Apr 14, 2006
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    can I have a simple example on which create a custom background and a font different between static text and edit box text?
    thanks
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    If you post it as seperate thread there will be samples coming your way soon.
     
  8. Arvind22

    Arvind22 New Member

    Joined:
    Jan 17, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for your application. It helped me change the text colors of various controls.
     
  9. phunkydizco

    phunkydizco New Member

    Joined:
    Apr 23, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  10. Ummar

    Ummar New Member

    Joined:
    Jul 24, 2008
    Messages:
    4
    Likes Received:
    1
    Trophy Points:
    0


    Fine piece of code , very simple yet powerful.
     
  11. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    Thanks.
     
  12. Ummar

    Ummar New Member

    Joined:
    Jul 24, 2008
    Messages:
    4
    Likes Received:
    1
    Trophy Points:
    0
    It's a very good hint.
    Thanx!
    Ummar
     
  13. piyumali2004

    piyumali2004 New Member

    Joined:
    Jan 1, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I'm new to the MFC programming. I need to change the back ground color of the edit boxes in my application. I hope the code provided in the first reply in this thread will do that. But I'm confused with the phrase "Add the WM_CTLCOLR Message handler for the dialog".

    Can any one please explain me how can I add that message handler?

    Thank You.
     
  14. Ummar

    Ummar New Member

    Joined:
    Jul 24, 2008
    Messages:
    4
    Likes Received:
    1
    Trophy Points:
    0
    A/A,

    Follow the following steps:

    1. Open the .h file (say "uFile.h") in which u want to change the color of a Textbox,
    2. Write the declaration as:
    public:
    afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
    CBrush *brush;

    3. Now open your .cpp (uFile.cpp) file,
    4. In the Message_Map write ON_WM_CTLCOLOR() message handler as:

    BEGIN_MESSAGE_MAP(CVCSDlg, CDialog)
    .
    .
    .
    .
    .
    .
    ON_WM_CTLCOLOR()

    END_MESSAGE_MAP()


    5. Write an event hanler for the Message_Handler as
    Code:
    HBRUSH CuFile::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    
    	// TODO:  Change any attributes of the DC here
    
    	switch (1) 
    	{ 
     		case 1:
     			pDC->SetTextColor(RGB(255, 255, 255));
     			pDC->SetBkColor(RGB(0, 0, 0));
     			return (HBRUSH)(brush->GetSafeHandle());
     		default:
     			return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
     	}
    	return hbr;
    }

    Alternatively you can do it as follows:
    1. Open the .h or .cpp,
    2. Open the properties Window,
    3. Click on Messages button at the top of this Properties Window,
    4. Search for "OnCtlColor",
    5. Click on the right side of it and add the "OnCtlColor",

    6. Now you need only to add the following code in the event handler that is generated automatically.

    Code:
    	switch (1) 
    	{ 
     		case 1:
     			pDC->SetTextColor(RGB(255, 255, 255));
     			pDC->SetBkColor(RGB(0, 0, 0));
     			return (HBRUSH)(brush->GetSafeHandle());
     		default:
     			return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
     	}
    	return hbr;
    
    That will work for sure.

    If u need further assistance, u r always welcome.

    Allah Hafiz!
     
    Last edited by a moderator: Jan 2, 2009
    shabbir likes this.
  15. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    That was nice Ummar and some reputation your way.
     
  16. piyumali2004

    piyumali2004 New Member

    Joined:
    Jan 1, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    Thank you very much for your reply. Actually I did it as you said it was successful. You have explained it very well. Thanks lot again for your help.

    Continue your good work.

    Regards

    Piyumali
     
  17. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    [COMMENT]If you liked some post add to its reputation by clicking [​IMG] beside that post[/COMMENT]
     
  18. timkhobau

    timkhobau New Member

    Joined:
    Aug 24, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Thanks you very much.
     
  19. coolerfantasy

    coolerfantasy New Member

    Joined:
    Sep 30, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    thank you , great code and simplest , though the text still original size
    and back ground color limited to text size ! :nice:
     

Share This Page