Click here to Skip to main content
15,885,870 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an edit control which background color depends from validity of user input.

If input is valid edit control should keep default look, else the background color should change to light gray.

I am checking validity in EN_UPDATE handler and if it is invalid I store the HWND of the edit control into vector.

Visual Styles are enabled.

The problem comes when I change the position of the mouse pointer. Let me describe it:

1. I click on edit control and type invalid input.

2. I move mouse pointer elsewhere, but edit control still has focus.

3. Now I delete invalid input by pressing backspace.

4. As soon as input becomes valid the color changes properly, but borders become thicker/darker.

These pictures illustrate the problem:

1. Edit control before typing in data: http://pbrd.co/Scn5QK[^]

2. Edit control when user pastes invalid data ( mouse pointer is in grey area ): http://pbrd.co/Scnvqc[^]

The last character is r.

3. Now if mouse pointer is out of edit control's client area ( on dialog's client area for example ) and user deletes r here is what I get: http://pbrd.co/ScnHWw[^]

Notice the thicker border.

When mouse pointer hovers above the edit control it gets repainted properly.

Here are the relevant code snippets ( if needed I can submit a small SSCCE ) :
C++
// minimal code snippet for EN_UPDATE
case WM_COMMAND:
{
    switch( LOWORD(wParam) )
    {
    case IDC_MYEDIT:
        {
            if( HIWORD(wParam) == EN_CHANGE )
            {
                if( /* invalid input */ )
                {
                    // store HWND into vector

                } 
                // InvalidateRect(...); // tried this too...
            }
        }
        break;

// minimal code snippet for WM_CTLCOLOREDIT
case WM_CTLCOLOREDIT:
    {
        if( /* this control is stored in vector */ )
        {
            //=== then this is invalid entry->paint it grey ===//

            // Needed SetBkMode for text's background transparency 
            SetBkMode( (HDC)wParam, TRANSPARENT ); 
            // return light gray brush 
            return (INT_PTR)( (HBRUSH)GetStockObject( LTGRAY_BRUSH ) );
        }
        else
            return DefWindowProc( ... );  // default processing
    }

How can I fix this?
Posted

1 solution

I have found a solution to my problem. I just added RedrawWindow instead of InvalidateRect and ordered frame to be redrawn as well :

C++
// minimal code snippet for EN_UPDATE
case WM_COMMAND:
{
    switch( LOWORD(wParam) )
    {
    case IDC_MYEDIT:
        {
            if( HIWORD(wParam) == EN_CHANGE )
            {
                if( /* invalid input */ )
                {
                    // store HWND into vector
 
                }

                // after finishing validation, redraw window INCLUDING THE FRAME !!
                // This solves the problem with edges entirely !!!

                RedrawWindow( (HWND)lParam, NULL, NULL,
                    RDW_ERASE | RDW_FRAME | RDW_INVALIDATE );
            }
        }
        break;
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900