// StaticCaption.cpp : implementation file // #include "stdafx.h" #include "Walkthrough.h" #include "StaticCaption.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CStaticCaption CStaticCaption::CStaticCaption() { // Create Colors for this control. These are used // in the OnPaint Handler. m_crBack1 = RGB(0,128,128); // Greenish m_crBack2 = RGB(128,0,128); // Purple m_crText = RGB(255,255,255); // White // Create a Font using the default GUI Font. m_pRightFont = CFont::FromHandle( (HFONT)GetStockObject(DEFAULT_GUI_FONT)); // Create a BOLD font based on the default font. LOGFONT lf; m_pRightFont->GetLogFont(&lf); lf.lfWeight = 700; // 700 = BOLD m_cLeftFont.CreateFontIndirect(&lf); } CStaticCaption::~CStaticCaption() {} BEGIN_MESSAGE_MAP(CStaticCaption, CStatic) //{{AFX_MSG_MAP(CStaticCaption) ON_WM_PAINT() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CStaticCaption message handlers void CStaticCaption::OnPaint() { CPaintDC dc(this); // device context for painting // Retrieve Window Caption for Later Use CString lcWndText; GetWindowText(lcWndText); // Get A Rect for this Control, Client Rect gives an // x and y value of zero. CRect rc; GetClientRect(&rc); // Draw the Background. DrawGradiant(dc, rc); // Draw the Caption text if the Window has any Text set. if (lcWndText != "") { DrawCapText(dc, rc, lcWndText); } } //----------{ Draw a Gradiant Background }----------------------// void CStaticCaption::DrawGradiant(CDC & pDC, CRect & pRect) { // Get RGB values of Left Side double a1 = GetRValue(m_crBack1); double a2 = GetGValue(m_crBack1); double a3 = GetBValue(m_crBack1); // Get RGB values of Right Side double b1 = GetRValue(m_crBack2); double b2 = GetGValue(m_crBack2); double b3 = GetBValue(m_crBack2); // Compute Step size for color gradiant double s1 = (b1 - a1)/pRect.Width(); double s2 = (b2 - a2)/pRect.Width(); double s3 = (b3 - a3)/pRect.Width(); for(int i = pRect.left; i < pRect.right; i++) { pDC.FillSolidRect(CRect(i,pRect.top,i+1,pRect.bottom), RGB((int)a1,(int)a2,(int)a3)); // Step to next color value a1 += s1; a2 += s2; a3 += s3; } } //----------{ Draw Label Text }---------------------------------// void CStaticCaption::DrawCapText(CDC & pDC, CRect & pRect, CString & pText) { // Prepare the DC for drawing the text. create // a transparent background, with the specified // font color. int lOldBkMode = pDC.SetBkMode(TRANSPARENT); CFont * lOldFont = pDC.SelectObject(&m_cLeftFont); COLORREF lOldColor = pDC.SetTextColor(m_crText); // Check for Description Text, if there is any // then grab it and panit it, using the Default // font. int lnTabPos = pText.Find('\t'); if (lnTabPos > 0) { pDC.SelectObject(m_pRightFont); CString lcDesc = pText.Right(pText.GetLength() - 1 - lnTabPos); UINT lOldAlign = pDC.SetTextAlign(TA_RIGHT); pDC.TextOut(pRect.right-2, pRect.top + 1, lcDesc); pDC.SetTextAlign(lOldAlign); pText = pText.Left(lnTabPos); pDC.SelectObject(&m_cLeftFont); } // Paint the Caption using the BOLD Font. pDC.TextOut(pRect.left+2, pRect.top + 1, pText); // Clean up the DC. pDC.SetTextColor(lOldColor); pDC.SelectObject(lOldFont); pDC.SetBkMode(lOldBkMode); }