⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 index045.htm

📁 一本不错的VC编程的参考书
💻 HTM
字号:
<html>
<style type="text/css"><!--
.p9 {  font-family: "宋体"; font-size: 9pt}a        {text-transform: none; text-decoration: none;}
a:hover {text-decoration: underline; color: #FF0000;}
--></style>
<body background="../di2001.jpg">
<h3 align="center"><font COLOR="#AOAO99"></font></h3>
<table width="100%" border="1" cellspacing="1">
<tr><td><p align="center"><font color="#FF0000">如何改变控件的颜色</font></td></tr>
<tr><td><p>
</Br>
有两种方法。其一,可以在父类中指定控件的颜色,或者利用MFC4.0新的消息反射在控件类中指定颜色。 当控件需要重新着色时,工作框调用父窗口(通常是对话框)的CWnd: : OnCrtlColor,可以在父窗口类中重置该函数并指定控件的新的绘画属性。例如,下述代码将对话中的所有编辑控件文本颜色改为红色:<Br>
HBRUSH CAboutDig : : OnCtlColor (CDC * pDCM , CWnd * pWnd , UINT nCtlColor)<Br>
</Br>
{<Br>
&nbsp;HBRUSH hbr = CDialog : : OnCtlColor (pDC, pWnd , nCtlColor )<Br>
</Br>
&nbsp;//Draw red text for all edit controls .<Br>
&nbsp;if (nCtlColor= = CTLCOLOR_EDIT )<Br>
&nbsp;pDC —> SetTextColor (RGB (255, 0 , 0 , ) )<Br>
</Br>
&nbsp;return hbr<Br>
}<Br>
</Br>
然而,由于每个父窗口必须处理通知消息并指定每个控件的绘画属性,所以,这种方法不是完全的面向对象的方法。控件处理该消息并指定绘画属性更合情合理。消息反射允许用户这样做。通知消息首先发送给父窗口,如果父窗口没有处理则发送给控件。创建一个定制彩色列表框控件必须遵循下述步骤。<Br>
</Br>
首先,使用ClassWizard 创建一个CListBox 的派生类并为该类添加下述数据成员。<Br>
class CMyListBox  publilc CListBox<Br>
{<Br>
…<Br>
private<Br>
&nbsp;COLORREF m_clrFor  // foreground color<Br>
&nbsp;COLORREF m_clrBack  //background color<Br>
&nbsp;Cbrush m_brush  //background brush<Br>
…<Br>
}<Br>
其次,在类的构造函数中,初始化数据中。<Br>
CMyListBox : : CMyListBox ()<Br>
{<Br>
&nbsp;//Initialize data members .<Br>
&nbsp;m_clrFore =RGB (255 , 255 , 0)  //yellow text<Br>
&nbsp;m_clrBack=RGB (0 , 0 , 255)  // blue background<Br>
&nbsp;m_brush . CreateSolidBrush (m _clrBack )<Br>
}<Br>
</Br>
最后,使用ClassWizard处理反射的WM_CTLCOLOR(=WM_CTLCOLOR)消息并指定新的绘画属性。<Br>
HBRUSH CMyListBox : : CtlColor (CDC* pDC, UINT nCtlColor )<Br>
{<Br>
&nbsp;pDC—>SetTextColor (m_clrFore)<Br>
&nbsp;pDC—>SetBkColor (m_clrBack)<Br>
</Br>
&nbsp;return (HBRUSH) m_brush.GetSafeHandle ()<Br>
}<Br>
现在,控件可以自己决定如何绘画,与父窗口无关。<Br>
</Br>
</p></td></tr>
</table>
</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -