[英]Difficulties in changing the background color of edit control
I have edit control in a dialog box which input is checked for validity. 我在对话框中有编辑控件,该对话框检查输入的有效性。
I should indicate validity by changing the background color of an edit control if input is invalid , otherwise I should do nothing. 如果输入无效 , 我应该通过更改编辑控件的背景颜色来表示有效性,否则我什么也不做。
I am checking the input in EN_CHANGE
handler and if input is invalid I store the handle of the edit control in a vector. 我正在EN_CHANGE
处理程序中检查输入,如果输入无效,则将编辑控件的句柄存储在向量中。 In the end I call InvalidateRect( (HWND)lParam, NULL, TRUE );
最后,我调用InvalidateRect( (HWND)lParam, NULL, TRUE );
so edit control can be repainted with proper color. 因此可以使用适当的颜色重新绘制编辑控件。
To repaint edit control I am handling WM_CTLCOLOREDIT
like this: 要重画编辑控件,我正在像这样处理WM_CTLCOLOREDIT
:
case WM_CTLCOLOREDIT:
{
bool IsInvalid = false; // does this edit control hold invalid text ?
// vector InvalidInput contains handles of edit controls
// with invalid input, so we check if our window is stored there
for( vector<HWND>::size_type i = 0;
!IsInvalid && ( i < InvalidInput.size() ); i++ )
{
if( InvalidInput[i] == (HWND)lParam )
IsInvalid = true;
}
// if input is invalid change background color to light gray
if( IsInvalid )
{
// Needed SetBkMode for text background transparency
SetBkMode( (HDC)wParam, TRANSPARENT );
// return light gray brush
return (INT_PTR)( (HBRUSH)GetStockObject( LTGRAY_BRUSH ) );
}
else
return FALSE; // say we didn't handle it
// so dialog procedure can do that for us
}
After I start the program edit control is painted properly. 启动程序后,编辑控件会正确绘制。
After I type valid entry edit control is painted properly. 输入有效条目后,编辑控件将正确绘制。
After I type invalid character immediately after , background is colored into light gray and everything seems to work fine. 在我紧接着输入无效字符之后 ,背景颜色变为浅灰色,并且一切正常。
If I delete the invalid character then the background stays gray instead of returning to default system color. 如果我删除了无效字符,则背景将保持灰色,而不是返回默认的系统颜色。
What am I doing wrong and how should I fix this ? 我在做什么错,应该如何解决?
If I put InvalidateRect()
in my WM_COMMAND
handler for IDC_MYEDIT
then the problem seems to disappear: 如果我将InvalidateRect()
放在IDC_MYEDIT
WM_COMMAND
处理程序中,则问题似乎消失了:
case WM_COMMAND:
{
switch( LOWORD(wParam) )
{
case IDC_MYEDIT:
{
if( HIWORD(wParam) == EN_CHANGE )
{
//do your validation stuff
}
InvalidateRect(...);
}
break;
// the rest of the code...
The error is here 错误在这里
else
return FALSE; // say we didn't handle it
// so dialog procedure can do that for us
The WM_CTLCOLOREDIT
message is listed as one of the special exceptions to the rule that returning FALSE
means "not handled". WM_CTLCOLOREDIT
消息被列为该规则的特殊例外之一,即返回FALSE
意味着“未处理”。 It must be handled. 必须对其进行处理。 If you don't want to handle it, you can pass the message to DefWindowProc
. 如果您不想处理它,可以将消息传递给DefWindowProc
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.