// allows a group of objects to be treated in the same way as a single instance of an object.
//
TuComposite<CuMyObject> myComposite;
CuMyObject *pMyObject = &myComposite;
//
class CuMyCompositeObject : public TuComposite<CuMyObject>
{
...
};
// TuComposite.h
template <typename T>
class TuComposite : public T
{
protected:
vector<T*> _itsElement;
typename vector<T*>::iterator _itor;
public:
TuComposite(){};
virtual ~TuComposite(){};
void push_back(T *pElement){ _itsElement.push_back(pElement); }
void insert(int n, T *pElement){ _itsElement.insert(_itsElement.begin() + n, pElement); }
T* at(const int nIndex){ return _itsElement[nIndex]; }
void reserve( int _Count ){_itsElement.reserve(_Count); }
int size(){ return _itsElement.size(); }
void remove(T *pElement);
void clear();
};
template <typename T>
void TuComposite<T>::clear()
{
_itor = _itsElement.begin();
while( _itor != _itsElement.end() )
{
T* pElement = *_itor;
delete pElement;
_itor++;
}
_itsElement.clear();
}
template <typename T>
void TuComposite<T>::remove(T *pElement)
{
_itor = _itsElement.begin();
while( _itor != _itsElement.end() )
{
if( pElement == *_itor )
{
_itsElement.erase(_itor);
break;
}
_itor++;
}
}
2010年1月5日 星期二
TuComposite - Template of Composite pattern
TuList - General purpose container of a list of objects
// General purpose container of a list of objects
ObjList : TuList;
ObjList := TuList.Create();
ObjList.Add(MyObject);
MyObject := ObjList.GetItems(i);
ObjList.Free;
// u_TList.pas
unit u_TList;
interface
uses
Windows, Classes, SysUtils,
StdCtrls,
ch_LogMessages;
type
TuList = class
private
m_List : TList;
public
constructor Create();
destructor Destroy(); override;
procedure Clear();
function Count: Integer;
function Add( Item:Pointer ):Integer;
function GetItems(Index: Integer): Pointer;
procedure Sort(Compare: TListSortCompare);
procedure Delete(Index: Integer);
end;
implementation
{ TuList }
function TuList.Add(Item: Pointer): Integer;
begin
Result := m_List.Add(Item);
end;
procedure TuList.Clear;
begin
m_List.Clear;
end;
function TuList.Count: Integer;
begin
Result := m_List.Count;
end;
constructor TuList.Create;
begin
m_List := TList.Create;
end;
procedure TuList.Delete(Index: Integer);
begin
m_List.Delete(Index);
end;
destructor TuList.Destroy;
begin
m_List.Free;
inherited;
end;
function TuList.GetItems(Index: Integer): Pointer;
begin
if (Index < 0) or (Index >= m_List.Count ) then begin
Index := 0;
end;
Result := m_List.Items[Index];
end;
procedure TuList.Sort(Compare: TListSortCompare);
begin
m_List.Sort(Compare);
end;
end.
2010年1月4日 星期一
CuKeyStatus - a struct of the keyboard status.
// a struct of the keyboard status.
// override operator ==
CuKeyStatus ksFromLLKeyHook;
CuKeyStatus ksClientDefault;
Assert( ksFromLLKeyHook == ksClientDefault );
// CuKeyStatus.h
class CuKeyStatus
{
public:
BOOL m_bAlt;
BOOL m_bCtrl;
BOOL m_bShift;
DWORD m_dwScanCode;
DWORD m_dwVirtualKey;
CuKeyStatus()
:m_bAlt(FALSE)
,m_bCtrl(FALSE)
,m_bShift(FALSE)
,m_dwScanCode(0)
,m_dwVirtualKey(0)
{};
~CuKeyStatus(void){};
BOOL operator == (CuKeyStatus &KeyStatus);
};
inline BOOL CuKeyStatus::operator == (CuKeyStatus &KeyStatus)
{
return m_bAlt == KeyStatus.m_bAlt &&
m_bCtrl == KeyStatus.m_bCtrl &&
m_bShift == KeyStatus.m_bShift &&
m_dwScanCode == KeyStatus.m_dwScanCode &&
m_dwVirtualKey == KeyStatus.m_dwVirtualKey ;
}
2010年1月3日 星期日
CuMenu - An encapsulation of the Windows HMENU.
// An encapsulation of the Windows HMENU.
CuMenu MenuTray.LoadMenu(IDR_MENU_MYMENU);
CuMenu *pTrackMenu = MenuTray.GetSubMenu(0);
pTrackMenu->TrackPopupMenu(TPM_BOTTOMALIGN, pt.x, pt.y, this->GetSafeHwnd() );
MenuTray.DestroyMenu();
// CuMenu.h
class CuMenu
{
HMENU m_hMenu;
public:
CuMenu(void);
~CuMenu(void);
BOOL DestroyMenu();
BOOL LoadMenu( UINT nIDResource );
CuMenu* GetSubMenu( int nPos ) const;
BOOL Attach( HMENU hMenu );
BOOL TrackPopupMenu(
UINT nFlags,
int x,
int y,
HWND hWnd,
LPCRECT lpRect = 0
);
};
// CuMenu.cpp
CuMenu::CuMenu(void)
:m_hMenu(NULL)
{
}
CuMenu::~CuMenu(void)
{
DestroyMenu();
}
BOOL CuMenu::LoadMenu( UINT nIDResource )
{
m_hMenu = ::LoadMenu(::GetModuleHandle(NULL), MAKEINTRESOURCE(nIDResource));
return ( m_hMenu != NULL );
}
CuMenu* CuMenu::GetSubMenu( int nPos ) const
{
HMENU hMenu = ::GetSubMenu( m_hMenu, nPos );
static CuMenu SubMenu;
SubMenu.Attach(hMenu);
return &SubMenu;
}
BOOL CuMenu::Attach( HMENU hMenu )
{
m_hMenu = hMenu;
return ( m_hMenu != NULL );
}
BOOL CuMenu::TrackPopupMenu(
UINT nFlags,
int x,
int y,
HWND hWnd,
LPCRECT lpRect /*= 0*/
)
{
return ::TrackPopupMenu(m_hMenu, nFlags, x, y, 0, hWnd, lpRect);
}
BOOL CuMenu::DestroyMenu()
{
return ::DestroyMenu(m_hMenu);
}
2010年1月2日 星期六
CuKeyboard - Conversion LowLevelKeyHook to CuKeyStatus
// Conversion LowLevelKeyHook to CuKeyStatus
LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
static CuKeyboard Keyboard;
Keyboard.FromLowLevelKeyHook(wParam, lParam);
CuKeyNameTextEx KeyNameTextEx(Keyboard.GetKeyStatus());
wstring strKeyboard = KeyNameTextEx;
return 0;
}
CuHook hook;
hook.Init(HookProc, WH_KEYBOARD_LL);
// CuKeyboard.h
class CuKeyboard
{
CuKeyStatus m_KeyStatus;
BOOL m_bScanCode[256];
public:
CuKeyboard(void);
~CuKeyboard(void);
void FromLowLevelKeyHook(WPARAM wParam, LPARAM lParam);
CuKeyStatus GetKeyStatus(){ return m_KeyStatus; }
private:
bool IsFunctionKey(DWORD dwScanCode);
};
// CuKeyboard.cpp
CuKeyboard::CuKeyboard(void)
{
memset((void*)m_bScanCode, 0, sizeof(m_bScanCode) );
}
CuKeyboard::~CuKeyboard(void)
{
}
void CuKeyboard::FromLowLevelKeyHook(WPARAM wParam, LPARAM lParam)
{
KBDLLHOOKSTRUCT *pKB = (KBDLLHOOKSTRUCT *)lParam;
//
BOOL bKeyDown = ( (wParam == WM_KEYDOWN) || (wParam == WM_SYSKEYDOWN) );
m_bScanCode[pKB->scanCode] = bKeyDown;
//
if( IsFunctionKey(pKB->scanCode) )
{
m_KeyStatus.m_bAlt = m_bScanCode[56];
m_KeyStatus.m_bCtrl = m_bScanCode[29];
m_KeyStatus.m_bShift = (m_bScanCode[42] || m_bScanCode[54]);
}
else
{
if( !bKeyDown )
m_KeyStatus.m_dwScanCode = 0;
else
m_KeyStatus.m_dwScanCode = pKB->scanCode;
}
}
bool CuKeyboard::IsFunctionKey(DWORD dwScanCode)
{
return ((dwScanCode == 56) || (dwScanCode == 29) || (dwScanCode == 42) || (dwScanCode == 54));
}
2010年1月1日 星期五
CuPoint - A 2-D point, similar to Windows POINT structure.
// CuPoint - A 2-D point, similar to Windows POINT structure.
class CuPoint : public tagPOINT
{
public:
// Constructors
// create an uninitialized point
CuPoint() throw();
// create from two integers
CuPoint(int initX, int initY) throw();
// create from another point
CuPoint(POINT initPt) throw();
// create from a size
CuPoint(SIZE initSize) throw();
// create from an LPARAM: x = LOWORD(dw) y = HIWORD(dw)
CuPoint(LPARAM dwPoint) throw();
// Operations
// translate the point
void Offset(int xOffset, int yOffset) throw();
void Offset(POINT point) throw();
void Offset(SIZE size) throw();
void SetPoint(int X, int Y) throw();
BOOL operator==(POINT point) const throw();
BOOL operator!=(POINT point) const throw();
void operator+=(SIZE size) throw();
void operator-=(SIZE size) throw();
void operator+=(POINT point) throw();
void operator-=(POINT point) throw();
// Operators returning CuPoint values
CuPoint operator+(SIZE size) const throw();
CuPoint operator-(SIZE size) const throw();
CuPoint operator-() const throw();
CuPoint operator+(POINT point) const throw();
// Operators returning CuSize values
CuSize operator-(POINT point) const throw();
// Operators returning CuRect values
CuRect operator+(const RECT* lpRect) const throw();
CuRect operator-(const RECT* lpRect) const throw();
};
inline CuPoint::CuPoint()
{ /* random filled */ }
inline CuPoint::CuPoint(int initX, int initY)
{ x = initX; y = initY; }
inline CuPoint::CuPoint(POINT initPt)
{ *(POINT*)this = initPt; }
inline CuPoint::CuPoint(SIZE initSize)
{ *(SIZE*)this = initSize; }
inline CuPoint::CuPoint(LPARAM dwPoint)
{
x = (short)LOWORD(dwPoint);
y = (short)HIWORD(dwPoint);
}
inline void CuPoint::Offset(int xOffset, int yOffset)
{ x += xOffset; y += yOffset; }
inline void CuPoint::Offset(POINT point)
{ x += point.x; y += point.y; }
inline void CuPoint::Offset(SIZE size)
{ x += size.cx; y += size.cy; }
inline void CuPoint::SetPoint(int X, int Y)
{ x = X; y = Y; }
inline BOOL CuPoint::operator==(POINT point) const
{ return (x == point.x && y == point.y); }
inline BOOL CuPoint::operator!=(POINT point) const
{ return (x != point.x || y != point.y); }
inline void CuPoint::operator+=(SIZE size)
{ x += size.cx; y += size.cy; }
inline void CuPoint::operator-=(SIZE size)
{ x -= size.cx; y -= size.cy; }
inline void CuPoint::operator+=(POINT point)
{ x += point.x; y += point.y; }
inline void CuPoint::operator-=(POINT point)
{ x -= point.x; y -= point.y; }
inline CuPoint CuPoint::operator+(SIZE size) const
{ return CuPoint(x + size.cx, y + size.cy); }
inline CuPoint CuPoint::operator-(SIZE size) const
{ return CuPoint(x - size.cx, y - size.cy); }
inline CuPoint CuPoint::operator-() const
{ return CuPoint(-x, -y); }
inline CuPoint CuPoint::operator+(POINT point) const
{ return CuPoint(x + point.x, y + point.y); }
inline CuSize CuPoint::operator-(POINT point) const
{ return CuSize(x - point.x, y - point.y); }
inline CuRect CuPoint::operator+(const RECT* lpRect) const
{ return CuRect(lpRect) + *this; }
inline CuRect CuPoint::operator-(const RECT* lpRect) const
{ return CuRect(lpRect) - *this; }
CuKeyNameTextEx - retrieves a string that represents the name of a composite key
// retrieves a string that represents the name of a composite key
CuKeyStatus KeyStatus;
....
CuKeyNameTextEx KeyNameTextEx(KeyStatus);
wstring strKeyText = KeyNameTextEx;
// Assert( strKeyText == TEXT("Alt+Ctrl+Shift+D"));
// Assert( strKeyText == TEXT("Alt+Ctrl+D"));
// Assert( strKeyText == TEXT("Ctrl+Shift+D"));
// CuKeyNameTextEx.h
class CuKeyNameTextEx
{
wstring m_strKeyText;
public:
CuKeyNameTextEx(void);
CuKeyNameTextEx(CuKeyStatus &KeyStatus);
~CuKeyNameTextEx(void);
void SetKeyStatus(CuKeyStatus &KeyStatus);
operator wstring() const
{
return m_strKeyText;
};
BOOL IsMustAdd(bool l, bool r);
private:
wstring ProcessFunctionKey(CuKeyStatus &KeyStatus);
};
// CuKeyNameTextEx.cpp
CuKeyNameTextEx::CuKeyNameTextEx(void)
{
}
CuKeyNameTextEx::~CuKeyNameTextEx(void)
{
}
CuKeyNameTextEx::CuKeyNameTextEx(CuKeyStatus &KeyStatus)
{
SetKeyStatus(KeyStatus);
}
void CuKeyNameTextEx::SetKeyStatus(CuKeyStatus &KeyStatus)
{
if( !KeyStatus.m_dwScanCode )
return;
wstring strResult;
strResult = ProcessFunctionKey(KeyStatus);
CuKeyNameText KeyName(KeyStatus.m_dwScanCode);
wstring strKeyText = KeyName;
wstring strAdd = TEXT("+");
if( !strKeyText.empty() && !strResult.empty() )
strResult += strAdd;
strResult += strKeyText;
m_strKeyText = strResult;
}
wstring CuKeyNameTextEx::ProcessFunctionKey(CuKeyStatus &KeyStatus)
{
wstring strResult;
wstring strAlt = TEXT("Alt");
wstring strCtrl = TEXT("Ctrl");
wstring strShift = TEXT("Shift");
wstring strAdd = TEXT("+");
if( KeyStatus.m_bAlt )
strResult += strAlt;
if( KeyStatus.m_bAlt && KeyStatus.m_bCtrl )
strResult += strAdd;
if( KeyStatus.m_bCtrl )
strResult += strCtrl;
if( KeyStatus.m_bShift && (KeyStatus.m_bAlt || KeyStatus.m_bCtrl) )
strResult += strAdd;
if( KeyStatus.m_bShift )
strResult += strShift;
return strResult;
}
訂閱:
文章 (Atom)