// 宏定义常用的窗口样式和窗口类样式 // 宏定义ASSERT, TRACE, TRACEMSG用于调试 void UILIB_API __Trace(LPCTSTR pstrFormat, ...) { #ifdef _DEBUG TCHAR szBuffer[300] = { 0 }; va_list args; va_start(args, pstrFormat); ::wvnsprintf(szBuffer, lengthof(szBuffer) - 2, pstrFormat, args); _tcscat(szBuffer, _T("\n")); va_end(args); ::OutputDebugString(szBuffer); #endif } LPCTSTR __TraceMsg(UINT uMsg) { #define MSGDEF(x) if(uMsg==x) return #x MSGDEF(WM_SETCURSOR); MSGDEF(WM_NCHITTEST); MSGDEF(WM_NCPAINT); MSGDEF(WM_PAINT); MSGDEF(WM_ERASEBKGND); MSGDEF(WM_NCMOUSEMOVE); MSGDEF(WM_MOUSEMOVE); MSGDEF(WM_MOUSELEAVE); MSGDEF(WM_MOUSEHOVER); MSGDEF(WM_NOTIFY); MSGDEF(WM_COMMAND); MSGDEF(WM_MEASUREITEM); MSGDEF(WM_DRAWITEM); MSGDEF(WM_LBUTTONDOWN); MSGDEF(WM_LBUTTONUP); MSGDEF(WM_LBUTTONDBLCLK); MSGDEF(WM_RBUTTONDOWN); MSGDEF(WM_RBUTTONUP); MSGDEF(WM_RBUTTONDBLCLK); MSGDEF(WM_SETFOCUS); MSGDEF(WM_KILLFOCUS); MSGDEF(WM_MOVE); MSGDEF(WM_SIZE); MSGDEF(WM_SIZING); MSGDEF(WM_MOVING); MSGDEF(WM_GETMINMAXINFO); MSGDEF(WM_CAPTURECHANGED); MSGDEF(WM_WINDOWPOSCHANGED); MSGDEF(WM_WINDOWPOSCHANGING); MSGDEF(WM_NCCALCSIZE); MSGDEF(WM_NCCREATE); MSGDEF(WM_NCDESTROY); MSGDEF(WM_TIMER); MSGDEF(WM_KEYDOWN); MSGDEF(WM_KEYUP); MSGDEF(WM_CHAR); MSGDEF(WM_SYSKEYDOWN); MSGDEF(WM_SYSKEYUP); MSGDEF(WM_SYSCOMMAND); MSGDEF(WM_SYSCHAR); MSGDEF(WM_VSCROLL); MSGDEF(WM_HSCROLL); MSGDEF(WM_CHAR); MSGDEF(WM_SHOWWINDOW); MSGDEF(WM_PARENTNOTIFY); MSGDEF(WM_CREATE); MSGDEF(WM_NCACTIVATE); MSGDEF(WM_ACTIVATE); MSGDEF(WM_ACTIVATEAPP); MSGDEF(WM_CLOSE); MSGDEF(WM_DESTROY); MSGDEF(WM_GETICON); MSGDEF(WM_GETTEXT); MSGDEF(WM_GETTEXTLENGTH); static TCHAR szMsg[10]; ::wsprintf(szMsg, _T("0x%04X"), uMsg); return szMsg; } class UILIB_API CRect : public tagRECT { public: CRect(); CRect(const RECT& src); CRect(int iLeft, int iTop, int iRight, int iBottom); int GetWidth() const; int GetHeight() const; void Empty(); void Join(const RECT& rc); // 与rc合并成更大的RECT void ResetOffset(); // 整体向左上角移动一个this(CRect)大小 void Normalize(); // 调整成员变量以保证其left <= right top <= bottom void Offset(int cx, int cy); void Inflate(int cx, int cy); // 与Deflate对应,分别是增大和缩小this(CRect),其实只要一个实现通过正负不同的参数进行控制即可 void Deflate(int cx, int cy); void Union(CRect& rc); // 求并集,感觉与Join一样 }; class UILIB_API CSize : public tagSIZE { public: CSize(); CSize(const SIZE& src); CSize(const RECT rc); // 求rc的长宽 CSize(int cx, int cy); }; class UILIB_API CPoint : public tagPOINT { public: CPoint(); CPoint(const POINT& src); CPoint(int x, int y); CPoint(LPARAM lParam); // 鼠标消息需要x = GET_X_LPARAM(lParam); y = GET_Y_LPARAM(lParam); }; class UILIB_API CStdPtrArray // 指针数组 { public: CStdPtrArray(int iPreallocSize = 0); virtual ~CStdPtrArray(); void Empty(); void Resize(int iSize); bool IsEmpty() const; int Find(LPVOID iIndex) const; bool Add(LPVOID pData); bool SetAt(int iIndex, LPVOID pData); bool InsertAt(int iIndex, LPVOID pData); bool Remove(int iIndex); int GetSize() const; LPVOID* GetData(); LPVOID GetAt(int iIndex) const; LPVOID operator[] (int nIndex) const; protected: LPVOID* m_ppVoid; int m_nCount; int m_nAllocated; }; class UILIB_API CStdValArray // 数据的数组 { public: CStdValArray(int iElementSize, int iPreallocSize = 0); virtual ~CStdValArray(); void Empty(); bool IsEmpty() const; bool Add(LPCVOID pData); bool Remove(int iIndex); int GetSize() const; LPVOID GetData(); LPVOID GetAt(int iIndex) const; LPVOID operator[] (int nIndex) const; protected: LPBYTE m_pVoid; int m_iElementSize; int m_nCount; int m_nAllocated; }; class UILIB_API CWindowWnd // 窗口封装,核心! { public: CWindowWnd(); HWND GetHWND() const; operator HWND() const; bool RegisterWindowClass(); // 当不是子类化窗口时调用 bool RegisterSuperclass(); // 当是子类化窗口时调用 HWND Create(HWND hwndParent, LPCTSTR pstrName, DWORD dwStyle, DWORD dwExStyle, const RECT rc, HMENU hMenu = NULL); // 调用下面的Create HWND Create(HWND hwndParent, LPCTSTR pstrName, DWORD dwStyle, DWORD dwExStyle, int x = CW_USEDEFAULT, int y = CW_USEDEFAULT, int cx = CW_USEDEFAULT, int cy = CW_USEDEFAULT, HMENU hMenu = NULL); // 调用CreateWindowEx传入this指针 HWND Subclass(HWND hWnd); // 为hWnd设置新的窗口过程__WndProc,并用CWindwowWnd Attach上hWnd void Unsubclass(); void ShowWindow(bool bShow = true, bool bTakeFocus = true); bool ShowModal(); // 通过EnableWindow(hWndParent, FALSE);并在内部实现消息循环模拟 void Close(); void CenterWindow(); void SetIcon(UINT nRes); protected: virtual LPCTSTR GetWindowClassName() const = 0; virtual LPCTSTR GetSuperClassName() const; // return NULL; virtual UINT GetClassStyle() const; // return 0; 窗口应该自己实现,在RegisterWindowClass或RegisterSuperclass中调用 LRESULT SendMessage(UINT uMsg, WPARAM wParam = 0, LPARAM lParam = 0L); LRESULT PostMessage(UINT uMsg, WPARAM wParam = 0, LPARAM lParam = 0L); void ResizeClient(int cx = -1, int cy = -1); virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam); // CallWindowProc(m_OldWndProc, m_hWnd, uMsg, wParam, lParam); virtual void OnFinalMessage(HWND hWnd); // this的最后的消息处理机会 static LRESULT CALLBACK __WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); // 取出this并调用HandleMessage static LRESULT CALLBACK __ControlProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); protected: HWND m_hWnd; WNDPROC m_OldWndProc; // 用来实现窗口子类化 bool m_bSubclassed; }; class UILIB_API CWaitCursor // 图标切换 { public: CWaitCursor(); ~CWaitCursor(); protected: HCURSOR m_hOrigCursor; }; class UILIB_API CStdString // 字符串 { public: enum { MAX_LOCAL_STRING_LEN = 63 }; CStdString(); CStdString(const TCHAR ch); CStdString(const CStdString& src); CStdString(LPCTSTR lpsz, int nLen = -1); // IsBadStringPtr virtual ~CStdString(); static CStdString RES(UINT nRes); void Empty(); int GetLength() const; bool IsEmpty() const; TCHAR GetAt(int nIndex) const; void Append(LPCTSTR pstr); void Assign(LPCTSTR pstr, int nLength = -1); LPCTSTR GetData(); void SetAt(int nIndex, TCHAR ch); operator LPCTSTR() const; TCHAR operator[] (int nIndex) const; const CStdString& operator=(const CStdString& src); const CStdString& operator=(const TCHAR ch); const CStdString& operator=(LPCTSTR pstr); #ifndef _UNICODE const CStdString& CStdString::operator=(LPCWSTR lpwStr); #endif CStdString operator+(const CStdString& src); CStdString operator+(LPCTSTR pstr); const CStdString& operator+=(const CStdString& src); const CStdString& operator+=(LPCTSTR pstr); const CStdString& operator+=(const TCHAR ch); bool operator == (LPCTSTR str) const; bool operator != (LPCTSTR str) const; bool operator <= (LPCTSTR str) const; bool operator < (LPCTSTR str) const; bool operator >= (LPCTSTR str) const; bool operator > (LPCTSTR str) const; int Compare(LPCTSTR pstr) const; int CompareNoCase(LPCTSTR pstr) const; void MakeUpper(); void MakeLower(); CStdString Left(int nLength) const; CStdString Mid(int iPos, int nLength = -1) const; CStdString Right(int nLength) const; int Find(TCHAR ch, int iPos = 0) const; int Find(LPCTSTR pstr, int iPos = 0) const; int ReverseFind(TCHAR ch) const; int Replace(LPCTSTR pstrFrom, LPCTSTR pstrTo); void ProcessResourceTokens(); int __cdecl Format(LPCTSTR pstrFormat, ...); protected: LPTSTR m_pstr; TCHAR m_szBuffer[MAX_LOCAL_STRING_LEN + 1]; };