/////////////////////////////////////////////////////////////////////////////
// Calculate window and text area coordinates
void CScrollEdit::CalculateWindowAndTextArea(void)
{
// Get window coordinates of control
GetWindowRect(&m_oWindowArea);
// Convert coordinates with respect to screen
ScreenToClient(&m_oWindowArea);
// Duplicate window area for calculation purposes
m_oTextArea = m_oWindowArea;
// Deflate area by 2 pixels
m_oTextArea.DeflateRect(2, 2);
}
/////////////////////////////////////////////////////////////////////////////
// Initialize size of text and its starting position
void CScrollEdit::PrepareTextFramesForScrolling(void)
{
// Create a CClientDC object
CClientDC oClientDC(this);
// Select our created font into device context
CFont* pOldFont = oClientDC.SelectObject(&m_oNormalFont);
// Loop through every object pointers in array
for (int i = 0; i < m_oTextFrames.GetSize(); i ++)
{
// Get pointer to current looping object
CTextFrame* pTextFrame = (CTextFrame*)m_oTextFrames.GetAt(i);
// Calculate and set size of text to be drawn based on current device context
pTextFrame->m_szTextSize = oClientDC.GetTextExtent(pTextFrame->m_strText);
}
// Restore original font
oClientDC.SelectObject(pOldFont);
// Check if there are at least 1 text frame in array
if (m_oTextFrames.GetSize() > 0)
{
// Set starting position in pixels for first frame to start ball rolling
((CTextFrame*)m_oTextFrames.GetAt(0))->m_iXOffSets.Add(m_oTextArea.right);
}
}
/////////////////////////////////////////////////////////////////////////////
// Set new position for current scrolling texts based on scroll speed
void CScrollEdit::IncrementScrollingTextFramesPosition(void)
{
// Make sure there are text to scroll
if (m_oTextFrames.GetSize() > 0)
{
// Loop through the indexes of current scrolling text
for (int i = 0; i < m_iTextFramesScrolling.GetSize(); i ++)
{
// Get index of scrolling text frame object
int iIndex = m_iTextFramesScrolling.GetAt(i);
// Get pointer to the text frame object
CTextFrame* pTextFrame = (CTextFrame*)m_oTextFrames.GetAt(iIndex);
// Check for the number of same element if array until current position, excluding
int iXOffSetIndex = NumOfSameElement(m_iTextFramesScrolling, i);
// Get and calcualte new x offset value
int iNewXOffSet = pTextFrame->m_iXOffSets.GetAt(iXOffSetIndex) - SCROLLSPEED;
// Increment position of scrolling text
pTextFrame->m_iXOffSets.SetAt(iXOffSetIndex, iNewXOffSet);
}
}
}
/////////////////////////////////////////////////////////////////////////////
// Set the clipping region so that text not within it will not be drawn
#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 CLR_GREEN RGB(0,255,0)
#define SCROLLRATE 1 // Frequency of timer
#define SCROLLSPEED 1 // Amount of pixels to scroll
#define SCROLLALLOWANCE 50 // Amount of pixels between 2 scroll text
#define ID_SCROLLTIMER (WM_USER + 1001) // ID for scroll timer
// 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_GREEN;
m_clrBackground = CLR_BLACK;
/////////////////////////////////////////////////////////////////////////////
// Create CFont objects for text
void CScrollEdit::CreateTextFonts(void)
{
// Get window font
CFont* pFont = GetFont();
// Create LOGFONT structure
LOGFONT lfLogFont;
// Get LOGFONT structure of current font
pFont->GetLogFont(&lfLogFont);
// 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);
}
/////////////////////////////////////////////////////////////////////////////
// Delete CTextFrame objects for scroll text
void CScrollEdit::DeleteTextFrames(void)
{
// Loop through every object pointers in array
for (int i = 0; i < m_oTextFrames.GetSize(); i ++)
{
// Check if pointer is valid
if (m_oTextFrames.GetAt(i) != NULL)
{
// Free memory of object
delete m_oTextFrames.GetAt(i);
// Assign null pointer value
m_oTextFrames.SetAt(i, NULL);
}
}
class CScrollEdit : public CEdit //CStatic
{
// Construction
public:
// Constructor
CScrollEdit();
// Attributes
public:
CObArray m_oTextFrames; // Array of CTextFrame objects
COLORREF m_clrText; // Text color
COLORREF m_clrBackground; // Control's background color
BOOL m_bStatus;
// Operations
public:
// Start scrolling text
BOOL StartScrolling(BOOL bRestart = TRUE);
// Add scrolling text and its url
BOOL AddScrollText(CString);
// Remove all previously added scroll text
BOOL RemoveAllScrollText(void);
// Calculate window and text area coordinates
void CalculateWindowAndTextArea(void);
// Initialize size of text and its starting position
void PrepareTextFramesForScrolling(void);
// Set new position for current scrolling texts based on scroll speed
void IncrementScrollingTextFramesPosition(void);
// Set the clipping region so that text not within it will not be drawn
void SetClippingRegion(CDC*);
// Draw scrolling text into text area
void DrawScrollingText(CDC*);
// Add index of new scrolling text if allowance satisfied
void AddOnNewScrollingText(void);
// Remove index of finished scrolling text if it runs out of view
void RemoveFinishedScrollingText(void);
// Check the number of same element previously in array
int NumOfSameElement(CArray<int, int>&, int);
// Get index of CTextFrame object in array that is being hovered
int IsTextBeingHovered(CPoint, int*);
// Attributes
private:
UINT m_uiScrollRate; // Timer value for scrolling
UINT m_uiScrollSpeed; // Amount of pixels to scroll
UINT m_uiScrollAllowance; // Amout of pixels between 2 text scroll
CRect m_oTextArea; // Coordinates of text area
CRect m_oWindowArea; // Coordinates of window area
CFont m_oNormalFont; // Pointer to current font of control
CFont m_oUnderlinedFont; // Pointer to current font of control with underlined attribute
/* --- For internal user --- */
CArray<int, int> m_iTextFramesScrolling; // Indexes of current scrolling text frame objects
};