Click here to Skip to main content
15,889,839 members
Articles / Desktop Programming / MFC
Article

Banner Control

Rate me:
Please Sign up or sign in to vote.
3.67/5 (5 votes)
7 Mar 2004 38.8K   1.4K   15   2
About banner-like control in applications

Image 1

Introduction

In my opinion the worst things in programming are really useful interface making. Therefore the using of existing solutions is preferable. But sometimes we need to realize our own one because found solutions can't achieve desirable results. I hope that this application allows people to simplify his or her life and work.

Background

In my work I have needed to use banner control. In Code Project I quickly found solutions of PaulWendt and Peter Mares and decided to combine their solutions.

Using the code

This application demonstrates banner control with docked possibility and font orientation according to docked position. By form controls we may to change scrolling speed and manipulate banner database with text attributes changing and banner positioning.

I have made orientation addition in class CMultiColorStatic

int CMultiColorStatic::GetOrientation()
{ return m_iOrientation;}

void CMultiColorStatic::SetOrientation(int iOrientation)
{ m_iOrientation = iOrientation; Invalidate(); }

Also I have added consequent insertions in Paint function in

CBannerStatic
class.

void CBannerStatic::OnPaint()
{
   CPaintDC dc(this);
      
   CRect rcBounds = m_rcBounds;      
   if(GetOrientation()==ORIENTATION_HORIZONTAL) 
     rcBounds.left = m_nTextOut;
   if(GetOrientation()==ORIENTATION_VERTICAL_LEFT) 
     rcBounds.bottom = m_rcBounds.Height()-m_nTextOut;
   if(GetOrientation()==ORIENTATION_VERTICAL_RIGHT) 
     rcBounds.top = m_nTextOut;
   dc.FillRect(m_rcBounds, &m_brBackGround);
   dc.IntersectClipRect(m_rcBounds); 

   m_nTextLength = 0;

   for (int i = 0; i < m_astrData.GetSize(); i++)
   {
      CColorString* pstrCurrent = 
        reinterpret_cast<CColorString*>(m_astrData.GetAt(i));
      TEXTMETRIC    stFontMetrics;
      SIZE          stSize;

      DetermineFont(pstrCurrent);

      dc.SelectObject(&m_ftText)->DeleteObject();
    if (pstrCurrent->GetBackColor() == ::GetSysColor(COLOR_BTNFACE))
      {
         dc.SetBkColor(m_crBackColor);
      }
      else
      {
         dc.SetBkColor(pstrCurrent->GetBackColor());
      }
    dc.SetTextColor(pstrCurrent->GetColor());
    dc.GetOutputTextMetrics(&stFontMetrics);

      GetTextExtentPoint32(dc.GetSafeHdc(), 
          *pstrCurrent, pstrCurrent->GetLength(), &stSize);

      if(GetOrientation()==ORIENTATION_VERTICAL_LEFT) 
      {
        dc.MoveTo(rcBounds.left, rcBounds.Height());
        dc.SetTextAlign( TA_UPDATECP);
      }
    if(GetOrientation()==ORIENTATION_VERTICAL_RIGHT)
    {
       dc.MoveTo(rcBounds.Width(),rcBounds.top);
         dc.SetTextAlign( TA_UPDATECP);
       }
       dc.DrawText(*pstrCurrent, rcBounds, DT_LEFT);
    
       if(GetOrientation()==ORIENTATION_HORIZONTAL) 
          rcBounds.left += stSize.cx + stFontMetrics.tmOverhang;
       if(GetOrientation()==ORIENTATION_VERTICAL_LEFT) 
          rcBounds.bottom -= stSize.cx + stFontMetrics.tmOverhang;
       if(GetOrientation()==ORIENTATION_VERTICAL_RIGHT) 
          rcBounds.top += stSize.cx + stFontMetrics.tmOverhang;

       m_nTextLength += stSize.cx + stFontMetrics.tmOverhang;
  }
}

Points of Interest

I don't understand why system font has not changed according to given orientation. I had no time to clear this situation and I had used other fonts.

void CMultiColorStatic::DetermineFont(const CColorString* const pstrData)
{
   LOGFONT stFont;

   m_ftText.DeleteObject();

   //---------------------------------------------------
   // set up the font based on pstrData
   //
   /*if (!GetFont()->GetLogFont(&stFont))
   {    
     memset(&stFont, 0, sizeof(stFont));

     stFont.lfWidth = ((stFont.lfHeight * 7) / 16);
     stFont.lfPitchAndFamily = VARIABLE_PITCH | FF_SWISS;
   }*/
  CFont font;  
  font.CreatePointFont(110, _T("Tahoma Bold"));
  font.GetLogFont(&stFont);
  font.DeleteObject();
  stFont.lfWidth = ((stFont.lfHeight * 12) / 16);
  stFont.lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;
  if (m_fAutoSize)
  {
    if(GetOrientation()==ORIENTATION_HORIZONTAL) stFont.lfHeight = 
         m_rcBounds.Height();
    if(GetOrientation()==ORIENTATION_VERTICAL_LEFT) stFont.lfHeight = 
         m_rcBounds.Width();
    if(GetOrientation()==ORIENTATION_VERTICAL_RIGHT) stFont.lfHeight = 
         m_rcBounds.Width();
  }
  
  if(GetOrientation()==ORIENTATION_HORIZONTAL) 
    stFont.lfEscapement = stFont.lfOrientation = 0;
  if(GetOrientation()==ORIENTATION_VERTICAL_LEFT) 
    stFont.lfEscapement = stFont.lfOrientation = 900;
  if(GetOrientation()==ORIENTATION_VERTICAL_RIGHT) 
    stFont.lfEscapement = stFont.lfOrientation = -900;
  stFont.lfWeight = (pstrData->GetBold() ? FW_HEAVY : FW_NORMAL);
  stFont.lfUnderline = pstrData->GetUnderlined();
  stFont.lfItalic = pstrData->GetItalic();
  stFont.lfQuality = PROOF_QUALITY;
  m_ftText.CreateFontIndirect(&stFont);
}

History

I think it is the first contribution to CodeProject on my banner theme.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionCreating in C# Pin
karizman8510-Mar-11 3:50
karizman8510-Mar-11 3:50 
GeneralVery nice !! Pin
WREY8-Mar-04 12:49
WREY8-Mar-04 12:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.