继承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"); } }
相关文章推荐
- js 实现单行文本滚动效果
- 用PHP+java实现自动新闻滚动窗口
- Android:继承ScrollView实现自定义向上滚动弹出框(背景半透明)
- 双缓冲实现滚动文本
- JQuery实现的页面滚动时浮动窗口控件
- JavaScript实现竖直文本滚动
- 如何用WINX实现可滚动的窗口(ScrollWindow)
- TextView布局文件中实现文本过长时滚动显示效果
- 纯c++实现之滚动窗口
- 用JavaScript编写程序实现文本滚动
- Android开发中TextView文本过长滚动显示实现方法分析
- 纯c++实现之滚动窗口
- jquery实现导航栏跟随窗口滚动
- 如何用WINX实现可滚动的窗口(ScrollWindow)
- 滑轮滑动+小圆点触发全屏上下滚动并实现不同浏览器窗口下的缩放
- flex 实现文本滚动的第三种方法
- js扩展滚动窗口小插件实现文字左右上下滚动效果实例
- mfc 继承cwnd 实现类似tabControl的标签按钮
- C#实现带指定文本水印的截图(指定窗口与 全屏)
- 继承SurfaceView实现滚动字幕组件