现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

分享一个类:滚动的CStatic

2014-01-11 22:24 工业·编程 ⁄ 共 2072字 ⁄ 字号 暂无评论

#pragma once

// CGoGoStatic

class CGoGoStatic : public CStatic
{
    DECLARE_DYNAMIC(CGoGoStatic)

public:
    CGoGoStatic();
    virtual ~CGoGoStatic();

    void GoGo();
    void Stop();
    void EndGoGo();

protected:
    DECLARE_MESSAGE_MAP()
    void CalcTextSize();
    CPoint m_ptStart;
    CSize m_szText;

public:
    afx_msg void OnTimer(UINT nIDEvent);
    afx_msg void OnPaint();
};
 
// GoGoStatic.cpp : 实现文件
//

#include "stdafx.h"
#include "DialogQuestion.h"
#include "./gogostatic.h"

#define ID_TIMER_GOGO 888888

// CGoGoStatic

IMPLEMENT_DYNAMIC(CGoGoStatic, CStatic)
CGoGoStatic::CGoGoStatic()
: m_ptStart(0)
, m_szText(0)
{
}

CGoGoStatic::~CGoGoStatic()
{
}

BEGIN_MESSAGE_MAP(CGoGoStatic, CStatic)
    ON_WM_TIMER()
    ON_WM_PAINT()
END_MESSAGE_MAP()

 

// CGoGoStatic 消息处理程序

void CGoGoStatic::OnTimer(UINT nIDEvent)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    if ( nIDEvent == ID_TIMER_GOGO )
    {
        m_ptStart.x -= 2;

        CRect rtClient;
        GetClientRect(&rtClient);
        rtClient.NormalizeRect();
        if ( m_ptStart.x+m_szText.cx < rtClient.left )
            m_ptStart.x = rtClient.right - 2;

        ShowWindow(SW_HIDE);
        ShowWindow(SW_NORMAL);
    }

    CStatic::OnTimer(nIDEvent);
}

void CGoGoStatic::OnPaint()
{
    CPaintDC dc(this); // device context for painting
    CString strText;
    GetWindowText(strText);
    CRect rtClient;
    GetClientRect(&rtClient);
    CRgn rgnClip;
    rgnClip.CreateRectRgn(rtClient.left, rtClient.top, rtClient.right, rtClient.bottom);
    dc.SelectClipRgn(&rgnClip);
    int nOldBkMode = dc.SetBkMode(TRANSPARENT);
    CGdiObject* pOldObj = dc.SelectStockObject(DEFAULT_GUI_FONT);
    dc.TextOut(m_ptStart.x, m_ptStart.y, strText);
    dc.SelectObject(pOldObj);
    dc.SetBkMode(nOldBkMode);
    dc.SelectClipRgn(NULL);
}

void CGoGoStatic::GoGo()
{
    KillTimer(ID_TIMER_GOGO);
    CalcTextSize();
    SetTimer(ID_TIMER_GOGO, 100, NULL);
}

void CGoGoStatic::Stop()
{
    KillTimer(ID_TIMER_GOGO);
}

void CGoGoStatic::EndGoGo()
{
    KillTimer(ID_TIMER_GOGO);
    m_ptStart = 0;
    ShowWindow(SW_HIDE);
    ShowWindow(SW_NORMAL);
}

void CGoGoStatic::CalcTextSize()
{
    CString strText;
    GetWindowText(strText);
    CDC* pDC = GetDC();
    pDC->SelectStockObject(DEFAULT_GUI_FONT);
    m_szText = pDC->GetTextExtent(strText);
}

给我留言

留言无头像?