问题描述
我在一个对话框中有编辑控件,检查输入是否有效。
我应该通过更改编辑控件的背景颜色来指示有效性,如果输入是无效,否则我什么都不做。
我正在检查 EN_CHANGE
处理程序中的输入,如果输入无效我将编辑控件的句柄存储在向量中。最后我调用 InvalidateRect((HWND)lParam,NULL,TRUE);
所以编辑控件可以用适当的颜色重新绘制。
要重新编辑编辑控件,我正在处理 WM_CTLCOLOREDIT
,如下所示:
case WM_CTLCOLOREDIT:
{
bool IsInvalid = 假跨度>; // 此编辑控件是否包含无效文字?
// vector InvalidInput包含编辑控件的句柄
// 输入无效,所以我们检查我们的窗口是否存储在那里
(向量< HWND> :: size_type i = 0 ;
!IsInvalid&&(i< InvalidInput.size()); i ++)
{
if (InvalidInput [i] ==(HWND)lParam)
IsInvalid = 真跨度>;
}
// 如果输入无效,请将背景颜色更改为浅灰色
if (IsInvalid)
{
// 需要用于文本背景透明度的SetBkMode
SetBkMode((HDC)wParam,TRANSPARENT);
// 返回浅灰色画笔
return (INT_PTR)((HBRUSH)GetStockObject(LTGRAY_BRUSH));
}
else
return FALSE; // 说我们没有处理它
// 所以对话程序可以为我们做到这一点
}
之后我启动程序编辑控件正确绘制。
输入有效的输入后,编辑控件被正确绘制。
我在之后立即输入无效字符后,背景颜色为浅灰色,一切似乎都正常。
如果删除无效字符然后背景保持灰色而不是返回默认系统颜色。
我做错了什么,我应该如何解决这个问题?
如果我放 InvalidateRect()
在我的 WM_COMMAND
处理程序中 IDC_MYEDIT
然后问题似乎消失了:
case WM_COMMAND:
{
switch (LOWORD( wParam))
{
case IDC_MYEDIT:
{
if (HIWORD(wParam)== EN_CHANGE)
{
// 进行验证东西
}
InvalidateRect(...);
}
break ;
// 代码的其余部分......
谢谢。
祝你好运。
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. In the end I call 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:
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:
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...
Thank you.
Best regards.
这篇关于更改编辑控件的背景颜色的困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!