Click here to Skip to main content
15,884,388 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 625.6K   6.1K   235  
WTL programming for MFC developers - frame windows.
#ifndef MYWINDOW_H_INCLUDED
#define MYWINDOW_H_INCLUDED

class CMyWindow : public CFrameWindowImpl<CMyWindow>
{
public:
    DECLARE_FRAME_WND_CLASS ( _T("First WTL window"), IDR_MAINFRAME );

    BEGIN_MSG_MAP_EX(CMyWindow)
        MSG_WM_CREATE(OnCreate)
        MSG_WM_DESTROY(OnDestroy)
        MSG_WM_ERASEBKGND(OnEraseBkgnd)
        MSG_WM_TIMER(OnTimer)
        COMMAND_ID_HANDLER_EX(IDC_EXIT, OnFileExit)
        COMMAND_ID_HANDLER_EX(IDC_ABOUT, OnAbout)
        CHAIN_MSG_MAP(CFrameWindowImpl<CMyWindow>)
    END_MSG_MAP()

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

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

    LRESULT OnEraseBkgnd ( HDC hdc )
    {
    CDCHandle  dc(hdc);
    CRect      rc;
    SYSTEMTIME st;
    CString    sTime;

        // Get our window's client area.
        GetClientRect ( rc );

        // Build the string to show in the window.
        GetLocalTime ( &st );
        sTime.Format ( _T("The time is %d:%02d:%02d"), st.wHour, st.wMinute, st.wSecond );

        // Set up the DC and draw the text.
        dc.SaveDC();
        
        dc.SetBkColor ( RGB(255,153,0) );
        dc.SetTextColor ( RGB(0,0,0) );
        dc.ExtTextOut ( 0, 0, ETO_OPAQUE, rc, sTime, sTime.GetLength(), NULL );

        // Restore the DC.
        dc.RestoreDC(-1);
        return 1;                       // We erased the background (ExtTextOut did it)
    }

    void OnTimer ( UINT uTimerID, TIMERPROC pTimerProc )
    {
        if ( 1 != uTimerID )
            SetMsgHandled(false);
        else
            RedrawWindow();
    }

    void OnFileExit(UINT uCode, int nID, HWND hwndCtrl)
    {
        DestroyWindow();
        return;
    }

    void OnAbout(UINT uCode, int nID, HWND hwndCtrl)
    {
    CSimpleDialog<IDD_ABOUT> dlg;

        dlg.DoModal();
        return;
    }
};

#endif  // ndef MYWINDOW_H_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