做一个对话框,怎样实现某一个位置文字的水平滚动,要看起来连续滚动,不要一个字一个字的滚动,最好用Edit控件,当然其它控件如果有好办法也行

解决方案 »

  1.   

    class CShowScrollText : public CMyHyperLink
    {
    // Construction
    public:
    CShowScrollText();// Attributes
    public:// Operations
    public:
    CFont m_font;
    // Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CShowScrollText)

    protected:
    virtual void PreSubclassWindow();
    //}}AFX_VIRTUAL// Implementation
    public:
    virtual ~CShowScrollText();
    void Clear();public: // Generated message map functions
    protected:
    //{{AFX_MSG(CShowScrollText)
    afx_msg void OnPaint();
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnTimer(UINT nIDEvent); afx_msg BOOL OnEraseBkgnd(CDC* pDC);
    //}}AFX_MSG
    INT m_iExtend;
    INT m_iCount;
    DECLARE_MESSAGE_MAP()protected:};
    // ShowScrollText.cpp : implementation file
    //#include "stdafx.h"
    #include "ShowScrollText.h"
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CShowScrollTextCShowScrollText::CShowScrollText()
    {
    m_iExtend = 0;
    m_iCount = 0;
    LOGFONT logfont;
    ZeroMemory(&logfont, sizeof(LOGFONT));
    logfont.lfCharSet = GB2312_CHARSET;
    logfont.lfHeight = -12;
    _tcscpy(logfont.lfFaceName, _T("宋体")); m_font.CreateFontIndirect(&logfont);
    }CShowScrollText::~CShowScrollText()
    {
    m_font.DeleteObject();
    }
    BEGIN_MESSAGE_MAP(CShowScrollText, CStatic)
    //{{AFX_MSG_MAP(CShowScrollText)
    ON_WM_ERASEBKGND()
    ON_WM_PAINT()
    ON_WM_CREATE()
    ON_WM_TIMER()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CShowScrollText message handlersvoid CShowScrollText::OnPaint() 
    {
    // TODO: Add your message handler code here

    CPaintDC dc(this);
    CRect rc;
    GetClientRect(rc);
    CString strText;
    GetWindowText(strText);
    CDC memDC;
    memDC.CreateCompatibleDC(&dc);
    CBitmap bmp;
    bmp.CreateCompatibleBitmap(&dc, rc.Width(), rc.Height());
    memDC.SelectObject(&bmp);
    memDC.SelectObject(&m_font);
    dc.SelectObject(&m_font);

    CSize size = memDC.GetTextExtent(strText);
    if (size.cx > rc.Width())
    {
    if (size.cx - m_iExtend > 0)
    memDC.TextOut(rc.left - m_iExtend, rc.top + (rc.Height() - size.cy)/2, strText);
    if (rc.left - m_iExtend + 8 +size.cx < rc.right)
    memDC.TextOut(rc.left - m_iExtend + 8+size.cx , rc.top + (rc.Height() - size.cy)/2, strText);
    }
    else
    memDC.TextOut(rc.left, rc.top + (rc.Height() - size.cy)/2, strText);
    dc.BitBlt(rc.left, rc.top, rc.Width(), rc.Height(), &memDC, rc.left, rc.top, SRCCOPY);
    memDC.DeleteDC();
    bmp.DeleteObject();
    }int CShowScrollText::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
    if (CStatic::OnCreate(lpCreateStruct) == -1)
    return -1;

    // TODO: Add your specialized creation code here
    SetTimer(1, 100, NULL);
    return 0;
    }
    void CShowScrollText::PreSubclassWindow() 
    {
    SetFont(&m_font);
    SetTimer(1, 100, NULL);
    CStatic::PreSubclassWindow();
    }void CShowScrollText::OnTimer(UINT nIDEvent) 
    {
    // TODO: Add your message handler code here and/or call default
    CString strText;
    GetWindowText(strText);
    if (strText == m_strText)
    {
    CDC* pDC = GetDC();
    pDC->SelectObject(&m_font);
    CSize size = pDC->GetTextExtent(strText);
    ReleaseDC(pDC);
    CRect rect;
    GetClientRect(rect);
    int iWidth = rect.Width();
    if (size.cx > iWidth)
    { Invalidate();
    UpdateWindow();
    m_iExtend += 2;
    if (m_iExtend > size.cx)
    m_iExtend -= size.cx + 8;
    }
    }
    else
    {
    m_iExtend = 0;
    m_strText = strText;
    }
    CStatic::OnTimer(nIDEvent);
    }BOOL CShowScrollText::OnEraseBkgnd(CDC* pDC) 
    {
    // TODO: Add your message handler code here and/or call default
    UNUSED_ALWAYS(pDC);
    return FALSE;
    }