vb 改变Label颜色

vb 改变Label颜色,第1张

Private Sub Label1_Click(Index As Integer)
If Label1(Index)BackColor = vbRed Then
Label1(Index)BackColor = Val(Label1(Index)Tag)
Else
Label1(Index)Tag = Label1(Index)BackColor
Label1(Index)BackColor = vbRed
End If
End Sub

实际的应用中,可以用WM_CTLCOLOR 消息改变mfc中控件的颜色,比如现在就来改变一个static text控件的
字体字体大小、字体颜色和背景色。
例如对话框的类为CTestDlg
1 在对话框的类中添加两个变量
方法:在classview选项卡中,选择CTestDlg,右键,add member variable
CBrush m_brush;
CFont m_font;
在OnInitDialog()函数中添加:
// TODO: ……
m_fontCreatePointFont(150,"华文行楷");//代表15号字体,华文行楷
m_brushCreateSolidBrush(RGB(0,255,0));//画刷为绿色
2 添加WM_CTLCOLOR 消息响应,添加的方法为:
add windows message handler->WM_CTLCOLOR->add and edit
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);
// TODO: Change any attributes of the DC here
if (pWnd->GetDlgCtrlID() == IDC_STATICText)
{
pDC->SetBkColor(RGB(0,255,0));//背景色为绿色
pDC->SetTextColor(RGB(255, 0, 0));//文字为红色
pDC->SelectObject(&m_font);//文字为15号字体,华文行楷
return m_brush;
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
这样就可以改变static text的背景色、字体、字体大小和字体颜色了。

在button1的事件里加入:
label1forecolor=%color%
其中%color%是颜色值,一般的颜色只需要vb+英文颜色名就可以了,如红色就是vbRed。或者敲完forecolor按=时它会d出个列表表示可用颜色。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/13400788.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-29
下一篇 2023-07-29

发表评论

登录后才能评论

评论列表(0条)

保存