GacUI Demo:文本框高亮(二)

GacUI Demo:文本框高亮(二)
    GacUI终于把 上一篇文章中提到的自动采用正则表达式进行高亮的Demo做出来了。这次实现的是C++ colorizer。而XML colorizer不仅需要正则表达式,还需要一个人工维护的状态,这个等到下一个Demo再提及。先看图



    在不需要人工维护状态,仅通过正则表达式就可以着色的时候,编写一个colorizer变得十分的简单。这个Colorizer虽然不是一定非得通过继承来实现,但是这个Demo还是使用了继承。首先编写一个类,继承自GuiTextBoxRegexColorizer,然后在构造函数里面填写下面的代码:

class  CppColorizer :  public  GuiTextBoxRegexColorizer
{
public :
    CppColorizer()
    {
        text::ColorEntry entry
= win7::Win7GetTextBoxTextColor();
        SetDefaultColor(entry);

        entry.normal.text
= Color( 128 0 255 );
        AddToken(L
" /d+(./d*)?([eE][+/-]?/d+)? " , entry);

        entry.normal.text
= Color( 163 21 21 );
        AddToken(L
" \ " ([ ^ \ " ]|\\\\/.)*\ "" , entry);

        entry.normal.text
= Color( 0 128 0 );
        AddToken(L
" ////[^\r\n]* " , entry);
        AddToken(L
" ///*(//|[*]*[^*//])*/*+// " , entry);

        entry.normal.text
= Color( 0 0 255 );
        AddToken(L
" #[a-zA-Z0-9_]* " , entry);
        AddToken(CppKeywords, entry);
        
        AddToken(L
" [a-zA-Z0-9_]+ " , GetDefaultColor());

        Setup();
    }
};

    然后只需要把它绑定到文本框里面就可以了。在这个Demo里面,我们在下拉框的事件里面添加下面的代码:

     void  comboSelector_SelectedIndexChanged(GuiGraphicsComposition *  sender, GuiEventArgs &  arguments)
    {
        
switch (comboSelector -> GetSelectedIndex())
        {
        
case   0 :(略)
        
case   1 :(略)
        
case   2 :
            textBox
-> SetColorizer( new  CppColorizer);
            textBox
-> SetText(
                L
" #include <iostream>\r\n "
                L
" using namespace std;\r\n "
                L
" \r\n "
                L
" int main()\r\n "
                L
" {\r\n "
                L
" \t//This is a comment\r\n "
                L
" \t/**This*is/another\r\n "
                L
" \tcomment**/\r\n "
                L
" \tcout<<\ " Hello, world ! \ " <<endl;\r\n "
                L
" \treturn 0;\r\n "
                L
" }\r\n "
                );
            
break ;
        
default :
            textBox
-> SetColorizer( 0 );
        }
    }

    当然这样是不完整的,因为CppColorizer里面还有一个CppKeywords的常量。这实际上是一个正则表达式,里面用“|”字符把所有C++的关键字连了起来。内容抄自MSDN的C++ Language Reference:

  1  const  wchar_t *  CppKeywords =
  2  L " __abstract| "
  3  L " __alignof| "
  4  L " __asm| "
  5  L " __assume| "
  6  L " __based| "
  7  L " __box| "
  8  L " __cdecl| "
  9  L " __declspec| "
 10  L " __delegate| "
 11  L " __event| "
 12  L " __except| "
 13  L " __fastcall| "
 14  L " __finally| "
 15  L " __forceinline| "
 16  L " __gc| "
 17  L " __hook| "
 18  L " __identifier| "
 19  L " __if_exists| "
 20  L " __if_not_exists| "
 21  L " __inline| "
 22  L " __int16| "
 23  L " __int32| "
 24  L " __int64| "
 25  L " __int8| "
 26  L " __interface| "
 27  L " __leave| "
 28  L " __m128d| "
 29  L " __m128| "
 30  L " __m128i| "
 31  L " __m64| "
 32  L " __multiple_inheritance| "
 33  L " __nogc| "
 34  L " __noop| "
 35  L " __pin| "
 36  L " __property| "
 37  L " __raise| "
 38  L " __sealed| "
 39  L " __single_inheritance| "
 40  L " __stdcall| "
 41  L " __super| "
 42  L " __try| "
 43  L " __except| "
 44  L " __finally| "
 45  L " __try_cast| "
 46  L " __unaligned| "
 47  L " __unhook| "
 48  L " __uuidof| "
 49  L " __value| "
 50  L " __virtual_inheritance| "
 51  L " __w64| "
 52  L " __wchar_t| "
 53  L " wchar_t| "
 54  L " abstract| "
 55  L " array| "
 56  L " bool| "
 57  L " break| "
 58  L " case| "
 59  L " catch| "
 60  L " char| "
 61  L " class| "
 62  L " const_cast| "
 63  L " const| "
 64  L " continue| "
 65  L " decltype| "
 66  L " default| "
 67  L " delegate| "
 68  L " delete| "
 69  L " do| "
 70  L " double| "
 71  L " dynamic_cast| "
 72  L " else| "
 73  L " enum| "
 74  L " event| "
 75  L " explicit| "
 76  L " extern| "
 77  L " false| "
 78  L " finally| "
 79  L " float| "
 80  L " for| "
 81  L " friend| "
 82  L " gcnew| "
 83  L " generic| "
 84  L " goto| "
 85  L " if| "
 86  L " initonly| "
 87  L " inline| "
 88  L " int| "
 89  L " interface| "
 90  L " interior_ptr| "
 91  L " literal| "
 92  L " long| "
 93  L " mutable| "
 94  L " namespace| "
 95  L " new| "
 96  L " new| "
 97  L " nullptr| "
 98  L " operator| "
 99  L " private| "
100  L " property| "
101  L " property| "
102  L " protected| "
103  L " public| "
104  L " register| "
105  L " reinterpret_cast| "
106  L " return| "
107  L " sealed| "
108  L " short| "
109  L " signed| "
110  L " sizeof| "
111  L " static_assert| "
112  L " static_cast| "
113  L " static| "
114  L " struct| "
115  L " switch| "
116  L " template| "
117  L " this| "
118  L " __thiscall| "
119  L " throw| "
120  L " true| "
121  L " try| "
122  L " typedef| "
123  L " typeid| "
124  L " typeid| "
125  L " typename| "
126  L " union| "
127  L " unsigned| "
128  L " using| "
129  L " virtual| "
130  L " void| "
131  L " volatile| "
132  L " while " ;

    使用GacUI为文本框着色已经变得如此简单。

你可能感兴趣的:(GacUI Demo:文本框高亮(二))