Click here to Skip to main content
15,887,214 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Clear text from a device context. Pin
CPallini12-Dec-07 5:12
mveCPallini12-Dec-07 5:12 
GeneralRe: Clear text from a device context. Pin
Mark Salsbery12-Dec-07 6:30
Mark Salsbery12-Dec-07 6:30 
GeneralRe: Clear text from a device context. Pin
Mr Simple12-Dec-07 7:12
Mr Simple12-Dec-07 7:12 
GeneralRe: Clear text from a device context. Pin
Mark Salsbery12-Dec-07 7:31
Mark Salsbery12-Dec-07 7:31 
GeneralRe: Clear text from a device context. Pin
Mr Simple12-Dec-07 22:43
Mr Simple12-Dec-07 22:43 
GeneralRe: Clear text from a device context. Pin
Mark Salsbery13-Dec-07 5:48
Mark Salsbery13-Dec-07 5:48 
GeneralRe: Clear text from a device context. Pin
Mr Simple13-Dec-07 6:33
Mr Simple13-Dec-07 6:33 
GeneralRe: Clear text from a device context. Pin
Mark Salsbery13-Dec-07 7:44
Mark Salsbery13-Dec-07 7:44 
Here's a working example, moving text with a timer across a bitmap background.

This is implemented on a simple dialog with just an ok button.  The same code would work in
a CWnd-derived class - In that case, WM_CREATE/OnCreate would be used instead of
OnInitDialog()...

#pragma once

#include <atlimage.h>

// CMovingTextDialog dialog

class CMovingTextDialog : public CDialog
{
    DECLARE_DYNAMIC(CMovingTextDialog)

public:
    CMovingTextDialog(CWnd* pParent = NULL);   // standard constructor
    virtual ~CMovingTextDialog();

// Dialog Data
    enum { IDD = IDD_DIALOG2 };  <font color="Red">//<-- Use the correct dialog resource ID here!</font>

protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

    DECLARE_MESSAGE_MAP()
public:
    virtual BOOL OnInitDialog();
protected:
    virtual void OnOK();
public:
    CImage BkImage;
    UINT_PTR MoveTimer;
    CString TextStr;
    CSize TextSize;
    CRect TextRect;

    afx_msg void OnPaint();
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);
    afx_msg void OnTimer(UINT_PTR nIDEvent);

};



// MovingTextDialog.cpp : implementation file
//

#include "stdafx.h"
#include "MFCTester.h"
#include "MovingTextDialog.h"


// CMovingTextDialog dialog

IMPLEMENT_DYNAMIC(CMovingTextDialog, CDialog)

CMovingTextDialog::CMovingTextDialog(CWnd* pParent /*=NULL*/)
    : CDialog(CMovingTextDialog::IDD, pParent)
{
    BkImage.Load(_T("e:\\test.bmp"));  <font color="Red">//<-- load your own bmp/jpg/tiff here!</font>
    MoveTimer = 0;
    TextStr = _T("Moving Text");
    TextSize.cx = TextSize.cy = 0;
    TextRect.SetRect(0,0,0,0);
}

CMovingTextDialog::~CMovingTextDialog()
{
}

void CMovingTextDialog::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CMovingTextDialog, CDialog)
    ON_WM_PAINT()
    ON_WM_ERASEBKGND()
    ON_WM_TIMER()
END_MESSAGE_MAP()


// CMovingTextDialog message handlers

BOOL CMovingTextDialog::OnInitDialog()
{
    CDialog::OnInitDialog();

    MoveTimer = ::SetTimer(*this, (UINT_PTR)12345, 10, NULL);

    return TRUE;  // return TRUE unless you set the focus to a control
    // EXCEPTION: OCX Property Pages should return FALSE
}

void CMovingTextDialog::OnOK()
{
    ::KillTimer(*this, MoveTimer);
    MoveTimer = 0;

    CDialog::OnOK();
}

void CMovingTextDialog::OnPaint()
{
    CPaintDC dc(this); 

    if (0 == TextSize.cx)
    {
        // Initialize TextSize and TextRect
        TextSize = dc.GetTextExtent(TextStr);
        TextRect.SetRect(0, 0, TextSize.cx, TextSize.cy);
        TextRect.InflateRect(4, 4);
    }

    dc.SetBkMode(TRANSPARENT);

    dc.DrawText(TextStr, &TextRect, DT_LEFT | DT_TOP);

}

BOOL CMovingTextDialog::OnEraseBkgnd(CDC* pDC)
{
    CRect ClientRect;
    GetClientRect(&ClientRect);

    BkImage.StretchBlt(*pDC, 0, 0, ClientRect.Width(), ClientRect.Height());

    return TRUE;//CDialog::OnEraseBkgnd(pDC);
}

void CMovingTextDialog::OnTimer(UINT_PTR nIDEvent)
{
    if (0 != TextSize.cx)
    {
        InvalidateRect(&TextRect);

        CRect ClientRect;
        GetClientRect(&ClientRect);

        TextRect.OffsetRect(2, 2);

        if (TextRect.right > ClientRect.right || TextRect.bottom > ClientRect.bottom)
        {
            TextRect.SetRect(0, 0, TextSize.cx, TextSize.cy);
            TextRect.InflateRect(4, 4);
        }

        UpdateWindow();
    }

    CDialog::OnTimer(nIDEvent);
}

What's different from what you are doing?

Mark



GeneralRe: Clear text from a device context. Pin
Mr Simple14-Dec-07 1:22
Mr Simple14-Dec-07 1:22 
GeneralRe: Clear text from a device context. Pin
Mark Salsbery14-Dec-07 4:47
Mark Salsbery14-Dec-07 4:47 
GeneralRe: Clear text from a device context. Pin
Mr Simple14-Dec-07 6:58
Mr Simple14-Dec-07 6:58 
GeneralRe: Clear text from a device context. Pin
Mark Salsbery14-Dec-07 8:52
Mark Salsbery14-Dec-07 8:52 
GeneralRe: Clear text from a device context. Pin
Mr Simple16-Dec-07 22:12
Mr Simple16-Dec-07 22:12 
GeneralRe: Clear text from a device context. Pin
Mark Salsbery17-Dec-07 5:50
Mark Salsbery17-Dec-07 5:50 
GeneralRe: Clear text from a device context. Pin
Mr Simple19-Dec-07 2:08
Mr Simple19-Dec-07 2:08 
GeneralRe: Clear text from a device context. Pin
Mark Salsbery19-Dec-07 6:14
Mark Salsbery19-Dec-07 6:14 
GeneralRe: Clear text from a device context. Pin
led mike12-Dec-07 7:47
led mike12-Dec-07 7:47 
GeneralRe: Clear text from a device context. Pin
Mark Salsbery12-Dec-07 7:58
Mark Salsbery12-Dec-07 7:58 
GeneralRe: Clear text from a device context. Pin
Nelek12-Dec-07 3:14
protectorNelek12-Dec-07 3:14 
QuestionUsing SendVirtualKey method? Pin
Benny_Lava12-Dec-07 1:03
Benny_Lava12-Dec-07 1:03 
GeneralMulti column combo box Pin
panthal12-Dec-07 0:33
panthal12-Dec-07 0:33 
QuestionRe: Multi column combo box Pin
Mark Salsbery12-Dec-07 6:33
Mark Salsbery12-Dec-07 6:33 
GeneralRe: Multi column combo box Pin
David Crow13-Dec-07 4:52
David Crow13-Dec-07 4:52 
GeneralRe: Multi column combo box Pin
Mark Salsbery13-Dec-07 5:49
Mark Salsbery13-Dec-07 5:49 
Questionhow the macro works Pin
George_George12-Dec-07 0:03
George_George12-Dec-07 0:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.