`
wo_deqing
  • 浏览: 60272 次
文章分类
社区版块
存档分类
最新评论

MFC中设置static text控件的背景色、字体、字号和颜色

 
阅读更多

在实际的应用中,可以用WM_CTLCOLOR 消息改变mfc中控件的颜色,比如现在就来改变一个static text控件的

字体、字体大小、字体颜色和背景色。

例如对话框的类为CTestDlg.

1. 在对话框的类中添加两个变量.

方法:在classview选项卡中,选择CTestDlg,右键,add member variable.

CBrush m_brush;
CFont m_font;

<wbr></wbr>

在OnInitDialog()函数中添加:

// TODO: ……

m_font.CreatePointFont(150,"华文行楷");//代表15号字体,华文行楷
m_brush.CreateSolidBrush(RGB(0,255,0));//画刷为绿色

<wbr></wbr>

2 添加WM_CTLCOLOR 消息响应,添加的方法为:

add windows message handler->WM_CTLCOLOR->add and edit

<wbr></wbr>

<wbr></wbr>

3 在HBRUSH CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 函数中的todo后添加代码,即:

HBRUSH CYourDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

<wbr><span style="font-size:18px">// TODO: Change any attributes of the DC here<br></span><wbr><span style="font-size:18px">if (pWnd-&gt;GetDlgCtrlID() == IDC_STATICText)<br></span><wbr><span style="font-size:18px">{</span></wbr></wbr></wbr>

<wbr><span style="font-size:18px">pDC-&gt;SetBkColor(RGB(0,255,0));//背景色为绿色<br></span><wbr><wbr><span style="font-size:18px">pDC-&gt;SetTextColor(RGB(255, 0, 0));//文字为红色<br></span><wbr><wbr><span style="font-size:18px">pDC-&gt;SelectObject(&amp;m_font);//文字为15号字体,华文行楷<br></span><wbr><span style="font-size:18px">return m_brush;<br></span><wbr><span style="font-size:18px">}</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><span style="font-size:18px">// TODO: Return a different brush if the default is not desired<br></span><wbr><span style="font-size:18px">return hbr;</span></wbr></wbr>

}

<wbr><wbr><p><wbr></wbr></p> <p><span style="font-size:18px">这样就可以改变static text的背景色、字体、字体大小和字体颜色了。</span></p> </wbr></wbr>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics