VC如何改变编辑框的背景颜色方法
来源:互联网 发布:java自行车是高端的吗 编辑:程序博客网 时间:2024/05/07 16:33
这里介绍的改变文本编辑框的背景颜色的方法不需要对CEdit生成新的类,步骤如下:
(1) 新建一个基于对话框的MFC应用程序,程序名称为Test;
(2) 在对话框上添加两个文本框,ID分别为IDC_EDIT1和IDC_EDIT2;
(3) 在CTestDlg的头文件中添加几个成员变量,如下所示;
class CTestDlg : public CDialog
{
protected:
CBrush m_redbrush,m_bluebrush;
COLORREF m_redcolor,m_bluecolor,m_textcolor;
};
(4) 在CTestDlg.cpp文件的BOOL CTestDlg::OnInitDialog()中添加以下代码:
BOOL CTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
m_redcolor=RGB(255,0,0); // 红色
m_bluecolor=RGB(0,0,255); // 蓝色
m_textcolor=RGB(255,255,255); // 文本颜色设置为白色
m_redbrush.CreateSolidBrush(m_redcolor); // 红色背景色
m_bluebrush.CreateSolidBrush(m_bluecolor); // 蓝色背景色
return TRUE; // return TRUE unless you set the focus to a control
}
(5) 右击对话框空白面,选择Event,为WM_CTLCOLOR添加消息响应函数,编辑代码如下:
HBRUSH CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
switch (nCtlColor) //对所有同一类型的控件进行判断
{
// process my edit controls by ID.
case CTLCOLOR_EDIT:
case CTLCOLOR_MSGBOX://假设控件是文本框或者消息框,则进入下一个switch
switch (pWnd->GetDlgCtrlID())//对某一个特定控件进行判断
{
// first CEdit control ID
case IDC_EDIT1: // 第一个文本框
// here
pDC->SetBkColor(m_bluecolor); // change the background
// color [background colour
// of the text ONLY]
pDC->SetTextColor(m_textcolor); // change the text color
hbr = (HBRUSH) m_bluebrush; // apply the blue brush
// [this fills the control
// rectangle]
break;
// second CEdit control ID
case IDC_EDIT2: // 第二个文本框
// but control is still
// filled with the brush
// color!
pDC->SetBkMode(TRANSPARENT); // make background
// transparent [only affects
// the TEXT itself]
pDC->SetTextColor(m_textcolor); // change the text color
hbr = (HBRUSH) m_redbrush; // apply the red brush
// [this fills the control
// rectangle]
break;
default:
hbr=CDialog::OnCtlColor(pDC,pWnd,nCtlColor);
break;
}
break;
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
注:case的类别有以下几种:
CTLCOLOR_BTN 按钮控件
CTLCOLOR_DLG 对话框
CTLCOLOR_EDIT 编辑框
CTLCOLOR_LISTBOX 列表框
CTLCOLOR_MSGBOX 消息框
CTLCOLOR_SCROLLBAR 滚动条
CTLCOLOR_STATIC 静态文本
以上方法,对只读的编辑框无效!
在一位大侠的帮助下,终于找到了改变只读编辑框背景的方法:
参考帮助文档“WM_CTLCOLOREDIT”条目下的内容。 是说ReadOnly和disabled不发送CTLCOLOR_EDIT。 但它发送‘CTLCOLOR_STATIC’。
if((nCtlColor == CTLCOLOR_EDIT) || (nCtlColor == CTLCOLOR_STATIC)) && (*pWnd == m_ReadyOnlyEdit) { 。。。。 }
但是本方法不适用于多行,继续探索,呵呵~
- VC如何改变编辑框的背景颜色方法
- VC如何改变编辑框的背景颜色方法
- VC如何改变编辑框的背景颜色方法
- 用VC如何改变编辑框的背景颜色
- VC 编辑框 改变背景、字体、文本颜色、长度限制
- VC 编辑框 改变背景、字体、文本颜色、长度限制
- MFC编辑框改变背景、颜色
- MFC编辑框改变背景、颜色等
- MFC编辑框改变背景、颜色等
- MFC编辑框改变背景、颜色
- MFC编辑框改变背景、颜色等
- 改变VC对话框的背景颜色
- VC改变控件的背景颜色
- VC++改变对话框的背景颜色
- VC 编辑框 改变背景、字体、文本颜色、长度限制、英文 汉字判断
- VC 编辑框 改变背景、字体、文本颜色、长度限制、英文 汉字判断
- VC 编辑框 改变背景、字体、文本颜色、长度限制、英文 汉字判断收藏
- VC 编辑框 改变背景、字体、文本颜色、长度限制、英文 汉字判断
- css3文字霓虹灯光效果
- 在WebView加载页面时添加cookies
- [Unity实战]框选效果
- Longest Common Prefix
- 【JavaScript】复选框的全选、反选,判断哪些复选框被选中
- VC如何改变编辑框的背景颜色方法
- 程序员的量化交易之路(29)--Cointrader之Tick实体(16)
- linux-进程监控
- 黑马程序员_面向对象(1)
- C中的volatile用法
- 主流浏览器CSS3和HTML5兼容性详细清单
- 七月建博展你来不来,不来你亏了!
- Linux 下鼠标对应的文件
- 黑马程序员--关于一些错误的解决办法