字幕滚动

头文件

 

#ifndef _MSG_SCROLLER_H_
#define _MSG_SCROLLER_H_


class CMsgScroller : public CView
{
public:
	CMsgScroller(void);
	DECLARE_DYNCREATE(CMsgScroller)

	virtual ~CMsgScroller(void);

	virtual BOOL Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID);
	afx_msg int OnCreate( LPCREATESTRUCT lpCreateStruct );

	void OnFontSet();

	void SetScrollText(CString& ScrollText);

protected:
	DECLARE_MESSAGE_MAP()

	afx_msg void OnPaint();
	afx_msg void OnSize(UINT nType,int cx,int cy);
	afx_msg void OnTimer(UINT nIDEvent);
	afx_msg void OnDestroy();

	virtual void OnDraw(CDC* pDC);

	CString m_ScrollText;
	

	void CalculateLines();

	void DrawText();

	std::vector<CString> m_lsShowMessage;

	CFont m_font;
	LOGFONT lf;

	int m_StringCurrentPos;
	int m_iYLocation;
	int m_iShowCount;
	CBrush m_brush;
	int m_SystemMetricsCX;

	bool m_NeedReCalculate;

	COLORREF m_clrNotifyBack;
	COLORREF m_clrNotifyText;
public:
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
};

#endif

 

cpp文件

 

 

#include "StdAfx.h"
#include "MsgScroller.h"
#include "SizeDlg.h"

IMPLEMENT_DYNCREATE(CMsgScroller, CView)


BEGIN_MESSAGE_MAP(CMsgScroller, CView)
	ON_WM_CREATE()
	ON_WM_PAINT()
	ON_WM_TIMER()
	ON_WM_SIZE()
	ON_WM_ERASEBKGND()
END_MESSAGE_MAP()

CMsgScroller::CMsgScroller(void)
{
	m_StringCurrentPos = 3;//左侧预留3
	m_iYLocation = 0;
	m_iShowCount = 0;
}

CMsgScroller::~CMsgScroller(void)
{
}

BOOL CMsgScroller::Create( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID )
{
	return CView::Create(NULL, NULL, dwStyle, rect, pParentWnd, nID);
}

void CMsgScroller::OnSize( UINT nType,int cx,int cy )
{
	m_NeedReCalculate = true;
	CView::OnSize(nType, cx, cy);
}

void CMsgScroller::OnPaint()
{
	//CRect rect;
	//CPaintDC dc(this);
	//GetClientRect(rect);
	//dc.FillSolidRect(rect, m_clrNotifyBack);
	return CView::OnPaint();
}

int CMsgScroller::OnCreate( LPCREATESTRUCT lpCreateStruct )
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;

	//m_iYLocation = GetSystemMetrics(ARW_BOTTOMLEFT);
	RECT rect;
	GetClientRect(&rect);
	m_iYLocation = rect.bottom;


	//读取字体配置
	unsigned int nBytes = 0ul;
	LOGFONT* pFont = 0;
	AfxGetApp()->GetProfileBinary(_T("FullScreen"),_T("notify_font"),(LPBYTE*)&pFont,&nBytes);
	if(nBytes == 0)
	{
		HFONT hFont = CreateFont(24,0,0,0,FW_NORMAL,FALSE,FALSE,FALSE,GB2312_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH,_T("宋体"));
		GetObject(hFont,sizeof(LOGFONT),&lf);
		DeleteObject(hFont);
	}
	else
	{
		lf = *pFont;
		delete pFont;
	}

	//读取背景色配置
	m_clrNotifyBack = AfxGetApp()->GetProfileInt(_T("FullScreen"),_T("notify_back"),RGB(249,249,249));
	//读取字体色配置
	m_clrNotifyText = AfxGetApp()->GetProfileInt(_T("FullScreen"),_T("notify_text"),RGB(0,0,0));

	// TODO: Add your specialized creation code here
	SetTimer(1, 50, 0); // 在这里设置计时器

	return 0;
}

void CMsgScroller::OnDraw( CDC* pDC )
{
	return CView::OnDraw(pDC);
}

void CMsgScroller::OnTimer( UINT nIDEvent )
{
	DrawText();
	CView::OnTimer(nIDEvent);
}

void CMsgScroller::DrawText()
{
	if(m_NeedReCalculate)
	{
		CalculateLines();
		m_NeedReCalculate = false;
	}

	if(m_lsShowMessage.size() == 0)
	{
		Invalidate();
		return;
	}

	m_iYLocation--;

	CRect rect;
	CDC* pDC;
	CDC memDC;

	GetClientRect(&rect);
	pDC = GetDC();
	CBitmap memBitmap;

	int height = 0;
	if(lf.lfHeight < 0)
	{
		height = -lf.lfHeight;
	}
	else
	{
		height = lf.lfHeight;
	}
	height += height/5;
	if(m_iYLocation < -(height *(int)m_lsShowMessage.size()))
	{
		m_iYLocation = rect.bottom;
	}

	memDC.CreateCompatibleDC(NULL);
	memBitmap.CreateCompatibleBitmap(pDC, rect.right, rect.bottom);
	memDC.SelectObject(&memBitmap);

	memDC.FillSolidRect(&rect, m_clrNotifyBack);
	CFont font;
	font.CreateFontIndirect(&lf);
	memDC.SelectObject(&font);
	memDC.SetTextColor(m_clrNotifyText);
	//memDC.SetBkMode(TRANSPARENT);

	memDC.Rectangle(&rect);
	memDC.FillRect(&rect, &m_brush);

	int i = 0;
	for(; i<m_lsShowMessage.size(); i++)
	{
		memDC.TextOut(m_StringCurrentPos, m_iYLocation + height * i, m_lsShowMessage[i]);
	}

	pDC->BitBlt(rect.left, rect.top, rect.right,rect.bottom, &memDC, 0, 0, SRCCOPY);
	this->ReleaseDC(pDC);
	memDC.DeleteDC();
	memBitmap.DeleteObject();
}

void CMsgScroller::SetScrollText( CString& ScrollText )
{
	m_ScrollText = ScrollText;
	
	m_NeedReCalculate = true;
}

void CMsgScroller::OnFontSet()
{
	CSizeDlg dlg;
	dlg.SetFont(lf);
	dlg.SetBackColor(m_clrNotifyBack);
	dlg.SetFontColor(m_clrNotifyText);
	if(dlg.DoModal() == IDOK)
	{
		m_clrNotifyBack = dlg.GetBackColor();
		m_clrNotifyText = dlg.GetFontColor();
		lf = dlg.GetFont();

		AfxGetApp()->WriteProfileBinary(_T("FullScreen"),_T("notify_font"),(LPBYTE)&lf,sizeof(LOGFONT));
		//读取背景色配置
		AfxGetApp()->WriteProfileInt(_T("FullScreen"),_T("notify_back"),m_clrNotifyBack);
		//读取字体色配置
		AfxGetApp()->WriteProfileInt(_T("FullScreen"),_T("notify_text"),m_clrNotifyText);

		m_NeedReCalculate = true;
	}
}


void CMsgScroller::CalculateLines()
{
	m_lsShowMessage.clear();

	CClientDC dc(this);
	CFont font;
	font.CreateFontIndirect(&lf);
	CFont* def_font = dc.SelectObject(&font);
	RECT rect;
	GetClientRect(&rect);

	TCHAR Word[8] = {0};
	CString LineWords;
	int i,j;
	for(i=0,j=0; i<m_ScrollText.GetAllocLength(); i++,j++)
	{
		memset(Word, 0, 8);
		if(m_ScrollText[i] < 0)
		{
			Word[0] = m_ScrollText[i++];
			Word[1] = m_ScrollText[i];
		}
		else
		{
			Word[0] = m_ScrollText[i];
		}
		CString LastWords = LineWords;
		LineWords += Word;
		CSize sz = dc.GetTextExtent(LineWords);
		if(sz.cx+6 > rect.right)
		{
			LineWords = Word;
			m_lsShowMessage.push_back(LastWords);
		}
	}
	m_lsShowMessage.push_back(LineWords);
}


BOOL CMsgScroller::OnEraseBkgnd(CDC* pDC)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	return CView::OnEraseBkgnd(pDC);

}

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值