2010年2月8日 星期一

CuFontManager


// CuFontManager.h



class CuFontManager  
{
private:
    static  CuFontManager   m_pInstance; 
    map<int, HFONT>       m_mapFontHandle;

    CuFontManager();
public:
    virtual ~CuFontManager();

    static CuFontManager *Instance()
    {
        return &m_pInstance;
    }

    HFONT GetFontHandle(HFONT hFont,int iFontSize);
};


// CuFontManager.cpp



CuFontManager CuFontManager::m_pInstance;

CuFontManager::CuFontManager()
{

}

CuFontManager::~CuFontManager()
{
    map<int, HFONT>::iterator it;

    for(it = m_mapFontHandle.begin(); it != m_mapFontHandle.end(); it++) {
        HFONT hFontCreated = it->second;
        DeleteObject(hFontCreated);
    }

    m_mapFontHandle.clear();
}

HFONT CuFontManager::GetFontHandle(HFONT hFont,int iFontSize)
{
    HFONT hFontCreated;
    LOGFONT lf;
    map<int, HFONT>::const_iterator it;

    it = m_mapFontHandle.find(iFontSize);

    if(it == m_mapFontHandle.end()) {
        if (GetObject(hFont, sizeof LOGFONT, &lf)) {
            HDC hDC;

            hDC = ::GetDC(NULL);
            lf.lfHeight=-((iFontSize * GetDeviceCaps(hDC, LOGPIXELSY)) / 72);
            lf.lfWeight = FW_SEMIBOLD;

            hFontCreated = CreateFontIndirect(&lf);
            m_mapFontHandle.insert(map<int, HFONT>::value_type(iFontSize, hFontCreated));
            ReleaseDC(NULL, hDC);
        }
    }
    else
        hFontCreated = it->second;

    return hFontCreated;
}

2010年2月7日 星期日

CuLight

//


class CuLight  
{
    // CuLight 一實體後預存的值
    // 在解構時會將這個值寫回registey
    int m_nDefaultValue;

    LIGHT_INCVALUE  m_pfIncValue;
    LIGHT_DECVALUE  m_pfDecValue;
    LIGHT_GETVALUE  m_pfGetValue;
    LIGHT_SETVALUE  m_pfSetValue;

    LIGHT_GETMAXSTEP m_pfGetMaxStep;

    CuDllManager m_LightDll;


public:
    wstring strDllfile;
    

    CuLight();
    virtual ~CuLight(); 


    void Init();
    int OnGetValue(void);
    void OnSetValue(int n);

    void OnDecValue();
    void OnIncValue();
    void OnSave(){ m_nDefaultValue = OnGetValue(); }

    int OnGetMaxStep(){ return m_pfGetMaxStep(); }


};



//


CuLight::CuLight()
{

}

CuLight::~CuLight()
{
    OnSetValue(m_nDefaultValue);
}

void CuLight::Init()
{

    CuFile f;
    wstring strFile = f.GetModuleFileName(strDllfile.c_str());

    Verify(m_LightDll.LoadLibrary(strFile.c_str()) != NULL, L"can't Load Light_dll.dll Error");


    m_pfIncValue = (LIGHT_INCVALUE)m_LightDll.GetProcAddress(L"IncValue");
    Assert(m_pfIncValue != NULL, L"can't Load LIGHT_INCVALUE");

    m_pfDecValue = (LIGHT_DECVALUE)m_LightDll.GetProcAddress(L"DecValue");
    Assert(m_pfDecValue != NULL, L"can't Load LIGHT_DECVALUE");

    m_pfGetValue = (LIGHT_GETVALUE)m_LightDll.GetProcAddress(L"GetValue");
    Assert(m_pfGetValue != NULL, L"can't Load LIGHT_GETVALUE");

    m_pfSetValue = (LIGHT_SETVALUE)m_LightDll.GetProcAddress(L"SetValue");
    Assert(m_pfSetValue != NULL, L"can't Load LIGHT_SETVALUE");

    m_pfGetMaxStep = (LIGHT_GETMAXSTEP)m_LightDll.GetProcAddress(L"GetMaxStep");
    Assert(m_pfGetMaxStep != NULL, L"can't Load LIGHT_GETMAXSTEP");

    m_nDefaultValue = OnGetValue();

}

int CuLight::OnGetValue(void)
{ 
    int value;
    value = m_pfGetValue(); 
    return value;
}
void CuLight::OnSetValue(int n)
{ 
    m_pfSetValue(n); 
}

void CuLight::OnDecValue()
{
    m_pfDecValue(); 

    int value;
    value = m_pfGetValue(); 

}

void CuLight::OnIncValue()
{
    m_pfIncValue(); 
    int value;
    value = m_pfGetValue(); 

}
CE沒有標準, 所以定個介面元件對不同的機器實作獨一的DLL檔! 在設定檔裡指定要載入那一份DLL!

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%是可以被繼承的, 或是一定要被繼承!

2010年2月5日 星期五

IuImageEx

class IuImageEx : public IuUnknown
{
public:
    virtual BOOL    Create( int nWidth, int nHeight, int nBPP = 16) = 0;

    virtual BYTE*   Data(void) = 0; 

    virtual UINT    Height() = 0;
    virtual void    Height(UINT nHeight) = 0;

    virtual UINT    Width() = 0;
    virtual void    Width(UINT nWidth) = 0;

    virtual UINT    BitCount() = 0;
    virtual void    BitCount(UINT nBitCount) = 0;

    virtual COLORREF GetPixel( int x, int y) = 0;
};
圖型編碼太多種, 但實際使用時, 也只有用到部份函式, 整個拉出介面時, 才發現用到的不多!

2010年2月4日 星期四

IuImageWnd

class IuImageWnd : public IuImageEx
{
public:
    virtual HDC GetDC() = 0;
    virtual void ReleaseDC() = 0;

    virtual BOOL BitBlt( HDC hDestDC, int xDest, int yDest, int nDestWidth, 
        int nDestHeight, int xSrc, int ySrc, DWORD dwROP = SRCCOPY ) = 0;

    virtual BOOL StretchBlt( HDC hDestDC, int xDest, int yDest, 
        int nDestWidth, int nDestHeight, int xSrc, int ySrc, int nSrcWidth, 
        int nSrcHeight, DWORD dwROP = SRCCOPY ) = 0;


    virtual operator HBITMAP() const = 0;

};
不管學院派對介面的定義為何! 在這裡介面獨立最好的地方是, 看起來就是輕巧, 使用起來壓力會小很多, 不必為了看到源碼或是太多功能搞的自己頭大!

2010年2月3日 星期三

CuImageEx

// CuImageEx.h



class CuImageEx : public IuImageEx 
{
    //圖高
    UINT    _nHeight;

    //圖寬
    UINT    _nWidth;

    // 圖的色素  :  16  
    UINT    _nBitCount;

    // 存放 圖形點陣列
    BYTE    *_bits;


public:
    CuImageEx();
    virtual ~CuImageEx();

    //////////////////////////////////////////////////////////////////
    // IuUnknown 公開虛擬界面 
    virtual ULONG Release();

    //////////////////////////////////////////////////////////////////
    // IuImage 公開虛擬界面 
    virtual BOOL Create(int nWidth, int nHeight, int nBPP = 16);

    virtual UINT    Height(){ return _nHeight; }
    virtual void    Height(UINT nHeight){ _nHeight = nHeight; }

    virtual UINT    Width(){ return _nWidth; }
    virtual void    Width(UINT nWidth){ _nWidth = nWidth; }

    virtual UINT    BitCount(){ return _nBitCount; }
    virtual void    BitCount(UINT nBitCount){ _nBitCount = nBitCount; }

    virtual COLORREF GetPixel( int x, int y);
    virtual BYTE*   Data(void){ return _bits; }


    //////////////////////////////////////////////////////////////////
    // 未公開虛擬界面 的 public 函式

    void Data(BYTE *pDate){ _bits = pDate; }

    void Destroy();

private:
    CuImageEx& operator =(CuImageEx& img);

};

 
// CuImageEx.cpp


CuImageEx::CuImageEx()
:_nHeight(0)
,_nWidth(0)
,_nBitCount(0)
,_bits(NULL)
{
}

CuImageEx::~CuImageEx()
{
    Destroy();
}

ULONG CuImageEx::Release()
{
    delete this;
    return 0;
}

BOOL CuImageEx::Create(int nWidth, int nHeight, int nBPP)
{

    Destroy();

    Height(nHeight);
    Width(nWidth);
    BitCount(nBPP);

    _bits = new BYTE[nWidth * nHeight * (nBPP >> 3)];

    return (_bits != NULL);

}

void CuImageEx::Destroy()
{

    Height(0);
    Width(0);
    BitCount(0);

    delete [] _bits;
    _bits = NULL;

}

COLORREF CuImageEx::GetPixel( int x, int y)
{

    WORD *pBits = (WORD*)this->Data();

    if( !pBits )
        return RGB(0, 0, 0);

    int nW = Width() ;

    // 
    if( nW % 2 )
        nW++;

    WORD wPixel = pBits[y * nW + x];

    return RGB( (wPixel >> 10 ) & 0x1f,
        (wPixel >> 5) & 0x1f,
        wPixel & 0x1f);
}

CuImageEx& CuImageEx::operator =(CuImageEx& img)
{
    
    if( this == &img )
        return *this;
    
    this->Create(img.Width() , img.Height(), img.BitCount() );

    memcpy(_bits, img._bits, _nWidth  * _nHeight * ( _nBitCount >> 3 ) );
        
    return *this;

}
與CuImageWnd 相異的地方在於, 若已經有一份點陣DC時, 其實其他的圖型運算是可以使用自製效能優化過的元件! 在這裡縮小圖結構方便替換運算元件!

2010年2月2日 星期二

CuImageWnd

// CuImageWnd.h



class CuImageWnd : public IuImageWnd
{

    // Image 的控制代碼
    HBITMAP m_hBitmap;

    // GetDC 時 暫存用的控制代碼
    HBITMAP m_hOldBitmap;

    // GetDC 時 暫存的DC控制代碼
    HDC m_hDC;

    // DC使用參考計數
    UINT m_nDCRefCount;

    CuImageEx m_ImageEx;
public:

    CuImageWnd();
    virtual ~CuImageWnd();

    //////////////////////////////////////////////////////////////////
    // IuUnknown 公開虛擬界面 
    virtual ULONG Release();

    //////////////////////////////////////////////////////////////////
    // IuImage 公開虛擬界面 
    virtual BOOL Create( int nWidth, int nHeight, int nBPP);

    virtual UINT    Height(){ return m_ImageEx.Height(); }
    virtual void    Height(UINT nHeight){ m_ImageEx.Height(nHeight); }

    virtual UINT    Width(){ return m_ImageEx.Width(); }
    virtual void    Width(UINT nWidth){ m_ImageEx.Width(nWidth); }

    virtual UINT    BitCount(){ return m_ImageEx.BitCount(); }
    virtual void    BitCount(UINT nBitCount){ m_ImageEx.BitCount(nBitCount); }

    virtual COLORREF GetPixel( int x, int y){ return m_ImageEx.GetPixel(x,y); }
    virtual BYTE*   Data(void){ return m_ImageEx.Data(); }

    //////////////////////////////////////////////////////////////////
    // IuImageWnd 公開虛擬界面 

        // window 支援函式


    virtual HDC GetDC();
    virtual void ReleaseDC();

    virtual BOOL BitBlt( HDC hDestDC, int xDest, int yDest, int nDestWidth, 
        int nDestHeight, int xSrc, int ySrc, DWORD dwROP = SRCCOPY );

    virtual BOOL StretchBlt( HDC hDestDC, int xDest, int yDest, 
        int nDestWidth, int nDestHeight, int xSrc, int ySrc, int nSrcWidth, 
        int nSrcHeight, DWORD dwROP = SRCCOPY );

    virtual operator HBITMAP() const;

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


    //////////////////////////////////////////////////////////////////
    // 未公開虛擬界面 的 public 函式
    void Destroy();

};



// CuImageWnd.cpp


CuImageWnd::CuImageWnd()
:m_hBitmap(NULL)
,m_hOldBitmap(NULL)
,m_hDC(NULL)
,m_nDCRefCount(0)
{
}


CuImageWnd::~CuImageWnd()
{

    Destroy();
}

ULONG CuImageWnd::Release()
{
    delete this;
    return 0;
}

BOOL CuImageWnd::Create( int nWidth, int nHeight, int nBPP)
{

    Destroy();

    BITMAPINFO  dibInfo;
    dibInfo.bmiHeader.biBitCount = nBPP;
    dibInfo.bmiHeader.biClrImportant = 0;
    dibInfo.bmiHeader.biClrUsed = 0;
    dibInfo.bmiHeader.biCompression = BI_RGB;
    dibInfo.bmiHeader.biHeight = nHeight;
    dibInfo.bmiHeader.biPlanes = 1;
    dibInfo.bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
    dibInfo.bmiHeader.biSizeImage = nWidth * nHeight * (nBPP >> 3 );
    dibInfo.bmiHeader.biWidth = nWidth;
    dibInfo.bmiHeader.biXPelsPerMeter = 0;
    dibInfo.bmiHeader.biYPelsPerMeter = 0;
    dibInfo.bmiColors[0].rgbBlue = 0;
    dibInfo.bmiColors[0].rgbGreen = 0;
    dibInfo.bmiColors[0].rgbRed = 0;
    dibInfo.bmiColors[0].rgbReserved = 0;

    Height(nHeight);
    Width(nWidth);
    BitCount(nBPP);

    void* ppvBits ;//= Data();

    m_hBitmap = ::CreateDIBSection(NULL, &dibInfo, DIB_RGB_COLORS,
        (void**)&ppvBits, NULL, 0);

    m_ImageEx.Data((BYTE*)ppvBits);
    
    return (BOOL)m_hBitmap;

}

void CuImageWnd::Destroy()
{
    if( m_hBitmap )
    {
        ::DeleteObject(m_hBitmap);
        m_hBitmap = NULL;
    }

    m_ImageEx.Data(0);

    Height(0);
    Width(0);
    BitCount(0);

}

HDC CuImageWnd::GetDC() 
{

    Assert(m_hBitmap != NULL, L" CuImage::GetDC() m_hBitmap != NULL");
    
    m_nDCRefCount++;
    if( m_hDC == NULL )
    {
        m_hDC = ::CreateCompatibleDC(NULL);
        m_hOldBitmap = HBITMAP(::SelectObject( m_hDC, m_hBitmap ));
    }

    return m_hDC;
}

void CuImageWnd::ReleaseDC() 
{
    HBITMAP hBitmap;

    Assert( m_hDC != NULL, L"");

    m_nDCRefCount--;
    if( m_nDCRefCount == 0 )
    {
        hBitmap = HBITMAP( ::SelectObject( m_hDC, m_hOldBitmap ) );
        Assert( hBitmap == m_hBitmap ,L"");
        ::DeleteDC( m_hDC );
        m_hDC = NULL;
    }
}


BOOL CuImageWnd::StretchBlt( HDC hDestDC, int xDest, int yDest, 
    int nDestWidth, int nDestHeight, int xSrc, int ySrc, int nSrcWidth, 
    int nSrcHeight, DWORD dwROP ) 
{

    BOOL bResult;

    Assert( m_hBitmap != NULL, L"" );
    Assert( hDestDC != NULL, L"" );

    HDC hDC = this->GetDC();

    bResult = ::StretchBlt( hDestDC, xDest, yDest, nDestWidth, nDestHeight, hDC,
        xSrc, ySrc, nSrcWidth, nSrcHeight, dwROP );

    this->ReleaseDC();

    return( bResult );

}


BOOL CuImageWnd::BitBlt( HDC hDestDC, int xDest, int yDest, int nDestWidth, 
    int nDestHeight, int xSrc, int ySrc, DWORD dwROP )
{
    BOOL bResult;
 
    Assert( m_hBitmap != NULL, L"");
    Assert( hDestDC != NULL, L"");

    GetDC();

    bResult = ::BitBlt( hDestDC, xDest, yDest, nDestWidth, nDestHeight, m_hDC, 
        xSrc, ySrc, dwROP );

    ReleaseDC();

    return( bResult );
}

CuImageWnd::operator HBITMAP() const
    { return m_hBitmap; }

支援Windows 專用的部份函式型別轉換: HDC HBITMAP 當然還有最重要的 點陣資料集