如何更改一个 MFC 编辑控件的背景色
2013-07-13 11:55
381 查看
概要
若要更改的 MFC 应用程序中编辑控件的背景色,您必须重写包含编辑控件的窗口的 OnCtlColor() 消息处理函数。在新的 OnCtlColor() 函数中,设置的背景颜色,并返回到将用于绘制背景的画笔的句柄。这必须在 OnCtlColor() 函数中接收的 CTLCOLOR_EDIT 和 CTLCOLOR_MSGBOX 消息的响应。
它也记录在"类库引用"下 CWnd::OnCtlColor()。
回到顶端 | 提供反馈
更多信息
下面的代码示例使用 CDialog 派生的类来演示该过程。类向导用于生成的 WM_CTLCOLOR 和 WM_DESTROY 消息的消息处理函数。这些函数分别称为 CEditDialog::OnCtlColor() 和 CEditDialog::OnDestroy()。请注意Visual C++.NET 中您可以从属性窗口中添加对话框对象的 WM_CTLCOLOR 和 WM_DESTROY 处理程序。对话框中可用的所有邮件的邮件选项卡中都列出。
示例代码
// editdlg.h : header file // //////////////////////////////////////////////////////////////////////// /// // CEditDialog dialog class CEditDialog : public CDialog { // Construction public: CEditDialog(CWnd* pParent = NULL); // standard constructor // Add a CBrush* to store the new background brush for edit controls. CBrush* m_pEditBkBrush; // Dialog Data //{{AFX_DATA(CEditDialog) enum { IDD = IDD_EDITDIALOG }; // NOTE: The ClassWizard will add data members here. //}}AFX_DATA // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CEditDialog) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: // Generated message map functions //{{AFX_MSG(CEditDialog) afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor); afx_msg void OnDestroy(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; // editdlg.cpp : implementation file // #include "stdafx.h" #include "mdi.h" #include "editdlg.h" #ifdef _DEBUG #undef THIS_FILE static char BASED_CODE THIS_FILE[] = __FILE__; #endif ////////////////////////////////////////////////////////////////////// // CEditDialog dialog CEditDialog::CEditDialog(CWnd* pParent /*=NULL*/) : CDialog(CEditDialog::IDD, pParent) { //{{AFX_DATA_INIT(CEditDialog) // NOTE: The ClassWizard will add member initialization here. //}}AFX_DATA_INIT // Instantiate and initialize the background brush to black. m_pEditBkBrush = new CBrush(RGB(0, 0, 0)); } void CEditDialog::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CEditDialog) // NOTE: The ClassWizard will add DDX and DDV calls here. //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CEditDialog, CDialog) //{{AFX_MSG_MAP(CEditDialog) ON_WM_CTLCOLOR() ON_WM_DESTROY() //}}AFX_MSG_MAP END_MESSAGE_MAP() ////////////////////////////////////////////////////////////////////// // CEditDialog message handlers HBRUSH CEditDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { switch (nCtlColor) { case CTLCOLOR_EDIT: case CTLCOLOR_MSGBOX: // Set color to green on black and return the background brush. pDC->SetTextColor(RGB(0, 255, 0)); pDC->SetBkColor(RGB(0, 0, 0)); return (HBRUSH)(m_pEditBkBrush->GetSafeHandle()); default: return CDialog::OnCtlColor(pDC, pWnd, nCtlColor); } } void CEditDialog::OnDestroy() { CDialog::OnDestroy(); // Free the space allocated for the background brush delete m_pEditBkBrush; }
转自:http://support.microsoft.com/kb/117778
相关文章推荐
- MFC中如何在一个类中调用另一个类的控件
- 如何将.NET中定义的一个Window Form控件作为一个MFC对话框调用?(From Msdn)
- Flex中如何给DataGrid控件的项目添加一个可编辑TextArea/TextInput,单击控件内的项目时,自动进入编辑状态
- MFC 静态文本框使用/更改button背景色/不使能button/设置标题/不显示控件/窗口最大最小化
- MFC中如何让一个CStatic控件响应消息
- [VC/MFC]Combo控件背景色更改(非自绘)
- VC++ 中如何更改Tree Control控件中节点的背景色和节点文字的颜色
- 如何更改MFC按钮的背景色 推荐
- 如何更改MFC按钮的背景色
- MFC中如何让一个CStatic控件响应消息
- MFC静态文本控件如何设置字体--大小和颜色及背景色
- Flex中如何给DataGrid控件的项目添加一个可编辑TextArea/TextInput,单击控件内的项目时,自动进入编辑状态
- 知道一个div的宽高背景色,如何不通过写在行间样式的办法更改样式?currentStyle(只兼容ie),getComeputedStyle(兼容)-->获取非行间样式
- 如何更改MFC按钮的背景色
- Flex中如何将NumericStepper控件作为DataGrid控件的一个编辑项目的例子
- 如何更改MFC按钮的背景色
- 颜色控件【MFC】自己写一个ListCtrl控件(实现栅格属性表的编辑)-java教程
- Occt的visualization模块:如何使用MFC和Occt显示一个Bspline曲线
- 当打开一个文档时,MFC如何选择所使用的文档模板对象?
- VC++ 中改变对话框及相关控件的背景色(MFC)