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:
I click on edit control and type invalid input.
I move mouse pointer elsewhere, but edit control still has focus.
Now I delete invalid input by pressing backspace.
As soon as input becomes valid the color changes properly, but borders become thicker/darker.
These pictures illustrate the problem:
Edit control before typing in data:
Edit control when user pastes invalid data ( mouse pointer is in grey area ):
The last character is r
.
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:
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 ) :
// 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?
I have found a solution to my problem. I just added RedrawWindow
instead of InvalidateRect
and ordered frame to be redrawn as well :
// 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;