怎么让Edit控件不能输入同时不改变它的背景色
如何让Edit控件不能输入同时不改变它的背景色?
请问:
如何让Edit控件不能输入同时不改变它的背景色?
我已经在网上搜索到一些资料,响应WM_CTLCOLOR,并且写代码:
HBRUSH CInputEdit::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CEdit::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: 在此更改 DC 的任何属性
if (pWnd-> GetDlgCtrlID()==IDC_Edit)
{
pDC-> SetBkMode(TRANSPARENT);
pDC-> SetBkColor(RGB(255,255,255));
static CBrush m_brushEdit(RGB(255,255,255));
hbr = m_brushEdit;
}
// TODO: 如果默认的不是所需画笔,则返回另一个画笔
return hbr;
}
但是,只要Edit控件的属性设为ReadOnly,这些设置无效,总是灰色。
另外,我尝试过响应=WM_CTLCOLOR消息,写函数:
HBRUSH CInputEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
// TODO: 在此更改 DC 的任何属性
HBRUSH hbr;
CBrush Brush;
if(nCtlColor==CTLCOLOR_EDIT)
{
Brush.CreateSolidBrush(RGB(255,255,255));
pDC-> SetTextColor(RGB(0,0,0));
pDC-> SetBkColor(RGB(255,255,255));
hbr=(HBRUSH)(Brush.GetSafeHandle());
return hbr;
}
// TODO: 如果不应调用父级的处理程序,则返回非空画笔
return NULL;
}
同样都不行。
请问,应该怎么做?
我是不是缺少了一些步骤?
------解决方案--------------------
在构造函数里:
m_clrBkgnd = 某些颜色;
m_brBkgnd.CreateSolidBrush( m_clrBkgnd );
m_clrText = 某些颜色;
m_clrBkgnd = 某些颜色;
m_clrDisableText = 某些颜色;
m_clrDisableBkgnd = 某些颜色;
HBRUSH CustEdit::CtlColor( CDC* pDC, UINT /*nCtlColor*/ )
{
if ( this-> IsWindowEnabled() )
{
// Text
pDC-> SetTextColor( m_clrText );
// Text bkgnd
pDC-> SetBkColor( m_clrBkgnd );
}
else
{
// Text
pDC-> SetTextColor( m_clrDisableText );
// Text bkgnd
pDC-> SetBkColor( m_clrDisableBkgnd );
}
return m_brBkgnd; // Control bkgnd
}
------解决方案--------------------
set edit control 'read-ony ' cause it to be a static text, cannot do it.
1)subclasswindow
2)process WM_CHAR in dialog wndproc-> override PreTranslateMessage()
------解决方案--------------------
-> override PreTranslateMessage()
// make all edit box input read-only
BOOL CYourDlg::PreTranslateMessage(MSG* pMsg)
{
switch(pMsg-> message)
{
// donot translate WM_CHAR
case WM_CHAR:
return TRUE;
}
return CDialog::PreTranslateMessage(pMsg);
}
请问:
如何让Edit控件不能输入同时不改变它的背景色?
我已经在网上搜索到一些资料,响应WM_CTLCOLOR,并且写代码:
HBRUSH CInputEdit::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CEdit::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: 在此更改 DC 的任何属性
if (pWnd-> GetDlgCtrlID()==IDC_Edit)
{
pDC-> SetBkMode(TRANSPARENT);
pDC-> SetBkColor(RGB(255,255,255));
static CBrush m_brushEdit(RGB(255,255,255));
hbr = m_brushEdit;
}
// TODO: 如果默认的不是所需画笔,则返回另一个画笔
return hbr;
}
但是,只要Edit控件的属性设为ReadOnly,这些设置无效,总是灰色。
另外,我尝试过响应=WM_CTLCOLOR消息,写函数:
HBRUSH CInputEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
// TODO: 在此更改 DC 的任何属性
HBRUSH hbr;
CBrush Brush;
if(nCtlColor==CTLCOLOR_EDIT)
{
Brush.CreateSolidBrush(RGB(255,255,255));
pDC-> SetTextColor(RGB(0,0,0));
pDC-> SetBkColor(RGB(255,255,255));
hbr=(HBRUSH)(Brush.GetSafeHandle());
return hbr;
}
// TODO: 如果不应调用父级的处理程序,则返回非空画笔
return NULL;
}
同样都不行。
请问,应该怎么做?
我是不是缺少了一些步骤?
------解决方案--------------------
在构造函数里:
m_clrBkgnd = 某些颜色;
m_brBkgnd.CreateSolidBrush( m_clrBkgnd );
m_clrText = 某些颜色;
m_clrBkgnd = 某些颜色;
m_clrDisableText = 某些颜色;
m_clrDisableBkgnd = 某些颜色;
HBRUSH CustEdit::CtlColor( CDC* pDC, UINT /*nCtlColor*/ )
{
if ( this-> IsWindowEnabled() )
{
// Text
pDC-> SetTextColor( m_clrText );
// Text bkgnd
pDC-> SetBkColor( m_clrBkgnd );
}
else
{
// Text
pDC-> SetTextColor( m_clrDisableText );
// Text bkgnd
pDC-> SetBkColor( m_clrDisableBkgnd );
}
return m_brBkgnd; // Control bkgnd
}
------解决方案--------------------
set edit control 'read-ony ' cause it to be a static text, cannot do it.
1)subclasswindow
2)process WM_CHAR in dialog wndproc-> override PreTranslateMessage()
------解决方案--------------------
-> override PreTranslateMessage()
// make all edit box input read-only
BOOL CYourDlg::PreTranslateMessage(MSG* pMsg)
{
switch(pMsg-> message)
{
// donot translate WM_CHAR
case WM_CHAR:
return TRUE;
}
return CDialog::PreTranslateMessage(pMsg);
}