65.9K
CodeProject 正在变化。 阅读更多。
Home

横幅控件

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.67/5 (5投票s)

2004 年 3 月 8 日

viewsIcon

39467

downloadIcon

1387

关于应用程序中类似横幅的控件

引言

我认为编程中最糟糕的事情之一就是制作真正有用的界面。因此,使用现有的解决方案是首选。但有时我们需要实现自己的解决方案,因为找到的解决方案无法达到预期的结果。我希望这个应用程序能够帮助人们简化他们的生活和工作。

背景

在我的工作中,我需要使用Banner控件。在Code Project上,我很快找到了PaulWendtPeter Mares的解决方案,并决定结合他们的解决方案。

使用代码

这个应用程序演示了一个带有停靠功能的Banner控件,并且字体方向根据停靠位置进行调整。通过窗体控件,我们可以改变滚动速度,并操作Banner数据库,修改文本属性和Banner位置。

我在CMultiColorStatic 类中添加了方向调整功能。

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

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

我还添加了在CBannerStatic 类中的Paint函数中的连续插入。

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;
  }
}

关注点

我不明白为什么系统字体没有根据给定的方向改变。我没有时间弄清楚这种情况,所以我使用了其他字体。

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);
}

历史

我认为这是我第一次在CodeProject上贡献关于Banner主题的文章。

© . All rights reserved.