Click here to Skip to main content
15,891,473 members
Articles / Desktop Programming / WTL

WTL for MFC Programmers, Part II - WTL GUI Base Classes

Rate me:
Please Sign up or sign in to vote.
4.97/5 (158 votes)
22 Dec 2005CPOL18 min read 631.5K   6.1K   235  
WTL programming for MFC developers - frame windows.
// WTLClockView.h : interface of the CWTLClockView class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_WTLCLOCKVIEW_H__C2B91381_24DC_4CD3_9136_B49760C3D63D__INCLUDED_)
#define AFX_WTLCLOCKVIEW_H__C2B91381_24DC_4CD3_9136_B49760C3D63D__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

class CWTLClockView : public CWindowImpl<CWTLClockView>
{
public:
    CWTLClockView() : m_bClockRunning(true)
    { }

    DECLARE_WND_CLASS(NULL)

    BOOL PreTranslateMessage(MSG* pMsg);

    BEGIN_MSG_MAP(CWTLClockView)
        MESSAGE_HANDLER(WM_PAINT, OnPaint)
        MSG_WM_CREATE(OnCreate)
        MSG_WM_DESTROY(OnDestroy)
        MSG_WM_TIMER(OnTimer)
        MSG_WM_ERASEBKGND(OnEraseBkgnd)
    END_MSG_MAP()

    LRESULT OnCreate ( LPCREATESTRUCT lpcs )
    {
        GetLocalTime ( &m_stLastTime );
        SetTimer ( 1, 1000 );
        SetMsgHandled(false);
        return 0;
    }

    void OnDestroy()
    {
        KillTimer(1);
        SetMsgHandled(false);
    }

    void OnTimer ( UINT uTimerID, TIMERPROC pTimerProc )
    {
        if ( 1 != uTimerID )
            SetMsgHandled(false);
        else
            {
            // If the clock is running, get the current time & redraw our
            // window so the new time appears.
            if ( m_bClockRunning )
                {
                GetLocalTime ( &m_stLastTime );
                RedrawWindow();
                }
            }
    }

    LRESULT OnEraseBkgnd ( HDC hdc )
    {
        return 1;   // OnPaint will draw the whole window
    }

    LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);

    void StartClock() 
    { 
        m_bClockRunning = true; 
        GetLocalTime ( &m_stLastTime );
        RedrawWindow();
    }

    void StopClock() 
    { 
        m_bClockRunning = false; 
        RedrawWindow();
    }

protected:
    SYSTEMTIME m_stLastTime;
    bool       m_bClockRunning;
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_WTLCLOCKVIEW_H__C2B91381_24DC_4CD3_9136_B49760C3D63D__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) VMware
United States United States
Michael lives in sunny Mountain View, California. He started programming with an Apple //e in 4th grade, graduated from UCLA with a math degree in 1994, and immediately landed a job as a QA engineer at Symantec, working on the Norton AntiVirus team. He pretty much taught himself Windows and MFC programming, and in 1999 he designed and coded a new interface for Norton AntiVirus 2000.
Mike has been a a developer at Napster and at his own lil' startup, Zabersoft, a development company he co-founded with offices in Los Angeles and Odense, Denmark. Mike is now a senior engineer at VMware.

He also enjoys his hobbies of playing pinball, bike riding, photography, and Domion on Friday nights (current favorite combo: Village + double Pirate Ship). He would get his own snooker table too if they weren't so darn big! He is also sad that he's forgotten the languages he's studied: French, Mandarin Chinese, and Japanese.

Mike was a VC MVP from 2005 to 2009.

Comments and Discussions