【问题标题】:Change frame color of CEdit when Focus焦点时更改CEdit的框架颜色
【发布时间】:2021-12-30 11:03:38
【问题描述】:

使用以下代码,我的编辑框会在获得焦点时获得红色框和黄色背景。但是,当我输入文本时,起初文本是不可见的,我必须将光标悬停到另一个对话框项才能看到我刚刚输入的文本。为什么我必须更改才能看到文本而无需移动鼠标?

HBRUSH CDialog23::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
    if (nCtlColor == CTLCOLOR_STATIC || nCtlColor == CTLCOLOR_DLG)
    {
        pDC->SetBkMode(TRANSPARENT);
        pDC->SetBkColor(RGB(255, 255, 255));
        hbr = m_Weiss;
        return hbr;
    }
    if (nCtlColor == CTLCOLOR_LISTBOX)
    {
        pDC->SetBkMode(TRANSPARENT);
        pDC->SetBkColor(RGB(255, 230, 153));
        hbr = m_Gelb;
        return hbr;
    }
    const CWnd* focusWnd = GetParent() ? GetParent()->GetFocus() : nullptr;
    bool hasFocus = focusWnd && focusWnd->GetSafeHwnd() == pWnd->GetSafeHwnd();
    if (hasFocus)
    {
        if (pWnd->GetDlgCtrlID() == pWnd->GetFocus()->GetDlgCtrlID() && nCtlColor != CTLCOLOR_STATIC)
        {
            pDC->SetBkMode(TRANSPARENT);
            pDC->SetBkColor(RGB(255, 230, 153));

            CRect rect;
            pWnd->GetWindowRect(&rect);
            rect.OffsetRect(-rect.left, -rect.top);
            CBrush brush(RGB(255, 0, 0));
            pDC->FrameRect(&rect, &brush);
            ReleaseDC(pDC);
            
            hbr = m_Gelb;
            return hbr;
        }
        else
        {
            pDC->SetBkMode(TRANSPARENT);
            pDC->SetBkColor(RGB(255, 255, 255));
            hbr = m_Weiss;
            return hbr;
        }
    }
    return hbr;
}

【问题讨论】:

    标签: c++ mfc


    【解决方案1】:

    删除ReleaseDC(); 您没有调用获取 DC,调用者将指向 DC 对象的指针传递给您。完全没有必要释放这个 DC。

    更新: 使用 MEMC(最小编码最大效果) 我做了更多的工作并提出了以下解决方案:

    • CEdit派生一个类

    • 处理反射消息:EN_SETFOCUSEN_KILLFOCUSWM_CTLCOLOR

    • WM_NCPAINT 处理程序中绘制框架 声明成员boolean 变量以确定何时应发生焦点更改。我称它为m_DoChangeBacground。其余如下:

       void CEditChangeFocus::OnEnSetfocus()
       {
           m_DoChangeBacground = true;
       }
      
      
       void CEditChangeFocus::OnEnKillfocus()
       {
           m_DoChangeBacground = false;
       }
      
      
       HBRUSH CEditChangeFocus::CtlColor(CDC* pDC, UINT nCtlColor)
       {
           // just the sanity check while debugging
           ASSERT(CTLCOLOR_EDIT == nCtlColor);
      
           if(m_DoChangeBacground)
           {
               pDC->SetBkMode(TRANSPARENT);
               //pDC->SetBkColor(RGB(255, 230, 153));
      
               return m_YellowBK;  // yellow brush
      
           }
           // TODO:  Return a non-NULL brush if the parent's handler should not be called
           return nullptr;
       }
      
      
       void CEditChangeFocus::OnNcPaint()
       {
           if(m_DoChangeBacground)
           {
               CRect rectWnd;
               GetWindowRect(&rectWnd);
               rectWnd.OffsetRect(-rectWnd.left - 2, -rectWnd.top - 2); // do painting in non client area
               CDC* pDC = GetDC();
               pDC->FrameRect(&rectWnd, &m_RedFrm);
               ReleaseDC(pDC);
           }
           else
           {
               CEdit::OnNcPaint();
           }
           // TODO: Add your message handler code here
           // Do not call CEdit::OnNcPaint() for painting messages
           // WRONG! do call it if not handled by your code
       }
      

    【讨论】:

      【解决方案2】:

      使用以下代码,我将自定义颜色集中在CEdit

      HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
      {
          HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
      
          // TODO: Change any attributes of the DC here
      
          switch(nCtlColor)
          {
          case CTLCOLOR_EDIT:
          case CTLCOLOR_MSGBOX:
              if(IDC_EDIT_HEX == pWnd->GetDlgCtrlID())
              {
                  pDC->SetBkColor(RGB(210, 210, 240));
                  hbr = static_cast<HBRUSH>(m_pBrush->GetSafeHandle());
              }
              break;
          }
      
          // TODO: Return a different brush if the default is not desired
          return hbr;
      }
      

      对你有帮助吗?

      【讨论】:

        猜你喜欢
        • 2010-09-18
        • 1970-01-01
        • 1970-01-01
        • 2016-01-22
        • 2020-05-11
        • 1970-01-01
        • 1970-01-01
        • 2014-06-18
        相关资源
        最近更新 更多