Scrolling Banners -- the MFC Way

Have you ever wanted to have a control on one of your applications that behaves like some of those internet banners? You'll have a banner and then just scrolling text that whips by? Another example of the effect I'm going for was the news ticker on AOL instant messenger. A bunch of hyperlinks whip by with news headlines; you click on the news headlines to go to the article. This type of thing is possible with the CBannerStatic class. Of course, it would be no good if all strings had to have the same style and color, would it? Of course not; each string should be able to have its own individual style and color. So what are we going to need to have this control?

First off, we're going to need a collection of strings; each string has its own color and style [ie: underlined, italicized, etc...] -- enter the CColorString class. It derives from CString. It uses a DWORD to hold both the color and value parameters. Since a COLORREF uses only the low 3 bytes to keep track of colors, I use the high byte to store style information ... like whether the string is italicized, underlined, or bold. CColorString also can store a background color; I waste another DWORD for this. In the future, I may move the style values to BOOL types so that the code is more understandable; we're supposed to get away from bit-masking operations in C++!

Now that we have a class to encapsulate strings that know their own style, we need a control that knows how to display them. Enter CMultiColorStatic. It's quite similar to CColorStatic-type controls ... but it is one static object that will allow multiple strings to be entered; each string has its own style. This is done by having a CPtrArray keep track of all of the CColorStrings that have been added to the control. Granted, there isn't a whole lot of gain by using one CMultiColorStatic instead of a bunch of CColorStatic's; I just wanted one control to be the base for CBannerStatic.

Speaking of CBannerStatic, that's the last piece of the puzzle. CBannerStatic combines scrolling with having multiple strings. For scrolling, it uses multimedia timers so that similar precision can be achieved on any windows platform [with normal timers, WinNT has 10ms precision, while Win9x has only 55ms precision]. I use the multimedia timers only for precision; when I receive the timer message, I post a WM_TIMER message to the control and the drawing is done in the WM_TIMER handler. When drawing becomes complicated, the app containing the banner could become unresponsive because the drawing takes longer than the scrolling ... and by the time the drawing is done it's time to scroll again, etc.... By retaining a message structure, the app will never become unresponsive.

The entire thing is fairly simple to use, but it'll take some time getting used to, I think. CBannerStatic provides a neat way to allow the user to click on an item and get results. The client can set an item cursor as well as an ItemClick callback function. It'd be too long to put everything here, but the sample app BannerTester is pretty comprehensive in going through the feature-set of CBannerStatic [as well as CMultiColorStatic].

Some caveats: The thing can be slow if it is made to be large and/or has a lot of message strings. It will bog down the entire system so beware!

One last note: I don't like to use Precompiled Headers and you'll find that if you just use the classes as provided, the compiler will complain about not having "StdAfx.h" included. If you get this problem, you can just add #include "StdAfx.h" to each cpp file for each of the three classes.

Downloads

Download demo project - 26 Kb
Download source - 13 Kb


Comments

  • How to reset the position of the text ?

    Posted by Legacy on 08/30/2002 12:00am

    Originally posted by: Xavier Ricco

    First of all, nice work !!!

    I want the banner to move only when the
    size of the text is >>> client area ...

    I have slightly modified the code in order
    to make this "automove" thing and it works ...

    Now, i have the following problem :

    When i change the text to another one,
    it is staying to its position ...

    Is there a way to reset the text
    to its initial position ?

    Thx in advance,

    Xavier


    Reply
  • Banner not working in mutiple dialogs

    Posted by Legacy on 07/09/2002 12:00am

    Originally posted by: Arun Kumar

    Hai,

    I would like to post a problem, which i faced , when the Bannerstatic is used in multiple dialog application,
    I am using banner in each of the dialog and i see that , after some dialog , the banner is stopped scrolling and i need to restart my application to again make it scroll.

    What could be wrong ?
    Why when the dialogs are switched fastly, the scroll is stopped?

    Please help me.

    Arun

    Reply
  • How do i make it efficient for large number of strings?

    Posted by Legacy on 03/22/2002 12:00am

    Originally posted by: Arati

    It works really well!
    What should i do if i want to have large number of strings in my banner? can u suggest a workaround which would not bog the CPU down?
    I am waiting for your reply.

    Thanks in advance,
    Arati.

    Reply
  • Very Nice.

    Posted by Legacy on 02/16/2002 12:00am

    Originally posted by: Jai Ganesh.M.G.

    By all the way, This is a wonderful work.
    Keep it up, Paul.

    Keep smiling,
    Jai.

    Reply
  • scrolling banners

    Posted by Legacy on 09/27/2001 12:00am

    Originally posted by: Alex

    There are countless gif gernerator apps that do the scrolling banner job for you.
    You need only a Static Gif Control in your dialog app.
    This is realized in VC7 .net.

    Alex

    Reply
  • Nice control but...

    Posted by Legacy on 09/19/2001 12:00am

    Originally posted by: Per Franck

    It's still 'choppy'.

    Shouldn't there be a way to produce perfectly smooth scrolling text in today's machines?

    Reply
  • Fix for Create() bug and Div By Zero issue

    Posted by Legacy on 09/17/2001 12:00am

    Originally posted by: Chris Hambleton

    I found a fix for the divide by zero bug in the release build and the issue with Create() failing.

    Add the following check at the beginning of CBannerStatic::CalculateScrollParameters():

    if(m_nBannerSpeed == 0)
    return;

    It looks like what's happening is that the Calc function is being called by OnSize() when m_nBannerSpeed is still 0...

    Reply
  • Change Font

    Posted by Legacy on 08/03/2001 12:00am

    Originally posted by: Uellinton Mendes

    How change BannerStatic's font

    Reply
  • How use with Create

    Posted by Legacy on 08/02/2001 12:00am

    Originally posted by: Uellinton Mendes

    I use:

    CBannerStati m_wndBanner;

    if (!m_wndBanner.Create(NULL, WS_CHILD | WS_VISIBLE, rcBanner, this, IDC_BANNERNEWS))
    return false;

    int m_nRedValue = 0;
    int m_nGreenValue = 0;
    int m_nBlueValue = 0;
    BOOL m_fBold = true;
    BOOL m_fItalic = false;
    BOOL m_fUnderlined = false;
    BOOL m_fHaveHandCursor = true;

    DWORD dwAddStyle = RGB(m_nRedValue, m_nGreenValue, m_nBlueValue);
    dwAddStyle |= (m_fItalic ? COLORSTRING_ITALIC : 0);
    dwAddStyle |= (m_fBold ? COLORSTRING_BOLD : 0);
    dwAddStyle |= (m_fUnderlined ? COLORSTRING_UNDERLINE : 0);
    CColorString strAddAppearance("Teste", dwAddStyle);
    m_wndBanner.AddString(strAddAppearance);

    But.. Generate a error. why?

    Reply
  • CStatic + Banner

    Posted by Legacy on 07/31/2001 12:00am

    Originally posted by: Vasudevan

    I tried to create A CBannerstatic control dynamically using the Create window funtcion.The program worked fine in the debu

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As everyone scrambles to protect customers and consumers from the Heartbleed virus, there will be a variety of mitigating solutions offered up to address this pesky bug. There are a variety of points within the data path where solutions could be put into place to mitigate this (and similar) vulnerabilities and customers must choose the most strategic point in the network at which to deploy their selected mitigation. Read this white paper to learn the ins and outs of mitigating the risk of Heartbleed and the …

  • Best-in-Class organizations execute on a strategy that supports the multi-channel nature of customer requests. These leading organizations do not just open up their service infrastructures to accommodate new channels, but also empower their teams to deliver an effective and consistent experience regardless of the channel selected by the customer. This document will highlight the key business capabilities that support a Best-in-Class customer engagement strategy.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds