鱼C论坛

 找回密码
 立即注册
查看: 3557|回复: 6

怎样把dephi的关键字高亮

[复制链接]
发表于 2012-6-13 19:55:35 | 显示全部楼层 |阅读模式
1鱼币
怎样把dephi的关键字高亮

最佳答案

查看完整内容

在 RichEdit 中实现代码着色 贴给你好了,我找到的,试过,很好使。 -------------------------------------- 在 RichEdit 中实现代码着色 下面的代码将以指定颜色对一些指定单词着色,就象delphi中的代码编辑器那样。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-6-13 19:55:36 | 显示全部楼层
在   RichEdit   中实现代码着色

贴给你好了,我找到的,试过,很好使。
--------------------------------------
在   RichEdit   中实现代码着色

下面的代码将以指定颜色对一些指定单词着色,就象delphi中的代码编辑器那样。
  1. procedure   CodeColors(Form   :   TForm;Style   :   String;   RichE   :   TRichedit;
  2.                                                   InVisible   :   Boolean);   
  3. const   
  4.     //   符号...   
  5.     CodeC1:   array[0..20]   of   String   =   ( '# ', '$ ', '( ', ') ', '* ', ', ',   
  6.                     '. ', '/ ', ': ', '; ', '[ ', '] ', '{ ', '} ', ' < ', '> ',   
  7.                     '- ', '= ', '+ ', ' ' ' ', '@ ');   
  8.     //   保留字...   
  9.     CodeC2:   array[0..44]   of   String   =   ( 'and ', 'as ', 'begin ',   
  10.                     'case ', 'char ', 'class ', 'const ', 'downto ',   
  11.                     'else ', 'end ', 'except ', 'finally ', 'for ',   
  12.                     'forward ', 'function ', 'if ', 'implementation ', 'interface ',   
  13.                     'is ', 'nil ', 'or ', 'private ', 'procedure ', 'public ', 'raise ',   
  14.                     'repeat ', 'string ', 'to ', 'try ', 'type ', 'unit ', 'uses ', 'var ',   
  15.                     'while ', 'external ', 'stdcall ', 'do ', 'until ', 'array ', 'of ',   
  16.                     'in ', 'shr ', 'shl ', 'cos ', 'div ');   
  17. var   
  18.     FoundAt   :   LongInt;   
  19.     StartPos,   ToEnd,   i   :   integer;   
  20.     OldCap,T   :   String;   
  21.     FontC,   BackC,   C1,   C2   ,C3   ,strC,   strC1   :   TColor;   
  22. begin   
  23.     OldCap   :=   Form.Caption;   
  24.     with   RichE   do   
  25.     begin   
  26.         Font.Name   :=   'Courier   New ';   
  27.         Font.Size   :=   10;   
  28.         if   WordWrap   then   WordWrap   :=   false;   
  29.         SelectAll;   
  30.         SelAttributes.color   :=   clBlack;   
  31.         SelAttributes.Style   :=   [];   
  32.         SelStart   :=   0;   
  33.         if   InVisible   then   
  34.         begin   
  35.             Visible   :=   False;   
  36.             Form.Caption   :=   'Executing   Code   Coloring... ';   
  37.         end;   
  38.     end;   

  39.     BackC   :=   clWhite;   FontC   :=   clBlack;   
  40.     C1   :=   clBlack;   C2   :=   clBlack;   C3   :=   clBlack;   
  41.     strC   :=   clBlue;   strC1   :=   clSilver;   

  42.     if   Style   =   'Twilight '   then   
  43.     begin   
  44.         BackC   :=   clBlack;   FontC   :=   clWhite;   
  45.         C1   :=   clLime;   C2   :=   clSilver;   C3   :=   clAqua;   
  46.         strC   :=   clYellow;   strC1   :=   clRed;   
  47.     end   
  48.     else   
  49.     if   Style   =   'Default '   then   
  50.     begin   
  51.         BackC   :=   clWhite;   FontC   :=   clBlack;   
  52.         C1   :=   clTeal;   C2   :=   clMaroon;   C3   :=   clBlue;   
  53.         strC   :=   clMaroon;   strC1   :=   clSilver;   
  54.     end   
  55.     else   
  56.     if   Style   =   'Ocean '   then   
  57.     begin   
  58.         BackC   :=   $00FFFF80;   FontC   :=   clBlack;   
  59.         C1   :=   clMaroon;   C2   :=   clBlack;   C3   :=   clBlue;   
  60.         strC   :=   clTeal;   strC1   :=   clBlack;   
  61.     end   
  62.     else   
  63.     if   Style   =   'Classic '   then   
  64.     begin   
  65.         BackC   :=   clNavy;   FontC   :=   clYellow;   
  66.         C1   :=   clLime;   C2   :=   clSilver;   C3   :=   clWhite;   
  67.         strC   :=   clAqua;   strC1   :=   clSilver;   
  68.     end   
  69.     else   
  70.     begin   
  71.         with   RichE   do   
  72.         begin   
  73.             T   :=   '{ '+Style+ '   =   Invalid   Style   [Default,Classic,Twilight,Ocean]   ONLY!   } ';   
  74.             Lines.Insert(0,T);   
  75.             StartPos   :=   0;   
  76.             ToEnd   :=   Length(Text)   -   StartPos;   
  77.             FoundAt   :=   FindText(T,   StartPos,   ToEnd,   [stWholeWord]);   
  78.             SelStart   :=   FoundAt;   
  79.             SelLength   :=   Length(T);   
  80.             SelAttributes.Color   :=   clRed;   
  81.             SelAttributes.Style   :=   [fsBold];   
  82.             StartPos   :=   0;   
  83.             ToEnd   :=   Length(Text)   -   StartPos;   
  84.             FoundAt   :=   FindText( 'ONLY! ',   StartPos,   ToEnd,   [stWholeWord]);   
  85.             SelStart   :=   FoundAt;   
  86.             SelLength   :=   4;   
  87.             SelAttributes.Color   :=   clRed;   
  88.             SelAttributes.Style   :=   [fsBold,fsUnderLine];   
  89.         end;   
  90.     end;   

  91.     RichE.SelectAll;   
  92.     RichE.color   :=   BackC;   
  93.     RichE.SelAttributes.color   :=   FontC;   

  94.     for   i   :=   0   to   100   do   
  95.     begin   
  96.         with   RichE   do   
  97.         begin   
  98.             StartPos   :=   0;   
  99.             ToEnd   :=   Length(Text)   -   StartPos;   
  100.             FoundAt   :=   FindText(IntToStr(i),   StartPos,   ToEnd,   [stWholeWord]);   
  101.             while   (FoundAt   <>   -1)   do   
  102.             begin   
  103.                 SelStart   :=   FoundAt;   
  104.                 SelLength   :=   Length(IntToStr(i));   
  105.                 SelAttributes.Color   :=   C1;   
  106.                 SelAttributes.Style   :=   [];   
  107.                 StartPos   :=   FoundAt   +   Length(IntToStr(i));   
  108.                 FoundAt   :=   FindText(IntToStr(i),   StartPos,   ToEnd,   [stWholeWord]);   
  109.             end;   
  110.         end;   
  111.     end;   
  112.     for   i   :=   0   to   20   do   
  113.     begin   
  114.         with   RichE   do   
  115.         begin   
  116.             StartPos   :=   0;   
  117.             ToEnd   :=   Length(Text)   -   StartPos;   
  118.             FoundAt   :=   FindText(CodeC1[i],   StartPos,   ToEnd,   []);   
  119.             while   (FoundAt   <>   -1)   do   
  120.             begin   
  121.                 SelStart   :=   FoundAt;   
  122.                 SelLength   :=   Length(CodeC1[i]);   
  123.                 SelAttributes.Color   :=   C2;   
  124.                 StartPos   :=   FoundAt   +   Length(CodeC1[i]);   
  125.                 FoundAt   :=   FindText(CodeC1[i],   StartPos,   ToEnd,   []);   
  126.             end;   
  127.         end;   
  128.     end;   
  129.     for   i   :=   0   to   44   do   
  130.     begin   
  131.         with   RichE   do   
  132.         begin   
  133.             StartPos   :=   0;   
  134.             ToEnd   :=   Length(Text)   -   StartPos;   
  135.             FoundAt   :=   FindText(CodeC2[i],   StartPos,   ToEnd,   [stWholeWord]);   
  136.             while   (FoundAt   <>   -1)   do   
  137.             begin   
  138.                 SelStart   :=   FoundAt;   
  139.                 SelLength   :=   Length(CodeC2[i]);   
  140.                 SelAttributes.Color   :=   C3;   
  141.                 SelAttributes.Style   :=   [fsBold];   
  142.                 StartPos   :=   FoundAt   +   Length(CodeC2[i]);   
  143.                 FoundAt   :=   FindText(CodeC2[i],   StartPos,   ToEnd,   [stWholeWord]);   
  144.             end;   
  145.         end;   
  146.     end;   
  147.     Startpos   :=   0;   
  148.     with   RichE   do   
  149.     begin   
  150.         FoundAt   :=   FindText( ' ' ' ',   StartPos,   Length(Text),   []);   
  151.         while   FoundAt   <>   -1   do   
  152.         begin   
  153.             SelStart   :=   FoundAt;   
  154.             Startpos   :=   FoundAt+1;   
  155.             FoundAt   :=   FindText( ' ' ' ',   StartPos,   Length(Text),   []);   
  156.             if   FoundAt   <>   -1   then   
  157.             begin   
  158.                 SelLength   :=   (FoundAt   -   selstart)+1;   
  159.                 SelAttributes.Style   :=   [];   
  160.                 SelAttributes.Color   :=   strC;   
  161.                 StartPos   :=   FoundAt+1;   
  162.                 FoundAt   :=   FindText( ' ' ' ',   StartPos,   Length(Text),   []);   
  163.             end;   
  164.         end;   
  165.     end;   

  166.     Startpos   :=   0;   
  167.     with   RichE   do   
  168.     begin   
  169.         FoundAt   :=   FindText( '{ ',   StartPos,   Length(Text),   []);   
  170.         while   FoundAt   <>   -1   do   
  171.         begin   
  172.             SelStart   :=   FoundAt;   
  173.             Startpos   :=   FoundAt+1;   
  174.             FoundAt   :=   FindText( '} ',   StartPos,   Length(Text),   []);   
  175.             if   FoundAt   <>   -1   then   
  176.             begin   
  177.                 SelLength   :=   (FoundAt   -   selstart)+1;   
  178.                 SelAttributes.Style   :=   [];   
  179.                 SelAttributes.Color   :=   strC1;   
  180.                 StartPos   :=   FoundAt+1;   
  181.                 FoundAt   :=   FindText( '{ ',   StartPos,   Length(Text),   []);   
  182.             end;   
  183.         end;   
  184.     end;      

  185.     if   InVisible   then   
  186.     begin   
  187.         RichE.Visible   :=   True;   
  188.         Form.Caption   :=   OldCap;   
  189.     end;   
  190.     RichE.SelStart   :=   0;   
  191. end;
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-6-13 19:56:07 | 显示全部楼层
真是被催啊大家帮帮忙啊
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-6-16 22:32:18 | 显示全部楼层

我问一个傻B的问题哈这个richedit   在哪里啊,怎样实现它啊,俺是新手
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-6-17 12:18:48 | 显示全部楼层
我问一个傻B的问题哈这个richedit   在哪里啊,怎样实现它啊,俺是新手

在Delphi里搜索组件啊 win32选项卡里 小甲鱼最新讲解的记事本用的就是她
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-6-17 15:44:09 | 显示全部楼层
哎算了不会搞啊,哈哈谢谢了给你分
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-3-16 01:53:26 | 显示全部楼层
看一看 看一看
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-6 18:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表