Click here to Skip to main content
6,594,932 members and growing! (14,970 online)
Email Password   helpLost your password?
Desktop Development » Static & Panel Controls » Custom Controls     Intermediate

Hyperlink Scroller

By Weiye Chen

An article on scrolling your hyperlinks, something like a news ticker
VC6, VC7Win2K, WinXP, MFC, Dev
Posted:21 Nov 2002
Views:96,946
Bookmarked:25 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
21 votes for this article.
Popularity: 6.19 Rating: 4.68 out of 5

1

2

3
1 vote, 7.1%
4
13 votes, 92.9%
5

Sample Image

Introduction

This article is about a subclassed static control which you can use to scroll hyperlinks from right to left. I once used it to scroll news headlines as hyperlinks in an application.

Using the code

The source for this subclassed control is in ScrollLink.h and ScrollLink.cpp. In it, you will find 2 classes, CScrollLink and CTextFrame. The latter is used by CScrollLink for each hyperlink to scroll, so you don't really need to know much about it to use this hyperlink scroller. To use it, you need to use a Static control and subclassed with CScrollLink. Note that you need to check the 'Notify' property of the static control.

The code listed below will show you how to 

  1. Change the scrolling text colors, fonts, scroll speed, rate, etc.
  2. Change the font attributes such as bold hyperlinks, underline them when hovered.
  3. Add hyperlink name and its url for scrolling.
  4. Remove all the hyperlinks that were added.
  5. Begin scrolling all the added hyperlinks.

1) Change the scrolling text colors, fonts, scroll speed, rate, etc.

#define CLR_RED RGB(255, 0, 0) // Red color

#define CLR_BLUE RGB(0, 0, 255) // Blue color

#define CLR_BLACK RGB(0, 0, 0) // Black color

#define CLR_GRAY RGB(178, 178, 178) // Gray color

#define CLR_WHITE RGB(255, 255, 255) // White color


#define SCROLLRATE 1 // Frequency of timer

#define SCROLLSPEED 1 // Amount of pixels to scroll

#define SCROLLALLOWANCE 50 // Amount of pixels between 2 scroll text
CScrollLink::CScrollLink()
{
    // Empty array

    m_oTextFrames.RemoveAll();
    m_iTextFramesScrolling.RemoveAll();

    // Scroll rate is the value used in SetTimer(), which scrolls your hyperlinks to

    // the left by x pixels, where x is the scroll speed value. Scroll allowance is the

    // gap in pixels between 2 scrolling hyperlinks

    m_uiScrollRate = SCROLLRATE;
    m_uiScrollSpeed = SCROLLSPEED;
    m_uiScrollAllowance = SCROLLALLOWANCE;

    // The text of the hyperlink will be black if there is no url for it. If not, it will

    // be blue. The color of the hyperlink is set to red when mouse it is hovered and the

    // background color of the control is set to the same as the button color.

    m_clrText = CLR_BLACK;
    m_clrHover = CLR_RED;
    m_clrBackground = GetSysColor(COLOR_BTNFACE);

    // Set flag

    m_bOnHover = FALSE;
}

2) Change the font attributes such as bold hyperlinks, underline them when hovered.

This hyperlink scroller uses the same font as its parent. You can change its font attributes for the scrolling text as well as when it is hovered in CreateTextFonts as show below.

void CScrollLink::CreateTextFonts(void)
{
    // Get window font

    CFont* pFont = GetFont();
    
    // Create LOGFONT structure

    LOGFONT lfLogFont;

    // Get LOGFONT structure of current font

    pFont->GetLogFont(&lfLogFont);

    // Set font to be bold

    lfLogFont.lfWeight = FW_BOLD;

    // Create normal font that is bold (when not hovered)

    m_oNormalFont.CreateFontIndirect(&lfLogFont);
    
    // Set underline attribute

    lfLogFont.lfUnderline = TRUE;

    // Create current font with underline attribute (when hovered)

    m_oUnderlinedFont.CreateFontIndirect(&lfLogFont);
}

3) Add hyperlink name and its url for scrolling.

Now to add your hyperlink to the scroller, you should use AddScrollText as shown below. Note that you can pass an empty url string and the scrolled text will appear as a normal text instead of a hyperlink.

BOOL CScrollLink::AddScrollText(CString strText /* Text to scroll */, 
                                CString strURL /* URL behind scrolled text */)
{
    // Allocate memory for CTextFrame object

    CTextFrame* pTextFrame = new CTextFrame(strText, strURL);

    // Check if memory allocation successful

    if (pTextFrame != NULL)
    {
        // Store object into array

        m_oTextFrames.Add(pTextFrame);

        // Assign null pointer value

        pTextFrame = NULL;

        // Indicate success

        return TRUE;
    }

    // Indicate failure

    return FALSE;
}

4) Remove all the hyperlinks that were added.

And to clear all the hyperlinks that you have added, call RemoveAllScrollText.

BOOL CScrollLink::RemoveAllScrollText(void)
{
    // Remove all scroll text

    DeleteTextFrames();

    // Repaint

    Invalidate();

    // Indicate success

    return TRUE;
}

5) Begin scrolling all the added hyperlinks.

After you have added your hyperlinks details, call StartScrolling to begin scrolling them.

/* bRestart = Flag to indicate whether to restart scrolling */
BOOL CScrollLink::StartScrolling(BOOL bRestart)
{
    // Make sure there are text to scroll

    if (m_oTextFrames.GetSize() > 0)
    {
        // Stop timer if any

        KillTimer(ID_SCROLLTIMER);

        // Check if scrolling is to be restarted

        if (bRestart == TRUE)
        {
            // Empty array of indexes of current scrolling text frames

            m_iTextFramesScrolling.RemoveAll();

            // Store index of the first text frame to be scrolling

            m_iTextFramesScrolling.Add(0);

            // Initialize size of text and its starting position

            PrepareTextFramesForScrolling();

            // Clear any previous scroll text

            Invalidate();
        }

        // Scrolling is to be continued

        else
        {}

        // Start timer

        SetTimer(ID_SCROLLTIMER, SCROLLRATE, NULL);

        // Indicate success

        return TRUE;
    }

    // Indicate failure

    return FALSE;
}

Points of Interest

Well, I need to apologize if my code comments appear vague. I am still improving on that. Also, I know that some of my code could be further improved, so I will be open to your comments, suggestions and criticisms.

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

About the Author

Weiye Chen


Member

Occupation: Web Developer
Location: Singapore Singapore

Other popular Static & Panel Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 35 (Total in Forum: 35) (Refresh)FirstPrevNext
GeneralMutiThread problems Pinmembershirley_cl18:00 15 Jul '09  
GeneralLittle question about using this control in ATL project Pinmembermfranco_neto6:46 5 Oct '06  
Generala little suggestion or improvement? Pinmemberice_lei23:55 18 Sep '06  
QuestionHow to display not fliking Pinmemberpyoon4:31 17 Apr '06  
GeneralHow to change the SCROLLRATE and SCROLLSPEED Pinmemberchinakknd22:15 26 Mar '06  
GeneralRe: How to change the SCROLLRATE and SCROLLSPEED PinmemberWeiye Chen2:57 27 Mar '06  
QuestionCan I creat the control with 'creat' method Pinmemberchgrbr21:37 31 Oct '05  
AnswerRe: Can I creat the control with 'creat' method PinmemberWeiye Chen22:18 8 Nov '05  
Generaltransparent text and halts Pinsusssir kaber14:25 28 Jul '05  
Generalright to left scroll ? Pinmemberwaelahmed17:44 5 Sep '04  
GeneralRe: right to left scroll ? PinmemberWeiye Chen1:59 6 Sep '04  
GeneralRe: right to left scroll ? Pinmemberwaelahmed18:07 11 Sep '04  
GeneralRe: right to left scroll ? PinmemberWeiye Chen18:55 11 Sep '04  
GeneralRe: right to left scroll ? Pinmemberwaelahmed18:57 11 Sep '04  
GeneralRe: Hi, I have two question! PinmemberWeiye Chen22:05 23 Jun '04  
GeneralRe: Image Background PinmemberWeiye Chen21:03 15 Jun '04  
GeneralTransparent background PinmemberWolfSupernova5:55 8 Nov '03  
GeneralRe: Transparent background PinmemberWolfSupernova6:29 8 Nov '03  
GeneralRe: Transparent background PinmemberWeiyeChen5:22 9 Nov '03  
GeneralBigger Font PinmemberRizi20:57 29 Sep '03  
GeneralRe: Bigger Font PinmemberWeiyeChen3:14 30 Sep '03  
GeneralObject ID problem Pinmembergigi boieru23:30 28 Jul '03  
GeneralRe: Object ID problem PinmemberWeiye Chen19:02 29 Jul '03  
GeneralRe: Object ID problem Pinmembergigi boieru5:38 31 Jul '03  
GeneralUhm PinmemberTak11:25 23 Nov '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 Nov 2002
Editor: Chris Maunder
Copyright 2002 by Weiye Chen
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project