您的位置:首页 > 其它

继承Cwnd实现的滚动文本窗口TEdit控件

2011-05-19 16:11 204 查看
直接继承Cwnd实现的滚动文本窗口,可以设置需要显示的文本,字体大小,字体颜色,窗口背景颜色,滚动速度;

窗口自动根据字体大小换行滚动显示;还有什么功能后续想到再加进去。

代码如下:

//CTEdit.h
#pragma once
#include <string>

// CTEdit

class CTEdit : public CWnd
{
DECLARE_DYNAMIC(CTEdit)

public:
CTEdit(unsigned long nbackColor = RGB(255, 255, 255));
virtual ~CTEdit();

protected:
DECLARE_MESSAGE_MAP()

public:
virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle,
const RECT& rect, CWnd* pParentWnd, UINT nID);

void SetAllText(std::string& allText);
std::string& GetAllText();

void SetTextFont(CFont* font);
CFont* GetTextFont();
void StartShowStr();

void SetScrollSpeed(unsigned int iSpeed);
unsigned int GetScrollSpeed() ;

void SetTextColor1(unsigned long ntextColor);
unsigned long GetTextColor1();

protected:
afx_msg void OnPaint();
afx_msg void OnTimer(UINT_PTR nIDEvent);

afx_msg BOOL RegisterCalss();
afx_msg void InitVariables(unsigned long nbackColor);

afx_msg void GetShowStr();

afx_msg void GetCharWidthAndHeight();

private:
std::string m_ShowStr;
std::string allStr;
int lineCharCount;
int lineCount;
int strPosition;

unsigned int minTime;

CRect controlRect;

unsigned long backColor;
unsigned long textColor;

CFont * pFont;
BOOL Flag;
};


// TEdit.cpp : implementation file
//

#include "stdafx.h"
//#include "KKKDsoFramer.h"
#include "TEdit.h"

// CTEdit

IMPLEMENT_DYNAMIC(CTEdit, CWnd)

CTEdit::CTEdit(unsigned long nbackColor)
{
InitVariables(nbackColor);
}

CTEdit::~CTEdit()
{
}

BEGIN_MESSAGE_MAP(CTEdit, CWnd)
ON_WM_PAINT()
ON_WM_TIMER()
END_MESSAGE_MAP()

// CTEdit message handlers

BOOL CTEdit::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID)
{
controlRect.CopyRect(&rect);
return CWnd::Create(lpszClassName,lpszWindowName, dwStyle, rect, pParentWnd, nID);
}

void CTEdit::OnPaint()
{
CRect rect;
CPaintDC dc(this);
dc.SelectObject(pFont);
CWnd::GetClientRect(rect);
dc.SetTextColor(textColor);
dc.SetBkMode(TRANSPARENT);

dc.DrawText(_T(m_ShowStr.c_str()), rect, DT_CENTER); //DT_LEFT, DT_RIGHT
}

//设置需要显示的文本
void CTEdit::SetAllText(std::string& allText)
{
allStr = allText;
}

std::string& CTEdit::GetAllText()
{
return allStr;
}

//设置显示字体
void CTEdit::SetTextFont(CFont* font)
{
pFont = font;
}

CFont* CTEdit::GetTextFont()
{
return pFont;
}

void CTEdit::SetScrollSpeed(unsigned int iSpeed)
{
minTime = iSpeed;
}

unsigned int CTEdit::GetScrollSpeed()
{
return minTime;
}

void CTEdit::StartShowStr()
{
GetCharWidthAndHeight();
SetTimer(1, minTime, NULL);
}

void CTEdit::SetTextColor1(unsigned long ntextColor)
{
textColor = ntextColor;
}

unsigned long CTEdit::GetTextColor1()
{
return textColor;
}

void CTEdit::OnTimer(UINT_PTR nIDEvent)
{
GetShowStr();
UpdateWindow();
Invalidate();
}

void CTEdit::GetCharWidthAndHeight()
{
CDC* dc = GetDC();
std::string str("H");
SIZE size;
CRect rect;
dc->SelectObject(pFont);
if(::GetTextExtentPoint32((HDC)dc->GetSafeHdc(),str.c_str(),str.length(),&size))
{
int charWidth = size.cx / str.length();

GetClientRect(rect);
lineCharCount = rect.Width() / charWidth ;
lineCount = rect.Height() / size.cy;
}
}

void CTEdit::GetShowStr()
{
int temp_pos = 0, i = 0;
int real_LineCout = 0, real_ShowTimes = 0;
std::string	temp;
m_ShowStr = "";

real_ShowTimes = (allStr.length() + lineCharCount * lineCount) / (lineCount * lineCharCount);

temp_pos = strPosition;

for (i = 0; i < lineCount; i++)
{
if (temp_pos  > allStr.length())
{
break;
}
else
{
temp = allStr.substr(temp_pos, lineCharCount);
}

temp += "/n";
m_ShowStr += temp;
temp_pos += lineCharCount;
}
strPosition += lineCharCount;
if (strPosition >= allStr.length())
{
strPosition = 0;
}
}

BOOL CTEdit::RegisterCalss()
{
//注册窗口
WNDCLASS wc;
memset(&wc, 0, sizeof(WNDCLASS));

wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = ::DefWindowProc;
wc.hInstance = AfxGetInstanceHandle();
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = (HBRUSH)(CreateSolidBrush(backColor));
wc.lpszMenuName = NULL;
wc.lpszClassName = _T("CTEdit");

if (!AfxRegisterClass(&wc))
{
TRACE("Class registration Failer");
return FALSE;
}
return TRUE;
}

void CTEdit::InitVariables(unsigned long nbackColor)
{
{
lineCount = 0;
lineCharCount = 0;
strPosition = 0;
minTime = 2000;
backColor = nbackColor;
textColor = RGB(0, 0, 0);
pFont = new CFont();
pFont->CreatePointFont(10, "Arial");
}

if (!RegisterCalss())
{
TRACE("Class registration Failer");
}
}


把上面的重新改了下,这下是真的滚动了。

//CTEdit.h

#pragma once
#include <string>

// CTEdit

class CTEdit : public CWnd
{
DECLARE_DYNAMIC(CTEdit)

public:
CTEdit(unsigned long nbackColor = RGB(255, 255, 255));
virtual ~CTEdit();

protected:
DECLARE_MESSAGE_MAP()

public:
virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle,
const RECT& rect, CWnd* pParentWnd, UINT nID);

void SetAllText(std::string& allText);
std::string& GetAllText();

void SetTextFont(CFont* font);
CFont* GetTextFont();
void StartShowStr();

void SetScrollSpeed(unsigned int iSpeed);
unsigned int GetScrollSpeed() ;

void SetTextColor1(unsigned long ntextColor);
unsigned long GetTextColor1();

protected:
afx_msg void OnPaint();
afx_msg void OnTimer(UINT_PTR nIDEvent);

afx_msg BOOL RegisterCalss();
afx_msg void InitVariables(unsigned long nbackColor);

afx_msg void GetShowStr();

afx_msg void GetCharWidthAndHeight();

private:
std::string m_ShowStr;
std::string allStr;
int lineCharCount;
int lineCount;
int charWidth;
int charHeight;
int strPosition;

unsigned int minTime;

CRect controlRect;

unsigned long backColor;
unsigned long textColor;

CFont * pFont;
BOOL Flag;
int curTimes; //当前移动到第几次
long rectTop; //移动的矩形Y坐标
};


// TEdit.cpp : implementation file
//

#include "stdafx.h"
//#include "KKKDsoFramer.h"
#include "TEdit.h"

// CTEdit

IMPLEMENT_DYNAMIC(CTEdit, CWnd)

CTEdit::CTEdit(unsigned long nbackColor)
{
InitVariables(nbackColor);
}

CTEdit::~CTEdit()
{
}

BEGIN_MESSAGE_MAP(CTEdit, CWnd)
ON_WM_PAINT()
ON_WM_TIMER()
END_MESSAGE_MAP()

// CTEdit message handlers

BOOL CTEdit::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID)
{
controlRect.CopyRect(&rect);
return CWnd::Create(lpszClassName,lpszWindowName, dwStyle, rect, pParentWnd, nID);
}

void CTEdit::OnPaint()
{
CRect rect;
CPaintDC dc(this);
dc.SelectObject(pFont);
CWnd::GetClientRect(rect);
dc.SetTextColor(textColor);
dc.SetBkMode(TRANSPARENT);
rect.top = rectTop--;
//rect.left += 2;
dc.DrawText(_T(m_ShowStr.c_str()), rect, DT_CENTER); //DT_LEFT, DT_RIGHT
}

//设置需要显示的文本
void CTEdit::SetAllText(std::string& allText)
{
allStr = allText;
}

std::string& CTEdit::GetAllText()
{
return allStr;
}

//设置显示字体
void CTEdit::SetTextFont(CFont* font)
{
pFont = font;
}

CFont* CTEdit::GetTextFont()
{
return pFont;
}

void CTEdit::SetScrollSpeed(unsigned int iSpeed)
{
minTime = iSpeed;
}

unsigned int CTEdit::GetScrollSpeed()
{
return minTime;
}

void CTEdit::StartShowStr()
{
GetCharWidthAndHeight();
SetTimer(1, minTime, NULL);
GetShowStr();
}

void CTEdit::SetTextColor1(unsigned long ntextColor)
{
textColor = ntextColor;
}

unsigned long CTEdit::GetTextColor1()
{
return textColor;
}

void CTEdit::OnTimer(UINT_PTR nIDEvent)
{
if (curTimes >= charHeight)
{
Flag = FALSE;
GetShowStr();
curTimes = 1;
rectTop = 0;
}
else
{
Flag = TRUE;
curTimes++;
}

UpdateWindow();
Invalidate();
}

void CTEdit::GetCharWidthAndHeight()
{
CDC* dc = GetDC();
std::string str("L");
SIZE size;
CRect rect;
dc->SelectObject(pFont);
if(::GetTextExtentPoint32((HDC)dc->GetSafeHdc(),str.c_str(),str.length(),&size))
{
charWidth = size.cx / str.length();
charHeight = size.cy;

GetClientRect(rect);
lineCharCount = rect.Width() / charWidth ;
lineCount = rect.Height() / size.cy;
}
}

void CTEdit::GetShowStr()
{
int temp_pos = 0, i = 0;
int real_LineCout = 0, real_ShowTimes = 0;
std::string	temp;
m_ShowStr = "";

real_ShowTimes = (allStr.length() + lineCharCount * lineCount) / (lineCount * lineCharCount);

temp_pos = strPosition;

for (i = 0; i < lineCount + 1; i++)
{
if (temp_pos  > allStr.length())
{
break;
}
else
{
temp = allStr.substr(temp_pos, lineCharCount);
}

temp += "/n";
m_ShowStr += temp;
temp_pos += lineCharCount;
}
strPosition += lineCharCount;
if (strPosition >= allStr.length())
{
strPosition = 0;
}
}

BOOL CTEdit::RegisterCalss()
{
//注册窗口
WNDCLASS wc;
memset(&wc, 0, sizeof(WNDCLASS));

wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = ::DefWindowProc;
wc.hInstance = AfxGetInstanceHandle();
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = (HBRUSH)(CreateSolidBrush(backColor));
wc.lpszMenuName = NULL;
wc.lpszClassName = _T("CTEdit");

if (!AfxRegisterClass(&wc))
{
TRACE("Class registration Failer");
return FALSE;
}
return TRUE;
}

void CTEdit::InitVariables(unsigned long nbackColor)
{
{
lineCount = 0;
lineCharCount = 0;
strPosition = 0;
minTime = 2000;
rectTop = 0;
curTimes = 1;
Flag = TRUE;
backColor = nbackColor;
textColor = RGB(0, 0, 0);
pFont = new CFont();
pFont->CreatePointFont(10, "Arial");
}

if (!RegisterCalss())
{
TRACE("Class registration Failer");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: