代码改变世界

控件保持多种绘图状态的做法

2013-11-21 15:03  Clingingboy  阅读(341)  评论(0编辑  收藏  举报

 

基本思路

#pragma once
#include <map>

struct IPainter
{
    void Paint() {}
};

class Element
{
    void SetPainterState(int dwState, IPainter* pPainter)
    {
        m_mapStatePainter[dwState]=pPainter;

        if(dwState==m_dwState)
        {
            GoToState(dwState);
        }
    }

    IPainter* GetPainterState(int dwState)
    {
        return m_mapStatePainter[m_dwState];
    }

    int GetState()
    {
        return m_dwState;
    }

    void GoToState(int dwState)
    {
        m_dwState=dwState;

        //InvalidateRect();
    }

    void Paint() 
    {
        m_mapStatePainter[m_dwState]->Paint();
    }

private:
    int        m_dwState;
    std::map<int,IPainter*>     m_mapStatePainter;
};