2010年2月6日 星期六

CuWinAppEx



// CuWinAppEx.h

class CuWinAppEx
{
private:
    static CuWinAppEx *m_pWinApp;

protected:

    class CuMainWnd : public CuWnd
    {
        virtual LRESULT OnMsg(MSG &msg)
        {
            return CuWinAppEx::GetTheApp()->OnMsg(msg);
        };
    };

    CuMainWnd m_MainWnd;

public:
    CuWinAppEx(void);
    virtual ~CuWinAppEx(void);


public:
    static CuWinAppEx* GetTheApp(); 
    virtual LRESULT OnMsg(MSG & msg);
    virtual BOOL Run();

public:
    int OnDestroy(void);
};


// CuWinAppEx.cpp



CuWinAppEx *CuWinAppEx::m_pWinApp;

CuWinAppEx::CuWinAppEx(void)
{
    m_pWinApp = this;
}

CuWinAppEx::~CuWinAppEx(void)
{
}

CuWinAppEx* CuWinAppEx::GetTheApp()
{
    return m_pWinApp;
}

LRESULT CuWinAppEx::OnMsg(MSG & msg)
{

    switch(msg.message)
    {
    case WM_DESTROY:
        OnDestroy();
        break;

    default:
        return FALSE;
    }

    return TRUE;
}

int CuWinAppEx::OnDestroy(void)
{
     ::PostQuitMessage(0);

     return 0;
}


BOOL CuWinAppEx::Run()
{
    MSG msg;

    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0)) 
    {
        //      if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return msg.wParam;
}

雖然函式裡有virtual 但有時後並不代表這個類別是打算被繼承的, 有一半是這個類別繼承了別人, 但在解構式定為virtual就表示, 這個類別100%是可以被繼承的, 或是一定要被繼承!

沒有留言:

張貼留言