7 个解决方案
#1
把代码贴出来让大家研究一下
#2
如何继承的?
#3
代码 太长了 。
#if !defined(AFX_SCROLLLINK_H__10BD0543_310C_11D6_887D_00002126253D__INCLUDED_)
#define AFX_SCROLLLINK_H__10BD0543_310C_11D6_887D_00002126253D__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ScrollLink.h : header file
//
#include <afxtempl.h>
/////////////////////////////////////////////////////////////////////////////
// CScrollEdit window
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);
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CScrollEdit)
public:
virtual BOOL DestroyWindow();
protected:
virtual void PreSubclassWindow();
//}}AFX_VIRTUAL
// Implementation
public:
// Destructor
virtual ~CScrollEdit();
// Generated message map functions
protected:
//{{AFX_MSG(CScrollEdit)
afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
afx_msg void OnPaint();
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnKillFocus(CWnd* pNewWnd);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
// Operations
private:
// Create CFont objects for text
void CreateTextFonts(void);
// Delete CTextFrame objects
void DeleteTextFrames(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
};
/////////////////////////////////////////////////////////////////////////////
class CTextFrame : public CObject
{
// Operations
public:
// Constructors
CTextFrame();
CTextFrame(CString);
// Destructor
virtual ~CTextFrame();
// Attrbutes
public:
CSize m_szTextSize; // Size of text
CString m_strText; // Text to scroll
CArray<int, int> m_iXOffSets; // X coordinates of text frames currently showing
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_SCROLLLINK_H__10BD0543_310C_11D6_887D_00002126253D__INCLUDED_)
#if !defined(AFX_SCROLLLINK_H__10BD0543_310C_11D6_887D_00002126253D__INCLUDED_)
#define AFX_SCROLLLINK_H__10BD0543_310C_11D6_887D_00002126253D__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ScrollLink.h : header file
//
#include <afxtempl.h>
/////////////////////////////////////////////////////////////////////////////
// CScrollEdit window
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);
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CScrollEdit)
public:
virtual BOOL DestroyWindow();
protected:
virtual void PreSubclassWindow();
//}}AFX_VIRTUAL
// Implementation
public:
// Destructor
virtual ~CScrollEdit();
// Generated message map functions
protected:
//{{AFX_MSG(CScrollEdit)
afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
afx_msg void OnPaint();
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnKillFocus(CWnd* pNewWnd);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
// Operations
private:
// Create CFont objects for text
void CreateTextFonts(void);
// Delete CTextFrame objects
void DeleteTextFrames(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
};
/////////////////////////////////////////////////////////////////////////////
class CTextFrame : public CObject
{
// Operations
public:
// Constructors
CTextFrame();
CTextFrame(CString);
// Destructor
virtual ~CTextFrame();
// Attrbutes
public:
CSize m_szTextSize; // Size of text
CString m_strText; // Text to scroll
CArray<int, int> m_iXOffSets; // X coordinates of text frames currently showing
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_SCROLLLINK_H__10BD0543_310C_11D6_887D_00002126253D__INCLUDED_)
#4
// ScrollLink.cpp : implementation file
//
#include "stdafx.h"
#include "Resource.h"
#include "ScrollEdit.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// Constant definitions
#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
/////////////////////////////////////////////////////////////////////////////
// CScrollEdit
CScrollEdit::CScrollEdit()
{
// 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_GREEN;
m_clrBackground = CLR_BLACK;
m_bStatus = TRUE;
}
CScrollEdit::~CScrollEdit()
{
// Delete CTextFrame objects
DeleteTextFrames();
}
BEGIN_MESSAGE_MAP(CScrollEdit, CEdit)
//{{AFX_MSG_MAP(CScrollEdit)
ON_WM_PAINT()
ON_WM_TIMER()
ON_WM_SIZE()
ON_WM_CONTEXTMENU()
ON_WM_LBUTTONDOWN()
ON_WM_KILLFOCUS()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// 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);
}
}
// Empty array
m_oTextFrames.RemoveAll();
m_iTextFramesScrolling.RemoveAll();
}
//
#include "stdafx.h"
#include "Resource.h"
#include "ScrollEdit.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// Constant definitions
#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
/////////////////////////////////////////////////////////////////////////////
// CScrollEdit
CScrollEdit::CScrollEdit()
{
// 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_GREEN;
m_clrBackground = CLR_BLACK;
m_bStatus = TRUE;
}
CScrollEdit::~CScrollEdit()
{
// Delete CTextFrame objects
DeleteTextFrames();
}
BEGIN_MESSAGE_MAP(CScrollEdit, CEdit)
//{{AFX_MSG_MAP(CScrollEdit)
ON_WM_PAINT()
ON_WM_TIMER()
ON_WM_SIZE()
ON_WM_CONTEXTMENU()
ON_WM_LBUTTONDOWN()
ON_WM_KILLFOCUS()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// 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);
}
}
// Empty array
m_oTextFrames.RemoveAll();
m_iTextFramesScrolling.RemoveAll();
}
#5
/////////////////////////////////////////////////////////////////////////////
// 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);
// Empty array
pTextFrame->m_iXOffSets.RemoveAll();
// 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
void CScrollEdit::SetClippingRegion(CDC* pDC /* Pointer to CDC object */)
{
// Create CRgn object
CRgn oClipRgn;
// Create clip region for text area
oClipRgn.CreateRectRgnIndirect(&m_oTextArea);
// Set clip region
pDC->SelectClipRgn(&oClipRgn);
}
// 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);
// Empty array
pTextFrame->m_iXOffSets.RemoveAll();
// 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
void CScrollEdit::SetClippingRegion(CDC* pDC /* Pointer to CDC object */)
{
// Create CRgn object
CRgn oClipRgn;
// Create clip region for text area
oClipRgn.CreateRectRgnIndirect(&m_oTextArea);
// Set clip region
pDC->SelectClipRgn(&oClipRgn);
}
#6
太长,大哥!你只要把重要的贴出来就行了。比如类的声明、使用、出错位置等。
this指针为空一般表示对象不存在,检查是否new。句柄无效检查一下create();
this指针为空一般表示对象不存在,检查是否new。句柄无效检查一下create();
#7
好长,帮你顶
#1
把代码贴出来让大家研究一下
#2
如何继承的?
#3
代码 太长了 。
#if !defined(AFX_SCROLLLINK_H__10BD0543_310C_11D6_887D_00002126253D__INCLUDED_)
#define AFX_SCROLLLINK_H__10BD0543_310C_11D6_887D_00002126253D__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ScrollLink.h : header file
//
#include <afxtempl.h>
/////////////////////////////////////////////////////////////////////////////
// CScrollEdit window
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);
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CScrollEdit)
public:
virtual BOOL DestroyWindow();
protected:
virtual void PreSubclassWindow();
//}}AFX_VIRTUAL
// Implementation
public:
// Destructor
virtual ~CScrollEdit();
// Generated message map functions
protected:
//{{AFX_MSG(CScrollEdit)
afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
afx_msg void OnPaint();
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnKillFocus(CWnd* pNewWnd);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
// Operations
private:
// Create CFont objects for text
void CreateTextFonts(void);
// Delete CTextFrame objects
void DeleteTextFrames(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
};
/////////////////////////////////////////////////////////////////////////////
class CTextFrame : public CObject
{
// Operations
public:
// Constructors
CTextFrame();
CTextFrame(CString);
// Destructor
virtual ~CTextFrame();
// Attrbutes
public:
CSize m_szTextSize; // Size of text
CString m_strText; // Text to scroll
CArray<int, int> m_iXOffSets; // X coordinates of text frames currently showing
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_SCROLLLINK_H__10BD0543_310C_11D6_887D_00002126253D__INCLUDED_)
#if !defined(AFX_SCROLLLINK_H__10BD0543_310C_11D6_887D_00002126253D__INCLUDED_)
#define AFX_SCROLLLINK_H__10BD0543_310C_11D6_887D_00002126253D__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ScrollLink.h : header file
//
#include <afxtempl.h>
/////////////////////////////////////////////////////////////////////////////
// CScrollEdit window
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);
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CScrollEdit)
public:
virtual BOOL DestroyWindow();
protected:
virtual void PreSubclassWindow();
//}}AFX_VIRTUAL
// Implementation
public:
// Destructor
virtual ~CScrollEdit();
// Generated message map functions
protected:
//{{AFX_MSG(CScrollEdit)
afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
afx_msg void OnPaint();
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnKillFocus(CWnd* pNewWnd);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
// Operations
private:
// Create CFont objects for text
void CreateTextFonts(void);
// Delete CTextFrame objects
void DeleteTextFrames(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
};
/////////////////////////////////////////////////////////////////////////////
class CTextFrame : public CObject
{
// Operations
public:
// Constructors
CTextFrame();
CTextFrame(CString);
// Destructor
virtual ~CTextFrame();
// Attrbutes
public:
CSize m_szTextSize; // Size of text
CString m_strText; // Text to scroll
CArray<int, int> m_iXOffSets; // X coordinates of text frames currently showing
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_SCROLLLINK_H__10BD0543_310C_11D6_887D_00002126253D__INCLUDED_)
#4
// ScrollLink.cpp : implementation file
//
#include "stdafx.h"
#include "Resource.h"
#include "ScrollEdit.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// Constant definitions
#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
/////////////////////////////////////////////////////////////////////////////
// CScrollEdit
CScrollEdit::CScrollEdit()
{
// 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_GREEN;
m_clrBackground = CLR_BLACK;
m_bStatus = TRUE;
}
CScrollEdit::~CScrollEdit()
{
// Delete CTextFrame objects
DeleteTextFrames();
}
BEGIN_MESSAGE_MAP(CScrollEdit, CEdit)
//{{AFX_MSG_MAP(CScrollEdit)
ON_WM_PAINT()
ON_WM_TIMER()
ON_WM_SIZE()
ON_WM_CONTEXTMENU()
ON_WM_LBUTTONDOWN()
ON_WM_KILLFOCUS()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// 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);
}
}
// Empty array
m_oTextFrames.RemoveAll();
m_iTextFramesScrolling.RemoveAll();
}
//
#include "stdafx.h"
#include "Resource.h"
#include "ScrollEdit.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// Constant definitions
#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
/////////////////////////////////////////////////////////////////////////////
// CScrollEdit
CScrollEdit::CScrollEdit()
{
// 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_GREEN;
m_clrBackground = CLR_BLACK;
m_bStatus = TRUE;
}
CScrollEdit::~CScrollEdit()
{
// Delete CTextFrame objects
DeleteTextFrames();
}
BEGIN_MESSAGE_MAP(CScrollEdit, CEdit)
//{{AFX_MSG_MAP(CScrollEdit)
ON_WM_PAINT()
ON_WM_TIMER()
ON_WM_SIZE()
ON_WM_CONTEXTMENU()
ON_WM_LBUTTONDOWN()
ON_WM_KILLFOCUS()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// 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);
}
}
// Empty array
m_oTextFrames.RemoveAll();
m_iTextFramesScrolling.RemoveAll();
}
#5
/////////////////////////////////////////////////////////////////////////////
// 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);
// Empty array
pTextFrame->m_iXOffSets.RemoveAll();
// 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
void CScrollEdit::SetClippingRegion(CDC* pDC /* Pointer to CDC object */)
{
// Create CRgn object
CRgn oClipRgn;
// Create clip region for text area
oClipRgn.CreateRectRgnIndirect(&m_oTextArea);
// Set clip region
pDC->SelectClipRgn(&oClipRgn);
}
// 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);
// Empty array
pTextFrame->m_iXOffSets.RemoveAll();
// 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
void CScrollEdit::SetClippingRegion(CDC* pDC /* Pointer to CDC object */)
{
// Create CRgn object
CRgn oClipRgn;
// Create clip region for text area
oClipRgn.CreateRectRgnIndirect(&m_oTextArea);
// Set clip region
pDC->SelectClipRgn(&oClipRgn);
}
#6
太长,大哥!你只要把重要的贴出来就行了。比如类的声明、使用、出错位置等。
this指针为空一般表示对象不存在,检查是否new。句柄无效检查一下create();
this指针为空一般表示对象不存在,检查是否new。句柄无效检查一下create();
#7
好长,帮你顶