VC++ 按钮颜色初始化

来源:百度知道 编辑:UC知道 时间:2024/06/04 10:29:06
我用VC做1个界面,需要使按钮的颜色根据数据库里的数据进行判断分别显示为黄色和绿色我是用
void CHLPView::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if(nIDCtl==IDC_BUTTON1) //checking for the button
{
CDC dc;
RECT rect;

dc.Attach(lpDrawItemStruct ->hDC); // Get the Button DC to CDC

rect = lpDrawItemStruct->rcItem; //Store the Button rect to our local rect.

dc.Draw3dRect(&rect,RGB(255,255,255),RGB(0,0,0));
CString str;
if(GetDlgItem(IDC_EDIT_Level)->GetWindowText(str),str=="高")
{
dc.FillSolidRect(&rect,RGB(255,255,0));//黄
}
else
{
dc.FillSolidRect(&rect,RGB(0,128,128));//绿Here you can define the required color to appear on the Button.
}

UINT state=lpDrawItemStruct->itemState; //This defines the state of the Push button either pressed or not.

if((state & ODS_SELECTED))
{

m_strText2 = m_pSet->m_Level;
AfxGetApp()->WriteProfileString("CPatient_DataView","健康等级2",m_strText2);
Invalidate();//刷新界面

修改背景色请响应这个消息WM_ERASEBKGND.

对话框, 或者其他的控件, 只要是修改背景的都响应这个函数.