设置界面的标题栏的渐变颜色,在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();
}