2010年1月12日 星期二

CuWnd - is distinct from a Windows window

// is distinct from a Windows window

//
class CuMainWnd : public CuWnd {};

//
CuWnd ListCtrlWnd;
ListCtrlWnd.Create(WC_LISTVIEW , TEXT(""), dwStyle, rect, hParentWnd, nID);

CuWnd EditWnd;
EditWnd.CreateEx(WS_EX_CLIENTEDGE, TEXT("EDIT"), TEXT(""), dwStyle, rect, hParentWnd, nID);
// CuWnd.h

class CuWnd
{
private:
    HWND m_hWnd;

private:

    //////////////////////////////////////////////////////////////////////
    // 函式名稱: WndProc
    // 函式說明: Create時預設的訊息回圈 會將訊息派送到 OnMsg裡
    //////////////////////////////////////////////////////////////////////
    static LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);

public:
    // 建構 解構
    CuWnd(void);
    virtual ~CuWnd(void);

protected:

    //////////////////////////////////////////////////////////////////////
    // 函式名稱: OnMsg
    // 函式說明: 繼承這個函式處理windows訊息
    //////////////////////////////////////////////////////////////////////
    virtual LRESULT OnMsg(MSG &msg);

public:

    virtual BOOL Create( LPCTSTR lpszClassName, LPCTSTR lpszWindowName,
        DWORD dwStyle,  const CuRect &rect, 
        HWND hParentWnd = NULL, UINT nID = NULL );

    virtual BOOL CreateEx( DWORD dwExStyle, LPCTSTR lpszClassName,
        LPCTSTR lpszWindowName, DWORD dwStyle, const CuRect& rect,
        HWND hParentWnd = NULL, UINT nID = NULL);

    ATOM WndRegisterClassEx(LPCTSTR szWindowClass);


    // inline 函式 實作碼在下方
    HWND GetSafeHwnd( ) const;
    BOOL ShowWindow(int nCmdShow);
    BOOL UpdateWindow();
    BOOL DestroyWindow();

    operator HWND() const
    {
        return GetSafeHwnd();
    }

public:
    // 訊息處理函式
    void OnDestroy();

};

inline HWND CuWnd::GetSafeHwnd() const
{ return (HWND)(this == NULL ? NULL : m_hWnd) ;}

inline BOOL CuWnd::ShowWindow(int nCmdShow)
{ return ::ShowWindow( m_hWnd, nCmdShow); }

inline BOOL CuWnd::UpdateWindow()
{ return ::UpdateWindow(m_hWnd); }

inline BOOL CuWnd::DestroyWindow()
{ 
 BOOL bResult = ::DestroyWindow(m_hWnd); 
 m_hWnd = NULL;
 return bResult;
}



// CuWnd.cpp


CuWnd::CuWnd(void)
:m_hWnd(NULL)
{

}

CuWnd::~CuWnd(void)
{
    this->DestroyWindow();
}


//////////////////////////////////////////////////////////////////////
// 函式名稱: OnMsg
// 函式說明: 預設訊息處理
//////////////////////////////////////////////////////////////////////
LRESULT CuWnd::OnMsg(MSG &msg)
{
    return 0;
}

void CuWnd::OnDestroy()
{
    PostQuitMessage(0);
}

LRESULT CALLBACK CuWnd::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static MSG msg;

    msg.hwnd = hWnd;
    msg.message = message;
    msg.wParam = wParam;
    msg.lParam = lParam;

    static CuWnd *pWnd = NULL;
    if( message == WM_CREATE )
    {
    
        CREATESTRUCT *pct = (LPCREATESTRUCT)lParam;
        ::SetWindowLong( hWnd , GWL_USERDATA, (LONG)pct->lpCreateParams );
        
    }


    pWnd = (CuWnd *)::GetWindowLong(hWnd, GWL_USERDATA);
    if( pWnd && pWnd->OnMsg(msg) )
        return 0;
    else
        return ::DefWindowProc(hWnd, message, wParam, lParam);
    


}

BOOL CuWnd::Create( LPCTSTR lpszClassName, LPCTSTR lpszWindowName,
                    DWORD dwStyle, const CuRect &rect,
                    HWND hParentWnd /* = NULL */, UINT nID /* = NULL*/ )
{


    m_hWnd = ::CreateWindow(lpszClassName, lpszWindowName, dwStyle,
        rect.left, rect.top, rect.Width(), rect.Height(), 
        hParentWnd, (HMENU)nID, GetModuleHandle(NULL), (LPVOID) this);

    Assert( m_hWnd != NULL, L"CreateWindow Error");
    DWORD dwError = GetLastError();

    return (BOOL) (m_hWnd == NULL ? NULL : m_hWnd);

}

BOOL CuWnd::CreateEx( DWORD dwExStyle, LPCTSTR lpszClassName,
                      LPCTSTR lpszWindowName, DWORD dwStyle, const CuRect& rect,
                      HWND hParentWnd  /* = NULL */, UINT nID /* = NULL*/)
{

    m_hWnd = ::CreateWindowEx( dwExStyle, lpszClassName, 
        lpszWindowName, dwStyle, 
        rect.left, rect.top, rect.Width(), rect.Height(),
        hParentWnd, (HMENU)nID, GetModuleHandle(NULL), (LPVOID) this);

    Assert( m_hWnd != NULL, L"CreateWindow Error");
    DWORD dwError = GetLastError();

    return (BOOL) (m_hWnd == NULL ? NULL : m_hWnd);
}


ATOM CuWnd::WndRegisterClassEx(LPCTSTR szWindowClass)
{

    WNDCLASS    wc;
    
    wc.style            = CS_HREDRAW | CS_VREDRAW;

    wc.lpfnWndProc      = (WNDPROC) WndProc;
    wc.cbClsExtra       = 0;
    wc.cbWndExtra       = 0;
    wc.hInstance        = GetModuleHandle(NULL);
    wc.hIcon            = 0;
    wc.hCursor          = 0;
    wc.hbrBackground    = 0;
    wc.lpszMenuName     = 0;
    wc.lpszClassName    = szWindowClass;
    
    return RegisterClass(&wc);


}

2010年1月11日 星期一

CCriticalSection - a critical section object.

// a critical section object.

CCriticalSection m_cs;
...


m_cs.Enter();
...
m_cs.Leave();

// CCriticalSection.h


class CCriticalSection  
{
    CRITICAL_SECTION m_CriticalSection;
public:
    void Leave();
    void Enter();

    CCriticalSection();
    virtual ~CCriticalSection();

};




// CCriticalSection.cpp

CCriticalSection::CCriticalSection()
{
    ::InitializeCriticalSection(&m_CriticalSection);
}

CCriticalSection::~CCriticalSection()
{
    ::DeleteCriticalSection(&m_CriticalSection);
}

void CCriticalSection::Enter()
{
    ::EnterCriticalSection(&m_CriticalSection);
}

void CCriticalSection::Leave()
{
    ::LeaveCriticalSection(&m_CriticalSection);
}


2010年1月10日 星期日

CuKeyStatusComposite - group of KeyStatus

// group of KeyStatus


CuKeyStatusComposite ksComposite;

for( int j = 0 ; j < arr.size() ; j++ )
{
    CuKeyStatus *pks = new CuKeyStatus;
    // ...
    ksComposite.push_back(pks);
}

ksComposite.clear();

// CuKeyStatusComposite.h

class CuKeyStatusComposite :
 public TuComposite<CuKeyStatus>
{
public:
    CuKeyStatusComposite(void);
    ~CuKeyStatusComposite(void);

    int GetEqualItemIndex( CuKeyStatus &KeyStatus);
};



// CuKeyStatusComposite.cpp

CuKeyStatusComposite::CuKeyStatusComposite(void)
{
}

CuKeyStatusComposite::~CuKeyStatusComposite(void)
{
    this->clear();
}

int CuKeyStatusComposite::GetEqualItemIndex( CuKeyStatus &KeyStatus)
{
    int nResult = -1;
    
    _itor = _itsElement.begin();
    
    while(  _itor != _itsElement.end() )
    {
    
        if( KeyStatus == *(*_itor) )
        {
            nResult = _itor - _itsElement.begin();
            break;
        }
    
        _itor++;
    
    }
    
    return nResult;

}
GetEqualItemIndex 只有這個專案需要, 沒有打算移到 TuComposite 內, 下次在遇到時, 在處理.

2010年1月9日 星期六

CReg - An encapsulation of the Windows registry key.

// An encapsulation of the Windows registry key.  


CReg reg(HKEY_LOCAL_MACHINE, TEXT("Software\\AppName\\"));
string strPath = reg.ValueSZ( "KeyName" );


// CReg.h

class CReg  
{
private:
    HKEY    m_hKey;
    int     m_Index;
    LPBYTE  m_lpbValue; // last value read, if any

public:
    CReg();
    ~CReg();

    BOOL Create(HKEY hkRoot, LPCTSTR pszKey) {
        DWORD dwDisp;
        return ERROR_SUCCESS==RegCreateKeyEx(hkRoot, pszKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &m_hKey, &dwDisp);
    }

    BOOL Open(HKEY hkRoot, LPCTSTR pszKey, REGSAM sam=KEY_READ) {
        return ERROR_SUCCESS==RegOpenKeyEx(hkRoot, pszKey, 0, sam, &m_hKey);
    }

    CReg(HKEY hkRoot, LPCTSTR pszKey) {
        m_hKey = NULL;
        m_Index = 0;
        m_lpbValue = NULL;
        Open(hkRoot, pszKey);
    }



    void MyFree(LPBYTE m_lpbValue) {
        delete [] m_lpbValue;
    }

    LPBYTE MyRgAlloc(int dwLen)
    {
        return new BYTE[dwLen];
    }

    void Reset() { 
        if(m_hKey) RegCloseKey(m_hKey); 
        MyFree(m_lpbValue);
        m_hKey = NULL;
        m_Index = 0;
        m_lpbValue = NULL;
    }

    operator HKEY() { return m_hKey; }

    BOOL IsOK(void) { return m_hKey!=NULL; }


    BOOL EnumKey(LPTSTR psz, DWORD dwLen) {
        if(!m_hKey) return FALSE;
        return ERROR_SUCCESS==RegEnumKeyEx(m_hKey, m_Index++, psz, &dwLen, NULL, NULL, NULL, NULL);
    }

    BOOL EnumValue(LPTSTR pszName, DWORD dwLenName, LPTSTR pszValue, DWORD dwLenValue) {
        DWORD dwType;
        if(!m_hKey) return FALSE;
        dwLenValue *= sizeof(TCHAR); // convert length in chars to bytes
        return ERROR_SUCCESS==RegEnumValue(m_hKey, m_Index++, pszName, &dwLenName, NULL, &dwType, (LPBYTE)pszValue, &dwLenValue);
    }

    BOOL ValueSZ(LPCTSTR szName, LPTSTR szValue, DWORD dwLen) 
    {
        if(!m_hKey) return FALSE;
        dwLen *= sizeof(TCHAR); // convert length in chars to bytes
        return ERROR_SUCCESS==RegQueryValueEx(m_hKey, szName, NULL, NULL, (LPBYTE)szValue, &dwLen);
    }

    DWORD ValueBinary(LPCTSTR szName, LPBYTE lpbValue, DWORD dwLen) {
        if(!m_hKey) return FALSE;
        DWORD dwLenWant = dwLen;
        if(ERROR_SUCCESS==RegQueryValueEx(m_hKey, szName, NULL, NULL, lpbValue, &dwLen))
            return dwLen;
        else
            return 0;
    }

    LPCTSTR ValueSZ(LPCTSTR szName)
    {
        if(!m_hKey) return TEXT("");
        DWORD dwLen = 0;
        if( (ERROR_SUCCESS != RegQueryValueEx(m_hKey, szName, NULL, NULL, NULL, &dwLen)) || 
            (dwLen == 0) )
                return TEXT("");
        MyFree(m_lpbValue);
        if( !(m_lpbValue = MyRgAlloc(dwLen)) ||
            (ERROR_SUCCESS != RegQueryValueEx(m_hKey, szName, NULL, NULL, m_lpbValue, &dwLen)) )
                return TEXT("");
        return (LPTSTR)m_lpbValue;
    }

    LPBYTE ValueBinary(LPCTSTR szName) {
        return (LPBYTE)ValueSZ(szName);
    }

    DWORD ValueDW(LPCTSTR szName, DWORD dwDefault=0) {
        if(!m_hKey) return FALSE;
        DWORD dwValue = dwDefault;
        DWORD dwLen = sizeof(DWORD);
        RegQueryValueEx(m_hKey, szName, NULL, NULL, (LPBYTE)&dwValue, &dwLen);
        return dwValue;
    }

    LONG  ValueDW2(LPCTSTR szName, DWORD &dwDefault) {
        if(!m_hKey) return FALSE;
        DWORD dwValue = dwDefault;
        DWORD dwLen = sizeof(DWORD);
        return RegQueryValueEx(m_hKey, szName, NULL, NULL, (LPBYTE)&dwValue, &dwLen);
    }

    BOOL SetSZ(LPCTSTR szName, LPCTSTR szValue, DWORD dwLen) {
        if(!m_hKey) return FALSE;
        return ERROR_SUCCESS==RegSetValueEx(m_hKey, szName, 0, REG_SZ, (LPBYTE)szValue, sizeof(TCHAR)*dwLen);
    }
    
    BOOL SetSZ(LPCTSTR szName, LPCTSTR szValue) {
        return SetSZ(szName, szValue, 1+lstrlen(szValue));
    }

    BOOL SetDW(LPCTSTR szName, DWORD dwValue) {
        if(!m_hKey) return FALSE;
        return ERROR_SUCCESS==RegSetValueEx(m_hKey, szName, 0, REG_DWORD, (LPBYTE)&dwValue, sizeof(DWORD));
    }
    
    BOOL SetBinary(LPCTSTR szName, LPBYTE lpbValue, DWORD dwLen) {
        if(!m_hKey) return FALSE;
        return ERROR_SUCCESS==RegSetValueEx(m_hKey, szName, 0, REG_BINARY, lpbValue, dwLen);
    }

    BOOL SetMultiSZ(LPCTSTR szName, LPCTSTR lpszValue, DWORD dwLen) {
        return ERROR_SUCCESS==RegSetValueEx(m_hKey, szName, 0, REG_MULTI_SZ, (LPBYTE)lpszValue, sizeof(TCHAR)*dwLen);
    }

    BOOL DeleteValue(LPCTSTR szName) {
        if(!m_hKey) return FALSE;
        return ERROR_SUCCESS==RegDeleteValue(m_hKey, szName);
    }

    BOOL DeleteKey(LPCTSTR szName) {
        if(!m_hKey) return FALSE;
        return ERROR_SUCCESS==RegDeleteKey(m_hKey, szName);
    }
};


// CReg.cpp

CReg::CReg()
{
        m_hKey = NULL;
        m_Index = 0;
        m_lpbValue = NULL;
}

CReg::~CReg()
{
    if(m_hKey) 
        RegCloseKey(m_hKey); 
    MyFree(m_lpbValue);
}

2010年1月8日 星期五

CuRecord - Access Record

// Access Record

$record = new CuRecord($ado, $table_name, $key);

//
$record->SetField("field_name", $field_value );
$record->Save();

//
$record->Load("field_name", $field_value);
$value = $record->GetField("field_name");

//
$record->Load("field_name", $field_value);
$record->Destroy();


// CuRecord.php

class CuRecord  {

    var $ado;
    var $table_name; 
    
    var $key_name; 
    var $key_value; 
    
    var $database_fields;
    var $modified_fields;
    
    
    function CuRecord($ado, $table_name, $key_name = "sn" , $key_value = "") {
        
        $this->ado = &$ado;
        $this->table_name = $table_name;
        $this->key_name = $key_name;
        $this->key_value = $this->_check_input($key_value);
        
        $this->database_fields = array();
        $this->modified_fields = array();        
    }
    
    
    function SetField( $field, $value) {

        if( is_numeric($field) ) 
            return;
        
        $this->database_fields[$field] = $value;
        $this->modified_fields[$field] = true;

    }


    function GetField($field) {
        return $this->database_fields[$field]; 
    }   


    function Destroy() {
        
        if( !$this->key_value )
            return;
        return $this->ado->sql("DELETE FROM $this->table_name WHERE $this->key_name = '$this->key_value'");
        
    }   
    

    function Save()
    {
        
        if( $this->key_value )
        {
            $result = $this->_update();
        }
        else
        {
            $result = $this->_insert();
            $this->_load_key_value();
        }
        
        return $this->key_value;
    }
    

    function Load($bargain_field, $bargain_value)
    {
        
        if(!($bargain_field && $bargain_value ))
        {
            $bargain_field = $this->key_name;
            $bargain_value = $this->key_value;
        }
        
        if( !($bargain_field && $bargain_value ) )
            return;
        
        $value = $this->_check_input($bargain_value);
        $stmt = "`$bargain_field` = '$value'";
        $stmt = "SELECT * FROM $this->table_name WHERE $stmt";
         
        $result = $this->ado->sql($stmt);
        if( $result == "" )
            return ;
        
        $result_table = $this->ado->fetch_array();
        $this->database_fields = $result_table[0];
        
        if( sizeof($this->modified_fields) > 0 ) {
            foreach( $this->modified_fields as $key => $value ) {
                $this->modified_fields[$key] = false;
            }
        }       
        
        $this->key_value = $result_table[0][$this->key_name];
        
        return $this->key_value;
        
    }
    

    function _load_key_value()
    {
        $stmt = "SELECT MAX($this->key_name) AS $this->key_name FROM `$this->table_name` WHERE ";
        foreach( $this->database_fields as $key => $value ){
            if( !is_numeric($key) && $value ){
                if( $this->modified_fields[$key] == true ){
                    $value = $this->_check_input($value); 
                    $stmt .= "`$key` = '$value' AND ";
                }
            }
        } 
        
        $stmt = substr($stmt,0,strlen($stmt)-5);    
        $handle = $this->ado->sql($stmt);
        
        $result_table = $this->ado->fetch_array();
        $this->key_value = $result_table[0][$this->key_name];
        
        return $result_rows;
    }
    

    function _insert() {

        $stmt = "INSERT INTO `$this->table_name` ";
        
        foreach ($this->database_fields as $key => $value) {
            if( !is_numeric( $key )&& $value){
                $key = $this->_check_input($key);
                $key_stmt .= " `$key`,";
                
                $value = $this->_check_input($value);
                $value_stmt .= " '$value',";
                
            }
        }
        
        $key_stmt = substr($key_stmt,0,strlen($key_stmt)-1); 
        $value_stmt = substr($value_stmt,0,strlen($value_stmt)-1); 
        
        $stmt .= " (" . $key_stmt . ") VALUES (" . $value_stmt . ")" ;
        $handle = $this->ado->sql($stmt, "_insert");
        
        
        return $handle . " " . $stmt;
        
    }    
    
    
    function _update() {
        
        $field_value = $this->key_value;
        $field_name = $this->key_name;
        $table_name = $this->table_name;
        
        $stmt = "UPDATE `$table_name` SET ";
        foreach ($this->database_fields as $key => $value) {
            if( !is_numeric( $key ) ){
                if( $this->modified_fields[$key] == true ) {
                    $value = $this->_check_input($value);
                    if( $value == "" ){
                        $stmt .= "`$key` = 'NULL', ";
                    }
                    else {
                        $stmt .= "`$key` = '$value', ";
                    }
                }
            }
        }
        
        $stmt = substr($stmt, 0, strlen($stmt) - 2);
        $stmt .= " WHERE `$field_name` = '$field_value'"; 
        
        $handle = $this->ado->sql($stmt);
        return $handle . " " . $stmt;
        
    } 
    
    
    function _check_input($value)
    {
        if($value == "")
            return "";

        if (get_magic_quotes_gpc())
        {
            $value = stripslashes($value);
        }
        
        if (!is_numeric($value))
        {
            $value =  mysql_real_escape_string($value);
        }
        
        return $value;
    }
}

2010年1月7日 星期四

CuSize - Similar to the Windows SIZE structure

// Similar to the Windows SIZE structure

// CuSize.h

class CuSize : public tagSIZE
{
public:

// Constructors
    // construct an uninitialized size
    CuSize() throw();
    // create from two integers
    CuSize(int initCX, int initCY) throw();
    // create from another size
    CuSize(SIZE initSize) throw();
    // create from a point
    CuSize(POINT initPt) throw();
    // create from a DWORD: cx = LOWORD(dw) cy = HIWORD(dw)
    CuSize(DWORD dwSize) throw();

// Operations
    BOOL operator==(SIZE size) const throw();
    BOOL operator!=(SIZE size) const throw();
    void operator+=(SIZE size) throw();
    void operator-=(SIZE size) throw();
    void SetSize(int CX, int CY) throw();

// Operators returning CuSize values
    CuSize operator+(SIZE size) const throw();
    CuSize operator-(SIZE size) const throw();
    CuSize operator-() const throw();

// Operators returning CuPoint values
    CuPoint operator+(POINT point) const throw();
    CuPoint operator-(POINT point) const throw();

// Operators returning CuRect values
    CuRect operator+(const RECT* lpRect) const throw();
    CuRect operator-(const RECT* lpRect) const throw();
};

inline CuSize::CuSize()
    { /* random filled */ }
inline CuSize::CuSize(int initCX, int initCY)
    { cx = initCX; cy = initCY; }
inline CuSize::CuSize(SIZE initSize)
    { *(SIZE*)this = initSize; }
inline CuSize::CuSize(POINT initPt)
    { *(POINT*)this = initPt; }
inline CuSize::CuSize(DWORD dwSize)
    {
        cx = (short)LOWORD(dwSize);
        cy = (short)HIWORD(dwSize);
    }
inline BOOL CuSize::operator==(SIZE size) const
    { return (cx == size.cx && cy == size.cy); }
inline BOOL CuSize::operator!=(SIZE size) const
    { return (cx != size.cx || cy != size.cy); }
inline void CuSize::operator+=(SIZE size)
    { cx += size.cx; cy += size.cy; }
inline void CuSize::operator-=(SIZE size)
    { cx -= size.cx; cy -= size.cy; }
inline void CuSize::SetSize(int CX, int CY)
    { cx = CX; cy = CY; }   
inline CuSize CuSize::operator+(SIZE size) const
    { return CuSize(cx + size.cx, cy + size.cy); }
inline CuSize CuSize::operator-(SIZE size) const
    { return CuSize(cx - size.cx, cy - size.cy); }
inline CuSize CuSize::operator-() const
    { return CuSize(-cx, -cy); }
inline CuPoint CuSize::operator+(POINT point) const
    { return CuPoint(cx + point.x, cy + point.y); }
inline CuPoint CuSize::operator-(POINT point) const
    { return CuPoint(cx - point.x, cy - point.y); }
inline CuRect CuSize::operator+(const RECT* lpRect) const
    { return CuRect(lpRect) + *this; }
inline CuRect CuSize::operator-(const RECT* lpRect) const
    { return CuRect(lpRect) - *this; }

2010年1月6日 星期三

CuADO - connection to a MySQL Server

// connection to a MySQL Server

$this->ado = new CuADO;
$ado->Connect($host, $username, $password);
$ado->SelectDB($db); 


// CuADO.php


class CuADO  {
    
    var $link;
    var $query_handle;
    
    function CuADO() {
        
    }
    
    function Connect($host, $username, $password)
    {
        $this->link = mysql_connect($host, $username, $password);
        return $this->link;
    }
    
    function SelectDB($db)
    {
        return mysql_select_db($db, $this->link);
    }

    function sql($stmt, $debug_key = "")
    {
        $this->query_handle = mysql_query($stmt, $this->link) ;
        return $this->query_handle;
    }

    function fetch_array() {
        
        $query_handle = &$this->query_handle;
        $i = 0;
        while($result_rows[$i] = mysql_fetch_array( $query_handle ) ){
            $i++;
        }
        
        return $result_rows;
        
    }   
    
}