Go4Expert

Go4Expert (http://www.go4expert.com/)
-   MFC (http://www.go4expert.com/articles/mfc-tutorials/)
-   -   Change the background and foreground color of an MFC Edit Box (http://www.go4expert.com/articles/change-background-foreground-color-mfc-t320/)

shabbir 24Jul2008 15:05

Re: Change the background and foreground color of an MFC Edit Box
 
Quote:

Originally Posted by Ummar
Fine piece of code , very simple yet powerful.

Thanks.

Ummar 7Aug2008 12:02

Re: Change the background and foreground color of an MFC Edit Box
 
It's a very good hint.
Thanx!
Ummar

piyumali2004 1Jan2009 09:22

Re: Change the background and foreground color of an MFC Edit Box
 
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.

Ummar 2Jan2009 11:58

Re: Change the background and foreground color of an MFC Edit Box
 
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!

shabbir 2Jan2009 13:58

Re: Change the background and foreground color of an MFC Edit Box
 
That was nice Ummar and some reputation your way.

piyumali2004 5Jan2009 11:18

Re: Change the background and foreground color of an MFC Edit Box
 
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

shabbir 5Jan2009 12:48

Re: Change the background and foreground color of an MFC Edit Box
 
Offtopic comment:
If you liked some post add to its reputation by clicking http://www.go4expert.com/images/buttons/reputation.gif beside that post

timkhobau 24Aug2009 17:12

Re: Change the background and foreground color of an MFC Edit Box
 
Thanks you very much.

coolerfantasy 1Oct2009 00:15

Re: Change the background and foreground color of an MFC Edit Box
 
thank you , great code and simplest , though the text still original size
and back ground color limited to text size ! :nice:


All times are GMT +5.5. The time now is 07:08.