Home Search Contact us About us
Title Colouring Edit box backgrounds.
Summary Colouring the background of an edit box makes it easier for the user to determine which edit is currently being edited.
Contributor John McTainsh
Published 4-Oct-2000
Last updated 4-Oct-2000
Page rating   92% for 5 votes Useless Brilliant

Description.

As the user tabs or moves from edit box to edit box in a form or dialog it can be dificult to see which box has focus. To help the user it is nice to see the selected edit box highlighted in a different colour.

How to do this.

Add a class member variable.
    HBRUSH m_hbr;
In the constructor for the window add.
    VERIFY( m_hbr = CreateSolidBrush( RGB( 255, 0, 0 ) );
In the destructor for the window add.
    DeleteObject( m_hbr );

the Form or Dialog class using the class wizard add message handlers for OnCommand and WM_CTLCOLOR replace the wizard generated code with the following.

// ***************************************************************************
//DESCRIPTION:
//      Catch SetFocus and KillFocus to highlight the window that
//      is currently active.
//PARAMS:
//      See Help
//RETURN:
//      See Help
//CREATED:
//      4-10-2000, 12:04:27 by john@mctainsh.com
// ***************************************************************************
BOOL CWxyzView::OnCommand(WPARAM wParam, LPARAM lParam) 
{
    TRACE( _T("CWxyzView::OnCommand(%d,%ld) \n"), wParam, lParam );
    
    //
    //Refresh the Display when a Control gets or looses focus
    //
    if( ( HIWORD( wParam ) == EN_SETFOCUS ) ||
        ( HIWORD( wParam ) == EN_KILLFOCUS ) )
    {
        UINT nID = LOWORD( wParam );
        if( nID != 0 )
        {
            CWnd* pWnd = GetDlgItem( nID );
            if( pWnd != NULL )
            {
                pWnd->Invalidate();
            }
        }    
    }
    return CFormView::OnCommand(wParam, lParam);
}

// ***************************************************************************
//DESCRIPTION:
//        Setup screen colours for the Edit boxes
//PARAMS:
//        See help
//RETURN:
//        See Help
//CREATED:
//        4-10-2000, 13:57:07 by john@mctainsh.com
// ***************************************************************************
HBRUSH CWxyzView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
    HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);

    //Change edit boxes only
    if( nCtlColor == CTLCOLOR_EDIT )
    {
        //If it has the focus then change the text and background
        if( pDC->GetWindow( ) == GetFocus() )
        {
            pDC->SetTextColor( RGB( 255, 255, 255 ) );
            pDC->SetBkColor( RGB( 255, 0, 0 ) );
            DeleteObject( hbr );
            hbr = m_hbr;
        }
    }
    return hbr;
}
Comments Date
Home Search Contact us About us