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
On This Page
- SUMMARY
- MORE INFORMATION
- Sample code
- 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.
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: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
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(); }
(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() 这样绘制也可以的
{
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);
}
- 重绘ComboBox[C#]
- ComboBox ListBox 重绘
- 重绘ComboBox[C#] .
- ComboBox重绘
- 重绘ComboBox[C#]
- combobox重绘代码笔记
- C# - ComboBox 重绘 WinForm开发系列
- easyUI重绘combobox中下拉箭头
- Extjs combobox 去重
- c#的控件重绘 包括Button、RadioButton、ComboBox、TextBox、GroupBox
- 【自绘ComboBox】带图标的ComboBox
- combobox自绘问题
- MFC 自绘ComboBox
- ComboBox
- combobox
- ComboBox
- ComboBox
- ComboBox
- git 思维导图
- python 从字典里取出内容,创建集合 dict -> set
- 最新内核(linux2.6.32)+文件系统(nfs)的编译
- Ubuntu(Linux)使用Eclipse搭建C/C++编译环境
- 学会正确的恋爱
- ComboBox重绘
- ADC0832程序完整版 源码+Proteus仿真
- 江西爱车网——地方汽车门户网站程序采用ASP+ACCESS开发
- jatoolsPrinter套打整理
- PHPCMS、帝国CMS及织梦对比
- (转)java 存储过程或函数开发(总结)
- drupal7 view添加查询条件又一方法效率不知道怎么样,但至少功能能实现!
- 如何自定义visifire图表主题和样式?
- 使用java连接Mysql数据库 2