Click here to Skip to main content
Licence 
First Posted 19 May 2003
Views 47,944
Bookmarked 25 times

A focus-sensitive EditBox Class

By | 19 May 2003 | Article
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

About the Author

DavidRipple



China China

Member

A Guy majors in Computational Mathmatics in WuHan University ,China.
Loving programming with MFC,ATL/WTL,COM,WinSock,PHP
DirectX.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 2 PinmemberMorries19:30 30 Nov '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 20 May 2003
Article Copyright 2003 by DavidRipple
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid