Visual C++ 开发实战宝典
功能:位图上显示文字,背景透明。
void CP400Dlg::OnPaint()
{
。。。。
CBitmap bmp;
bmp.LoadBitmap(IDB_YCH);
BITMAP bmInfo;
bmp.GetBitmap(&bmInfo);
int nBmpWidth,nBmpHeight;
nBmpWidth = bmInfo.bmWidth;
nBmpHeight = bmInfo.bmHeight;
CRect clientRC;
GetClientRect(clientRC);
CDC *pDC = GetDC();
CDC memDC;
memDC.CreateCompatibleDC(pDC);
memDC.SelectObject(&bmp);
pDC->StretchBlt(0,0,clientRC.Width(),clientRC.Height(),&memDC,0,0,nBmpWidth,nBmpHeight,SRCCOPY);
memDC.DeleteDC();
bmp.DeleteObject();
pDC->SetBkMode(TRANSPARENT);
CFont font;
font.CreatePointFont(350,"华文行楷",pDC);
pDC->SelectObject(&font);
pDC->SetTextColor(RGB(0,0,255));
pDC->SetBkColor(RGB(255,0,0));
pDC->TextOut(10,20,"江山如此多娇。");
pDC->TextOut(10,90,"引无数英雄竞折腰。");
}
功能:静态文本框去除背景,透明。
HBRUSH CP400Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
if (nCtlColor = CTLCOLOR_STATIC)
{
if (pWnd == GetDlgItem(IDC_STATIC1))
{
pDC->SetBkColor(RGB(125,255,0));
//文字背景
pDC->SetTextColor(RGB(255,0,0)); //文字颜色
pDC->SetBkMode(TRANSPARENT); //不显示文字背景
hbr = (HBRUSH)::GetStockObject(NULL_BRUSH); //编辑框背景。注意:和文字背景不是一个意思。
}
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
效果:

本文介绍了如何在Visual C++中实现位图背景上的文字透明显示,以及静态文本框的背景透明。通过OnPaint()函数进行位图绘制,并设置DC的背景模式为TRANSPARENT,实现背景透明。同时,使用OnCtlColor()函数处理静态文本框的控件颜色,设置透明背景和自定义文字颜色。

3635

被折叠的 条评论
为什么被折叠?



