
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
- Change the scrolling text colors, fonts, scroll speed, rate, etc.
- Change the font attributes such as bold hyperlinks, underline them when
hovered.
- Add hyperlink name and its url for scrolling.
- Remove all the hyperlinks that were added.
- Begin scrolling all the added hyperlinks.
1) Change the scrolling text colors, fonts, scroll speed, rate, etc.
#define CLR_RED RGB(255, 0, 0) #define CLR_BLUE RGB(0, 0, 255) #define CLR_BLACK RGB(0, 0, 0) #define CLR_GRAY RGB(178, 178, 178) #define CLR_WHITE RGB(255, 255, 255)
#define SCROLLRATE 1 #define SCROLLSPEED 1 #define SCROLLALLOWANCE 50
CScrollLink::CScrollLink()
{
m_oTextFrames.RemoveAll();
m_iTextFramesScrolling.RemoveAll();
m_uiScrollRate = SCROLLRATE;
m_uiScrollSpeed = SCROLLSPEED;
m_uiScrollAllowance = SCROLLALLOWANCE;
m_clrText = CLR_BLACK;
m_clrHover = CLR_RED;
m_clrBackground = GetSysColor(COLOR_BTNFACE);
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)
{
CFont* pFont = GetFont();
LOGFONT lfLogFont;
pFont->GetLogFont(&lfLogFont);
lfLogFont.lfWeight = FW_BOLD;
m_oNormalFont.CreateFontIndirect(&lfLogFont);
lfLogFont.lfUnderline = TRUE;
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 ,
CString strURL )
{
CTextFrame* pTextFrame = new CTextFrame(strText, strURL);
if (pTextFrame != NULL)
{
m_oTextFrames.Add(pTextFrame);
pTextFrame = NULL;
return TRUE;
}
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)
{
DeleteTextFrames();
Invalidate();
return TRUE;
}
5) Begin scrolling all the added hyperlinks.
After you have added your hyperlinks details, call StartScrolling to begin
scrolling them.
BOOL CScrollLink::StartScrolling(BOOL bRestart)
{
if (m_oTextFrames.GetSize() > 0)
{
KillTimer(ID_SCROLLTIMER);
if (bRestart == TRUE)
{
m_iTextFramesScrolling.RemoveAll();
m_iTextFramesScrolling.Add(0);
PrepareTextFramesForScrolling();
Invalidate();
}
else
{}
SetTimer(ID_SCROLLTIMER, SCROLLRATE, NULL);
return TRUE;
}
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.