您当前的位置: 首页 >  ui

插件开发

暂无认证

  • 1浏览

    0关注

    492博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

第一章 第二小节Duilib的基本支撑类(二)

插件开发 发布时间:2022-03-21 11:03:38 ,浏览量:1

文章目录
    • 1.CDuiString
    • 2.CStdStringPtrMap
    • 3.CDuiVariant
    • 4.作者答疑
  如果采用一篇文章将基础类介绍下来,会显得比较长,阅读不便,所以分成两部分,接下来讲解CDuiString、CStdStringPtrMap和CDuiVariant,这三个基本对象。

1.CDuiString

  它是Duilib库中对字符串对象的一个封装,里面包含了对字符串的常用操作,见注释。如下所示:

class UILIB_API CDuiString
    {
    public:
        enum { MAX_LOCAL_STRING_LEN = 63 };
		//---------初始化的方式------------
        CDuiString();
        CDuiString(const TCHAR ch);
        CDuiString(const CDuiString& src);
        CDuiString(LPCTSTR lpsz, int nLen = -1);
        ~CDuiString();

        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() const;
        std::wstring GetStringW();
        std::string GetStringA();

        void SetAt(int nIndex, TCHAR ch);
        operator LPCTSTR() const;
		
		//字符串的赋值和链接
        TCHAR operator[] (int nIndex) const;
        const CDuiString& operator=(const CDuiString& src);
        const CDuiString& operator=(const TCHAR ch);
        const CDuiString& operator=(LPCTSTR pstr);
#ifdef _UNICODE
        const CDuiString& CDuiString::operator=(LPCSTR lpStr);
        const CDuiString& CDuiString::operator+=(LPCSTR lpStr);
#else
        const CDuiString& CDuiString::operator=(LPCWSTR lpwStr);
        const CDuiString& CDuiString::operator+=(LPCWSTR lpwStr);
#endif
        CDuiString operator+(const CDuiString& src) const;
        CDuiString operator+(LPCTSTR pstr) const;
        const CDuiString& operator+=(const CDuiString& src);
        const CDuiString& operator+=(LPCTSTR pstr);
        const CDuiString& operator+=(const TCHAR ch);
		//字符串的比较函数
        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();//字符串转小写

		//字符串截断
        CDuiString Left(int nLength) const;
        CDuiString Mid(int iPos, int nLength = -1) const;
        CDuiString 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);//
		//格式化字符串
        int __cdecl Format(LPCTSTR pstrFormat, ...);//
        int __cdecl SmallFormat(LPCTSTR pstrFormat, ...);//

    protected:
        int __cdecl InnerFormat(LPCTSTR pstrFormat, va_list Args);//

    protected:
        LPTSTR m_pstr;
        TCHAR m_szBuffer[MAX_LOCAL_STRING_LEN + 1];
    };
2.CStdStringPtrMap

  它是以CDuiString为key的一种map对象,内部数组元素是TITEM对象,而相关对象是对象指针。类里包含有map数据机构的基本操作。如下所示:

 class UILIB_API CStdStringPtrMap
    {
    public:
        CStdStringPtrMap(int nSize = 83);
        ~CStdStringPtrMap();

        void Resize(int nSize = 83);
        LPVOID Find(LPCTSTR key, bool optimize = true) const;
        bool Insert(LPCTSTR key, LPVOID pData);
        LPVOID Set(LPCTSTR key, LPVOID pData);
        bool Remove(LPCTSTR key);
        void RemoveAll();
        int GetSize() const;
        LPCTSTR GetAt(int iIndex) const;
        LPCTSTR operator[] (int nIndex) const;

    protected:
        TITEM** m_aT;
        int m_nBuckets;
        int m_nCount;
    };
3.CDuiVariant

  它是继承至VARIANT的一种变体数据类型,同一种结构保存不同类型的数据,提高内存的利用效率。

class CDuiVariant : public VARIANT
    {
    public:
        CDuiVariant() 
        { 
            VariantInit(this); 
        }
        CDuiVariant(int i)
        {
            VariantInit(this);
            this->vt = VT_I4;
            this->intVal = i;
        }
        CDuiVariant(float f)
        {
            VariantInit(this);
            this->vt = VT_R4;
            this->fltVal = f;
        }
        CDuiVariant(LPOLESTR s)
        {
            VariantInit(this);
            this->vt = VT_BSTR;
            this->bstrVal = s;
        }
        CDuiVariant(IDispatch *disp)
        {
            VariantInit(this);
            this->vt = VT_DISPATCH;
            this->pdispVal = disp;
        }

        ~CDuiVariant() 
        { 
            VariantClear(this); 
        }
    };
4.作者答疑

  如有疑问,请留言。

关注
打赏
1665481431
查看更多评论
立即登录/注册

微信扫码登录

0.0483s