ComboBox重绘

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

On This Page

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.

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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值