在 RichEdit 中实现代码着色
下面的代码将以指定颜色对一些指定单词着色,就象delphi中的代码编辑器那样。
procedure CodeColors(Form : TForm;Style : String; RichE : TRichedit;
InVisible : Boolean);
const
// 符号...
CodeC1: array[0..20] of String = (''''#'''',''''$'''',''''('''','''')'''',''''*'''','''','''',
''''.'''',''''/'''','''':'''','''';'''',''''['''','''']'''',''''{'''',''''}'''',''''<'''',''''>'''',
''''-'''',''''='''','''' '''','''''''''''''''',''''@'''');
// 保留字...
CodeC2: array[0..44] of String = (''''and'''',''''as'''',''''begin'''',
''''case'''',''''char'''',''''class'''',''''const'''',''''downto'''',
''''else'''',''''end'''',''''except'''',''''finally'''',''''for'''',
''''forward'''',''''function'''',''''if'''',''''implementation'''',''''interface'''',
''''is'''',''''nil'''',''''or'''',''''private'''',''''procedure'''',''''public'''',''''raise'''',
''''repeat'''',''''string'''',''''to'''',''''try'''',''''type'''',''''unit'''',''''uses'''',''''var'''',
''''while'''',''''external'''',''''stdcall'''',''''do'''',''''until'''',''''array'''',''''of'''',
''''in'''',''''shr'''',''''shl'''',''''cos'''',''''div'''');
var
FoundAt : LongInt;
StartPos, ToEnd, i : integer;
OldCap,T : String;
FontC, BackC, C1, C2 ,C3 ,strC, strC1 : TColor;
begin
OldCap := Form.Caption;
with RichE do
begin
Font.Name := ''''Courier New'''';
Font.Size := 10;
if WordWrap then WordWrap := false;
SelectAll;
SelAttributes.color := clBlack;
SelAttributes.Style := [];
SelStart := 0;
if InVisible then
begin
Visible := False;
Form.Caption := ''''Executing Code Coloring...'''';
end;
end;
BackC := clWhite; FontC := clBlack;
C1 := clBlack; C2 := clBlack; C3 := clBlack;
strC := clBlue; strC1 := clSilver;
if Style = ''''Twilight'''' then
begin
BackC := clBlack; FontC := clWhite;
C1 := clLime; C2 := clSilver; C3 := clAqua;
strC := clYellow; strC1 := clRed;
end
else
if Style = ''''Default'''' then
begin
BackC := clWhite; FontC := clBlack;
C1 := clTeal; C2 := clMaroon; C3 := clBlue;
strC := clMaroon; strC1 := clSilver;
end
else
if Style = ''''Ocean'''' then
begin
BackC := $00FFFF80; FontC := clBlack;
C1 := clMaroon; C2 := clBlack; C3 := clBlue;
strC := clTeal; strC1 := clBlack;
end
else
if Style = ''''Classic'''' then
begin
BackC := clNavy; FontC := clYellow;
C1 := clLime; C2 := clSilver; C3 := clWhite;
strC := clAqua; strC1 := clSilver;
end
else
begin
with RichE do
begin
T := ''''{'''' Style '''' = Invalid Style [Default,Classic,Twilight,Ocean] ONLY! }'''';
Lines.Insert(0,T);
StartPos := 0;
ToEnd := Length(Text) - StartPos;
FoundAt := FindText(T, StartPos, ToEnd, [stWholeWord]);
SelStart := FoundAt;
SelLength := Length(T);
SelAttributes.Color := clRed;
SelAttributes.Style := [fsBold];
StartPos := 0;
ToEnd := Length(Text) - StartPos;
FoundAt := FindText(''''ONLY!'''', StartPos, ToEnd, [stWholeWord]);
SelStart := FoundAt;
SelLength := 4;
SelAttributes.Color := clRed;
SelAttributes.Style := [fsBold,fsUnderLine];
end;
end;
RichE.SelectAll;
RichE.color := BackC;
RichE.SelAttributes.color := FontC;
for i := 0 to 100 do
begin
with RichE do
begin
StartPos := 0;
ToEnd := Length(Text) - StartPos;
FoundAt := FindText(IntToStr(i), StartPos, ToEnd, [stWholeWord]);
while (FoundAt <> -1) do
begin
SelStart := FoundAt;
SelLength := Length(IntToStr(i));
SelAttributes.Color := C1;
SelAttributes.Style := [];
StartPos := FoundAt Length(IntToStr(i));
FoundAt := FindText(IntToStr(i), StartPos, ToEnd, [stWholeWord]);
end;
end;
end;
for i := 0 to 20 do
begin
with RichE do
begin
StartPos := 0;
ToEnd := Length(Text) - StartPos;
FoundAt := FindText(CodeC1[i], StartPos, ToEnd, []);
while (FoundAt <> -1) do
begin
SelStart := FoundAt;
SelLength := Length(CodeC1[i]);
SelAttributes.Color := C2;
StartPos := FoundAt Length(CodeC1[i]);
FoundAt := FindText(CodeC1[i], StartPos, ToEnd, []);
end;
end;
end;
for i := 0 to 44 do
begin
with RichE do
begin
StartPos := 0;
ToEnd := Length(Text) - StartPos;
FoundAt := FindText(CodeC2[i], StartPos, ToEnd, [stWholeWord]);
while (FoundAt <> -1) do
begin
SelStart := FoundAt;
SelLength := Length(CodeC2[i]);
SelAttributes.Color := C3;
SelAttributes.Style := [fsBold];
StartPos := FoundAt Length(CodeC2[i]);
FoundAt := FindText(CodeC2[i], StartPos, ToEnd, [stWholeWord]);
end;
end;
end;
Startpos := 0;
with RichE do
begin
FoundAt := FindText('''''''''''''''', StartPos, Length(Text), []);
while FoundAt <> -1 do
begin
SelStart := FoundAt;
Startpos := FoundAt 1;
FoundAt := FindText('''''''''''''''', StartPos, Length(Text), []);
if FoundAt <> -1 then
begin
SelLength := (FoundAt - selstart) 1;
SelAttributes.Style := [];
SelAttributes.Color := strC;
StartPos := FoundAt 1;
FoundAt := FindText('''''''''''''''', StartPos, Length(Text), []);
end;
end;
end;
Startpos := 0;
with RichE do
begin
FoundAt := FindText(''''{'''', StartPos, Length(Text), []);
while FoundAt <> -1 do
begin
SelStart := FoundAt;
Startpos := FoundAt 1;
FoundAt := FindText(''''}'''', StartPos, Length(Text), []);
if FoundAt <> -1 then
begin
SelLength := (FoundAt - selstart) 1;
SelAttributes.Style := [];
SelAttributes.Color := strC1;
StartPos := FoundAt 1;
FoundAt := FindText(''''{'''', StartPos, Length(Text), []);
end;
end;
end;
if InVisible then
begin
RichE.Visible := True;
Form.Caption := OldCap;
end;
RichE.SelStart := 0;
end;
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关文章
LED数码管仿真显示程序 南充市市级机关评估系统 (DBISAM “Delphi 书评” Sql Server数据库安装示例04225 Web Application 開 發 利 器 - Delphi中远程控制的实现 利用Delphi中的画布画树 儿时的编程算法心得笔记 创建Photoshop式浮动窗口应用程 图书管理系统 (BDE Paradox)
热点关注
- AnsiString?PChar?赋值
- Report Machine 3.0 (报表
- MapX v5.02.25 破解文件
- WDBOX v1.0 多选下拉列表
- 用Delphi开发视频聊天软件
- Delphi客户服务器应用开发
- AES 加密算法函数包及演示
- ACCESS/SQL 数据库存取图
- 熊猫烧香核心源码(Delphi
- 把整个网页保存成JPG图片(
- 高级计算器 V1.0 (含代码)
- Delphi版模仿熊猫烧香病毒
- 精确定位打印程序 (含源代
- Delphi文件管理(六)
- 将数字四舍五入保留两位小
- 绝地程序编辑器 v1.0.1.4
- 关于自定义界面的方案(Eas
- Delphi 对象链接与嵌入(
- 用HTML页面做程序界面演示
- Delphi模拟最小化恢复关闭
- 用Delphi实现24位真彩色图
- 限制客户机运行程序 (有关
- Delphi中用API实现在MSN的
- DELPHI数据库应用程序的开
- Delphi 动态链接库编程(
- QQ号码自动申请器及源代码
- Win9x 下调整系统时钟控件
- delphi中的XML解析控件TXM
- 几个游戏内存修改器代码
- TT8 DevExpress 最新系列
- Tnt Delphi Unicode Contr
- DelphiTwain 2004-1-20 (
- Delphi中数据的自动录入
- 经验技巧:分享两条Delphi
- Delphi中Hash表的使用方法
IDC资讯
虚拟主机
域名注册
托管租用
vps主机
智能建站
网站运营
建站经验
策划盈利
搜索优化
网站推广
免费资源
网站联盟
联盟新闻
联盟介绍
联盟点评
网赚技巧
行业资讯
业界动态
搜索引擎
网络游戏
门户动态
电子商务
广告传媒
网络编程
Asp.Net编程
Asp编程
Php编程
Xml编程
Access
Mssql
Mysql
其它
服务器技术
Web服务器
Ftp服务器
Mail服务器
Dns服务器
安全防护
软件技巧
其它软件
Word
Excel
Powerpoint
Ghost
Vista
QQ空间
QQ
FlashGet
迅雷
Internet Explorer
网页制作
FrontPages
Dreamweaver
Javascript
css
photoshop
fireworks
Flash
程序设计
Java技术
C/C++
VB
delphi
网络知识
网络协议
网络安全
网络管理
组网方案
Cisco技术
操作系统
Win2000
WinXP
Win2003
Mac OS
Linux
FreeBSD




