CIPAddressCtrl BG/TEXT Color 
Author Message
 CIPAddressCtrl BG/TEXT Color

Hello,
    Could some one guide me as to whats the proper way to give a different
bg/text color to a CIPAddressCtrl control?
MFC does not send a WM_CTLCOLOR message for CIPAddressCtrl.

Regards,
Adrian.



Wed, 05 May 2004 01:18:17 GMT  
 CIPAddressCtrl BG/TEXT Color
Hi!

Quote:

> Hello,
>     Could some one guide me as to whats the proper way to give a different
> bg/text color to a CIPAddressCtrl control?
> MFC does not send a WM_CTLCOLOR message for CIPAddressCtrl.

> Regards,
> Adrian.

Well, here's my quick hack. It begins with deriving a new class from CIPAddressCtrl -- which I'll
call CColorIPAddressCtrl, and using ClassWizard to add handlers for the WM_CTLCOLOR and WM_PAINT
messages, and adding a private helper function to the implementation file. They go something like
this...

    static BOOL CALLBACK EnumChildProc(HWND hWnd, LPARAM lParam)
    {
        union {
            LPARAM lp;
            struct {
                RECT rgRects[4];
                INT cRects;
            } *Ics;
        } p = { lParam };

        return (p.Ics && ::GetWindowRect(hWnd, &p.Ics ->rgRects[p.Ics ->cRects]))
            ? ++p.Ics ->cRects : FALSE;
    }

    HBRUSH CColorIPAddressCtrl::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
        CWnd* pParent = GetParent();
        if (pParent && nCtlColor == CTLCOLOR_EDIT)
        {
            return (HBRUSH)pParent->SendMessage(
                WM_CTLCOLOREDIT, (WPARAM)pDC->m_hDC, (LPARAM)m_hWnd);
        }
        return (HBRUSH)Default();
    }

    void CColorIPAddressCtrl::OnPaint()
    {
        CWnd* pParent = GetParent();
        if (!pParent)
            Default();
        else
        {
            struct {
                RECT rgRects[4];
                INT cRects;
            } ics = { 0 };

            if (EnumChildWindows(m_hWnd, EnumChildProc,
                (LPARAM)&ics) && (ics.cRects == 4))
            {
                CPaintDC dc(this);
                CRect rc;

                COLORREF crFore;
                COLORREF crBk;

                if (dc.SaveDC())
                {
                    pParent->SendMessage(
                        WM_CTLCOLOREDIT, (WPARAM)dc.m_hDC, (LPARAM)m_hWnd);
                    crFore = dc.GetTextColor();
                    crBk = dc.GetBkColor();
                    dc.RestoreDC(-1);
                }
                else
                {
                    crFore = GetSysColor(COLOR_WINDOWTEXT);
                    crBk = GetSysColor(COLOR_WINDOW);
                }

                dc.GetClipBox(&rc);
                dc.FillSolidRect(&rc, crBk);

                while (--ics.cRects)
                {
                    rc = ics.rgRects[ics.cRects];
                    rc |= ics.rgRects[ics.cRects-1];

                    ScreenToClient(&rc);

                    dc.SetPixel(rc.CenterPoint(), crFore);
                }
            }
        }
    }

In this implementation the IP control now gets it's foreground and background colors from it's
parent by sending it a WM_CTLCOLOREDIT message. So now, you can add a handler to the parent (or add
a WM_CTLCOLOR_REFLECT handler to the class) and set the colors something like so...

    HBRUSH CParentDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
        if (pWnd->m_hWnd == m_addressColor.m_hWnd)
        {
            static CBrush brush(RGB(128,0,128));

            pDC->SetTextColor(RGB(255,255,255));
            pDC->SetBkColor(RGB(128,0,128));
            return brush;
        }
        return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    }

It's hardly tested, so watch out for bugs. But anyway...

HTH,

Jeff...
--
Please post all follow-ups to the newsgroup only.



Wed, 05 May 2004 13:35:13 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Question: How to set text fore/bg color in console applications

2. change bg color on controls

3. How to change BG color of a SDI ??

4. bg color in checkbox ?

5. How to change dialog's bg color ?

6. Changing BG color in CFormView

7. CIPAddressCtrl - background color

8. Background Color, Text Color

9. Change CButton color and CButton text color in CDialog

10. Changing text colors(2 different colors) in ToolTips

11. Background color and text color in menu

12. Change text color and background color in CEditView

 

 
Powered by phpBB® Forum Software