Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A focus-sensitive EditBox Class

0.00/5 (No votes)
19 May 2003 1  
Change the back color of an edit box when it gets the focus, and back to another color when focus is lost

Sample Image - CoolEdit.jpg

Introduction

I wanted a focus-sensitive Editbox to make my GUI program more attractive. I found one in codetools, which was distributed by Warren J. Hebert. However I downloaded it and found it worked perfectly only when the Editbox is single line style. But my application wants the EditBox to be a multi-line .So I wrote one! This is CEditEx with the features below:

  • Change the back color of an edit box when it gets the focus, and back when it loses the focus.
  • Use the flat scroll bar instead of the default behavior.

The Implemention of this class:

The following snippets are used to track to mouse
CEditEx::OnMouseMove(UINT nFlags, CPoint point)
{
    CEdit::OnMouseMove(nFlags, point);

    if (m_bIsFocused)
        return;

    if (GetCapture() != this)
    {
        // come in for the first time

        m_bMouseOver = TRUE;
        SetCapture();
        Invalidate(TRUE);
    }
    else
    {
        CRect rect;

        GetClientRect(&rect);

        if (!rect.PtInRect(point))
        {
            //Mouse move out of Edit control

            m_bMouseOver = FALSE;
            Invalidate(TRUE);
            ReleaseCapture();
        }
    }
}

void CEditEx::OnSetFocus(CWnd* pOldWnd) 
{
    CEdit::OnSetFocus(pOldWnd);

    m_bMouseOver=TRUE;
    Invalidate(TRUE);
}

void CEditEx::OnKillFocus(CWnd* pNewWnd)
{
    CEdit::OnKillFocus(pNewWnd);

    m_bMouseOver=FALSE;
    Invalidate(TRUE);
}
Change the scrollbar appearance
// In the Message map

ON_CONTROL_REFLECT(EN_VSCROLL, OnVscroll)

void CEditEx::OnVscroll()
{
    InitializeFlatSB(GetSafeHwnd());
    // Use the flat scrollbar feature provided by Microsoft.

    // See MSDN for this API

}
//Change the background color of EditBox according to different contex.

// In the Message map

ON_WM_CTLCOLOR_REFLECT()

CEditEx::CEditEx()
{
    m_bMouseOver = FALSE;
    brHot.CreateSolidBrush(RGB(255, 255, 255));
    br.CreateSolidBrush(RGB(221, 221, 221));
}

HBRUSH CEditEx::CtlColor(CDC* pDC, UINT nCtlColor)
{
    if (m_bMouseOver)
    {
        pDC->SetBkColor(RGB(255, 255, 255));
        return brHot;
    }
    else
    {
        pDC->SetBkColor(RGB(221, 221, 221));
        return br;
    }
}

To use the CEditEx class, you should do as follows:

  • Add an EditBox into your application. Change the style to be multi-line, No border, Want Return, Vertical Scrollbar.
  • In your application add a member variable derived from CEditEx. See, for example testDlg.cpp:
CEditEx m_ctlEdit1;

Points of Interest

Now I'm a senior student of WuHan university in China. I program with MFC, TCP/IP, PHP, javascript, C, HTML and I am also interested in Network Security,DirectX etc.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here