ComboBox重绘

来源:互联网 发布:国际志愿者网络 编辑:程序博客网 时间:2024/06/11 18:48

How to subclass CListBox and CEdit inside of CComboBox

Article ID: 174667 - View products that this article applies to.
This article was previously published under Q174667
Expand all |Collapse all

On This Page

  • SUMMARY
    • MORE INFORMATION
      • Sample code
      • Properties
        • Give Feedback

          SUMMARY

          While it is simple to directly subclass a combo box control, it is not simple to subclass the edit or list box inside a combo box. The problem is that it is difficult to get the HWNDs of the child controls in a portable manner.

          One safe way to subclass the internal edit and list box controls is to subclass them in the WM_CTLCOLORXXX messages. Because Win32 sends separate WM_CTLCOLOREDIT and WM_CTLCOLORLISTBOX messages, these messages are safe and easy ways to get the HWNDs of the child controls of the combo box.

          Below is a CSuperComboBox class, which is an MFC implementation of this method. Because MFC routes all the WM_CTLCOLOR messages to OnCtlColor, the subclassing takes place there.
          Back to the top |Give Feedback

          MORE INFORMATION

          Use ClassWizard to derive a class from CComboBox and add message handlers for WM_CTLCOLOR and WM_DESTROY. Then manually edit the header file to add the data members, m_edit and m_listbox. Finally, copy the code from the message handlers below:

          Sample code

             // SuperComboBox.h : header file   class CSuperComboBox : public CComboBox   {   public:      CEdit      m_edit;      CListBox   m_listbox;   protected:      afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);      afx_msg void OnDestroy();   ...   };   // SuperComboBox.cpp : implementation file   HBRUSH CSuperComboBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)   {      if (nCtlColor == CTLCOLOR_EDIT)      {         //[ASCII 160][ASCII 160][ASCII 160]Edit control         if (m_edit.GetSafeHwnd() == NULL)            m_edit.SubclassWindow(pWnd->GetSafeHwnd());      }      else if (nCtlColor == CTLCOLOR_LISTBOX)      {         //ListBox control         if (m_listbox.GetSafeHwnd() == NULL)            m_listbox.SubclassWindow(pWnd->GetSafeHwnd());      }      HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);      return hbr;   }   void CSuperComboBox::OnDestroy()   {      if (m_edit.GetSafeHwnd() != NULL)         m_edit.UnsubclassWindow();      if (m_listbox.GetSafeHwnd() != NULL)         m_listbox.UnsubclassWindow();      CComboBox::OnDestroy();   }
          Note that for subclassing to occur, the dialog box must be painted at least once. There are cases when the dialog box doesn't paint at all (for example, closing the dialog box before it is displayed, hidden dialog boxes). This method may not be suitable when access to the subclassed windows are needed in these cases.

          (c) Microsoft Corporation 1997, All Rights Reserved. Contributions by Kelly Marie Ward, Microsoft Corporation 

          2.Eidt,不包括右边的button按钮,这个BUTTON用spy++看不到,应该是Window是用directUI自绘的。
          在重绘时,只需要在OnPaint中根据鼠标状态贴不通的图就行了,不需要考虑EDIT和Button,但注意,贴图不能全部stretchblt贴,必须是环绕EDIT控件贴,EDIT控件的背景布能贴,否则重绘时会闪烁

          3.ListBox,自绘边框时,有一点需特别注意,GetWindowRect(&rect); ScreenToClient()后,rect的left, top都是-1,FillRect()需要对四条边框都+1,否则看不到效果 
          void CUIList::OnPaint()
          {
          CPaintDC dc(this);


          CRect rect;
          // GetClientRect(&rect);
          GetWindowRect(&rect);

          // ::ScreenToClient(m_hWnd, &rect);
          ScreenToClient(&rect);

          // HDC hlistDC = dc.GetSafeHdc();
          CDC *pDC =  GetWindowDC();
          HDC hlistDC = pDC->GetSafeHdc();

          POINT point;
          point.x = point.y = 1;
          LOGPEN lgPen = {PS_SOLID, point, RGB(200, 120, 120)};
          HPEN hPen = ::CreatePenIndirect(&lgPen);

          HPEN hOldPen = (HPEN)::SelectObject(hlistDC, hPen);

          HBRUSH hOldBrush = (HBRUSH)::SelectObject(hlistDC, (HBRUSH)::GetStockObject(NULL_BRUSH));

          ::Rectangle(hlistDC, rect.left+1, rect.top+1, rect.right+1, rect.bottom+1 );
          //其实直接用GetClientRect() 这样绘制也可以的
                  //::Rectangle(hlistDC, rect.left, rect.top, rect.right+2, rect.bottom+2 );   

          ::SelectObject(hlistDC, hOldBrush);
          ::SelectObject(hlistDC, hOldPen);

          DeleteObject(hPen);
          ReleaseDC(pDC);
          }