ETNoticeWnd.cpp 3.09 KB
// ETNoticeWnd.cpp : 实现文件
//

#include "stdafx.h"
#include "ETNoticeWnd.h"
#include "afxdialogex.h"
#include "resource.h"

const int SCROLLSPEED = -2;

// CETNoticeWnd 对话框

IMPLEMENT_DYNAMIC(CETNoticeWnd, CDialogEx)

CETNoticeWnd::CETNoticeWnd(CWnd* pParent /*=NULL*/)
	: CDialogEx(CETNoticeWnd::IDD, pParent),
	m_iTimerID(0), m_iCurPos(0),
	m_iDelayTime(0), m_iIntervalTime(0),
	m_sText(_T(""))
{

}

CETNoticeWnd::~CETNoticeWnd()
{
}

void CETNoticeWnd::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	//DDX_Control(pDX, IDC_EDIT_NOTICE, m_editNotice);
}


BEGIN_MESSAGE_MAP(CETNoticeWnd, CDialogEx)
	ON_WM_TIMER()
	ON_WM_HSCROLL()
	ON_WM_PAINT()
	ON_WM_SIZE()
	ON_WM_CTLCOLOR()
END_MESSAGE_MAP()


// CETNoticeWnd 消息处理程序

void CETNoticeWnd::SetWindowText(CString sText)
{
	m_editNotice.SetWindowText(sText);
	if (sText.IsEmpty())
	{
		CloseNoticeWnd();
		ShowWindow(SW_HIDE);
	}
	else
	{
		ShowWindow(SW_SHOW);
		m_sText = sText;
		ReSizeEdit();
		if (m_iTimerID == 0)
		{
			m_iTimerID = 1;
			SetTimer(m_iTimerID, 100, NULL);
		}
	}
}

void CETNoticeWnd::CloseNoticeWnd()
{
	m_editNotice.SetWindowText(_T(""));
	ReSetState();
	if (m_iTimerID != 0)
	{
		KillTimer(m_iTimerID);
	}
}

void CETNoticeWnd::ReSetState()
{
	m_iCurPos = 0;
	m_iDelayTime = 0;
}

BOOL CETNoticeWnd::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	SetWindowPos(&wndTop, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
	m_editNotice.SetWindowPos(&wndTop, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
	ReSizeEdit();

	m_fontText.CreatePointFont(120, _T("微软雅黑"));
	m_editNotice.SetTextFont(&m_fontText);

	m_iIntervalTime = 100; //设置计时器间隔时间,单位毫秒

	m_editNotice.EnableWindow(FALSE);

	m_brushBKG.CreateSolidBrush(RGB(255, 0, 0));

	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常:  OCX 属性页应返回 FALSE
}

void CETNoticeWnd::ReSizeEdit()
{
	ReSetState();
	
	CPaintDC dc(this);
	dc.SelectObject(m_fontText);

	CSize szText = dc.GetTextExtent(m_sText);
	CRect rtClient;
	GetClientRect(&rtClient);
	m_editNotice.MoveWindow(rtClient.left, rtClient.Height() / 2 - szText.cy / 2, szText.cx, szText.cy);
}

void CETNoticeWnd::OnTimer(UINT_PTR nIDEvent)
{
	if (m_iDelayTime < 3000)
	{
		m_iDelayTime += m_iIntervalTime;
		return;
	}
	CRect rtEdit;
	m_editNotice.GetWindowRect(&rtEdit);
	ScreenToClient(&rtEdit);
	if (m_iCurPos < (0 - rtEdit.Width()))
	{
		CRect rtClient;
		GetClientRect(&rtClient);
		int iMoveDistance = rtClient.Width() + rtEdit.Width();
		ScrollWindow(iMoveDistance, 0);
		m_iCurPos += iMoveDistance;
	}
	else
	{
		ScrollWindow(SCROLLSPEED, 0);
		m_iCurPos += SCROLLSPEED;
	}

	CDialogEx::OnTimer(nIDEvent);
}


void CETNoticeWnd::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	
	//CRect rtClient;
	//GetClientRect(&rtClient);
	//dc.FillSolidRect(rtClient, RGB(255, 0, 0));     //设置为红色背景 

	CDialogEx::OnPaint();
}


void CETNoticeWnd::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);

	UpdateWindow();
}






HBRUSH CETNoticeWnd::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);

	return m_brushBKG;
}