设置渐变颜色标题栏

设置界面的标题栏的渐变颜色,在OnPaint中调用如下函数即可

 

DrawCaption(CDC *pDC, const CString strCaption, const COLORREF clrCaption/* =RGB */)
{
	CDC memdc;
	memdc.CreateCompatibleDC(pDC);
	CRect rectCaption, rectClient;
	CBitmap bmp;

	GetClientRect(rectClient);
	//标题栏的大小与位置
	rectCaption.left = rectClient.left;
	rectCaption.top = rectClient.top;
	rectCaption.right = rectClient.right;
	rectCaption.bottom = rectCaption.top + 20;		//标题栏背景的高度	

	bmp.CreateCompatibleBitmap(pDC, rectCaption.Width(), rectCaption.Height());
	memdc.SelectObject(bmp);

	//起始颜色
	int clrBBase = clrCaption>>16 & 0x000000FF;
	int clrGBase = clrCaption>>8 & 0x000000FF;
	int clrRBase = clrCaption & 0x000000FF;	
	//过渡颜色
	int clrRCurr = clrRBase;
	int clrGCurr = clrGBase;
	int clrBCurr = clrBBase;
	//色彩增量
	const double nRClrInc = (double)(255-clrRBase)/(double)rectCaption.Width();	
	const double nGClrInc = (double)(255-clrGBase)/(double)rectCaption.Width();
	const double nBClrInc = (double)(255-clrBBase)/(double)rectCaption.Width();

	//开始画标题栏
	CRect rectDraw = rectCaption;
	for(int nLeft=rectCaption.left, nRight=rectCaption.left+1; nLeft<rectCaption.right; nLeft++, nRight++)
	{
		rectDraw.left = nLeft;
		rectDraw.right = nRight;
		memdc.FillSolidRect(rectDraw, RGB(clrRCurr, clrGCurr, clrBCurr));
		clrRCurr = (int)((nLeft-rectCaption.left)*nRClrInc+clrRBase);
		clrGCurr = (int)((nLeft-rectCaption.left)*nGClrInc+clrGBase);
		clrBCurr = (int)((nLeft-rectCaption.left)*nBClrInc+clrBBase);
	}
	memdc.SetBkMode(TRANSPARENT);
	//设置字体
	CFont font;
	font.CreatePointFont(110,_T("宋体"),pDC);
	memdc.SelectObject(&font);
	//得到字体的高度
	TEXTMETRIC tm = {0};
	memdc.GetTextMetrics(&tm);
	int nHeight = tm.tmHeight+tm.tmExternalLeading;
	//显示标题
	memdc.SetTextColor(RGB(0,0,0));
	memdc.TextOut(rectCaption.left + 26, (rectCaption.Height()-nHeight)/2+1,strCaption, (int)_tcslen(strCaption));
	memdc.SetTextColor(RGB(255,255,255));
	memdc.TextOut(rectCaption.left + 25, (rectCaption.Height()-nHeight)/2, strCaption, (int)_tcslen(strCaption));

	pDC->BitBlt(rectCaption.left, rectCaption.top, rectClient.Width(), rectClient.Height(), &memdc, 0, 0, SRCCOPY);
	memdc.DeleteDC();
	bmp.DeleteObject();
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值