用下面方法对按钮控件进行了自绘。 请问如何添加代码,设置按钮的背景色?
voidCHyperLinkButton::DrawItem(LPDRAWITEMSTRUCTlpDrawItemStruct){CDC*pDC=CDC::FromHan...
void CHyperLinkButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);// 获取DC指针
CRect rect(lpDrawItemStruct->rcItem);//定义按钮区域并初始化
pDC->SetBkMode(TRANSPARENT);//设置背景为透明
UINT nBtnState = lpDrawItemStruct->itemState;//取得按钮状态
CFont * pOldFont = NULL;
CString strText;
GetWindowText(strText); //获得按钮文本
if (m_bMouseOver)//光标在按钮上
{
if (nBtnState&ODS_SELECTED)//按下按钮,绘制反向矩形框
{
pDC->Draw3dRect(rect,GetSysColor(COLOR_3DSHADOW), GetSysColor(COLOR_3DHILIGHT));
}
else//未按下按钮,绘制正常矩形框
{
pDC->Draw3dRect(rect,GetSysColor(COLOR_3DHILIGHT),GetSysColor(COLOR_3DSHADOW));
}
pDC->SetTextColor(RGB(0,0,255)); //字体颜色设置为蓝色
if (m_fontUnderline.GetSafeHandle() == NULL)
{
CFont * pFont = GetFont();
ASSERT(pFont);
LOGFONT lf;
pFont->GetLogFont(&lf);
lf.lfUnderline = TRUE; //加下画线
m_fontUnderline.CreateFontIndirect(&lf);
}
pOldFont = pDC->SelectObject(&m_fontUnderline);
}
else pDC->SetTextColor(GetSysColor(COLOR_BTNTEXT));//采用系统字体
pDC->DrawText(strText, rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);//显示按钮文本
if (pOldFont)
pDC->SelectObject(pOldFont);
} 展开
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);// 获取DC指针
CRect rect(lpDrawItemStruct->rcItem);//定义按钮区域并初始化
pDC->SetBkMode(TRANSPARENT);//设置背景为透明
UINT nBtnState = lpDrawItemStruct->itemState;//取得按钮状态
CFont * pOldFont = NULL;
CString strText;
GetWindowText(strText); //获得按钮文本
if (m_bMouseOver)//光标在按钮上
{
if (nBtnState&ODS_SELECTED)//按下按钮,绘制反向矩形框
{
pDC->Draw3dRect(rect,GetSysColor(COLOR_3DSHADOW), GetSysColor(COLOR_3DHILIGHT));
}
else//未按下按钮,绘制正常矩形框
{
pDC->Draw3dRect(rect,GetSysColor(COLOR_3DHILIGHT),GetSysColor(COLOR_3DSHADOW));
}
pDC->SetTextColor(RGB(0,0,255)); //字体颜色设置为蓝色
if (m_fontUnderline.GetSafeHandle() == NULL)
{
CFont * pFont = GetFont();
ASSERT(pFont);
LOGFONT lf;
pFont->GetLogFont(&lf);
lf.lfUnderline = TRUE; //加下画线
m_fontUnderline.CreateFontIndirect(&lf);
}
pOldFont = pDC->SelectObject(&m_fontUnderline);
}
else pDC->SetTextColor(GetSysColor(COLOR_BTNTEXT));//采用系统字体
pDC->DrawText(strText, rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);//显示按钮文本
if (pOldFont)
pDC->SelectObject(pOldFont);
} 展开
1个回答
2011-09-16
展开全部
if (nBtnState&ODS_SELECTED)//按下按钮,绘制反向矩形框
{
pDC->Draw3dRect(rect,GetSysColor(COLOR_3DSHADOW), GetSysColor(COLOR_3DHILIGHT));
}
else//未按下按钮,绘制正常矩形框
{
pDC->Draw3dRect(rect,GetSysColor(COLOR_3DHILIGHT),GetSysColor(COLOR_3DSHADOW));
}
Draw3dRect改变第二个参数就好了,第二个参数指定颜色
{
pDC->Draw3dRect(rect,GetSysColor(COLOR_3DSHADOW), GetSysColor(COLOR_3DHILIGHT));
}
else//未按下按钮,绘制正常矩形框
{
pDC->Draw3dRect(rect,GetSysColor(COLOR_3DHILIGHT),GetSysColor(COLOR_3DSHADOW));
}
Draw3dRect改变第二个参数就好了,第二个参数指定颜色
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询