编辑控件没有使用选定的画笔完全重画 [英] Edit control not repainted entirely with chosen brush

查看:219
本文介绍了编辑控件没有使用选定的画笔完全重画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个编辑控件,其背景颜色取决于用户输入的有效性。



如果输入有效,编辑控件应保持默认外观,否则背景颜色应更改



我正在检查 EN_UPDATE 处理程序的有效性,如果它无效,我存储 HWND 的编辑控制向量



样式已启用。



问题出现在我更改鼠标指针的位置时。让我描述一下:


  1. 我点击编辑控制项并输入无效输入。


  2. 我将鼠标指针移动到其他位置,


这些照片说明了这个问题:



在输入数据之前编辑控件:



当用户粘贴无效数据(灰色区域中的鼠标指针)时,编辑控件:



最后一个字符是 r



现在,如果鼠标指针离开编辑控制的客户区域(例如对话框的客户区域)和用户删除 r 这里是我得到的:



注意较粗的边框。



当鼠标指针悬停在编辑控件上方时,它会被正确重绘。



这里是相关的代码片段如果需要我可以提交一个小的 SSCCE ):

  // EN_UPDATE $ b的最小代码片段$ b case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDC_MYEDIT:
{
if(HIWORD EN_CHANGE)
{
if(/ * invalid input * /)
{
//将HWND存储到向量

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

// WM_CTLCOLOREDIT的最小代码片段
case WM_CTLCOLOREDIT:
{
if(/ *此控件存储在向量* /中)
{
// ===那么这是无效的输入 - > paint it grey === //

//需要SetBkMode用于文本的背景透明度
SetBkMode((HDC)wParam , 透明 );
// return light gray brush
return(INT_PTR)((HBRUSH)GetStockObject(LTGRAY_BRUSH));
}
else
return DefWindowProc(...); //默认处理
}

如何解决?

解决方案

我找到了我的问题的解决方案。我刚刚添加了 RedrawWindow ,而不是 InvalidateRect 和有序框架重绘: / p>

  // EN_UPDATE的最小代码片段
case WM_COMMAND:
{
switch(LOWORD wbaram)== $
{
if(/ *无效的输入* wParam))
{
case IDC_MYEDIT:
{
if(HIWORD(wParam)== EN_CHANGE) /)
{
//将HWND存储到向量

}

//完成验证后,重绘窗口包含框架
/ /这解决了边缘的问题

RedrawWindow((HWND)lParam,NULL,NULL,
RDW_ERASE | RDW_FRAME | RDW_INVALIDATE);
}
}
断点;


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:

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;

这篇关于编辑控件没有使用选定的画笔完全重画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆