如何实现Memo中部分字符颜色的改变?(20分)

  • 主题发起人 主题发起人 sunnysheng
  • 开始时间 开始时间
S

sunnysheng

Unregistered / Unconfirmed
GUEST, unregistred user!
如何实现Memo中部分字符颜色的改变?
 
Memo实现不了,用RichEdit吧,比如选中的文件变成红色<br>RichEdit.SelAttributes.Color:=clRed;<br>
 
楼上说的对,Memo功能太弱了,你可以用RichEdit,或者找其他的替代品。<br>http://www.playicq.com 有一个RichEdit2,有兴趣的话你可以试试。
 
Memo是不行的,用RichEdit没错
 
谢谢各位! <br>&nbsp; 那能不能使字符的背影色改变。?
 
同样原因,不行。 
 
如果是要改变memo的背景颜色可以改变<br>memo1.color<br>如果是字体颜色可以改变<br>memo1.font.color
 
问一下,richedit怎么用啊?
 
和memo一样的用!
 
unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls, Menus, ComCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; PageControl1: TPageControl;<br>&nbsp; &nbsp; TabSheet1: TTabSheet;<br>&nbsp; &nbsp; TabSheet2: TTabSheet;<br>&nbsp; &nbsp; RichEdit1: TRichEdit;<br>&nbsp; &nbsp; Memo1: TMemo;<br>&nbsp; &nbsp; Button3: TButton;<br>&nbsp; &nbsp; TabSheet3: TTabSheet;<br>&nbsp; &nbsp; RichEdit2: TRichEdit;<br>&nbsp; &nbsp; Label2: TLabel;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure RichEdit1KeyUp(Sender: TObject; var Key: Word;<br>&nbsp; &nbsp; &nbsp; Shift: TShiftState);<br>&nbsp; &nbsp; procedure Button3Click(Sender: TObject);<br>&nbsp; &nbsp; procedure RichEdit1Change(Sender: TObject);<br>&nbsp; &nbsp; procedure FormShow(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; &nbsp; function whether(st:string):Boolean;<br>&nbsp; &nbsp; function zuai(st:string):Boolean;<br>&nbsp; &nbsp; function zhaoqian(si:longint):longint;<br>&nbsp; &nbsp; function zhaohou(si:longint):longint;<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br>&nbsp;uses StrUtils,unit2;<br>&nbsp;var<br>&nbsp; filename,filename2:string;<br>&nbsp; &nbsp;yqian,yhou:longint;<br>{$R *.dfm}<br>function TForm1.whether(st:string):Boolean;<br>var<br>&nbsp;i:longint;<br>&nbsp;s:string;<br>begin<br>&nbsp;Result:=false;<br>&nbsp;for i:=0 to memo1.lines.Count-1 do<br>&nbsp; &nbsp;begin//1<br>&nbsp; &nbsp;s:=trim(memo1.lines.Strings );<br>&nbsp; &nbsp; &nbsp; if st=s then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin//2<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result:=true;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;//2<br>&nbsp; &nbsp;end;//1<br>end;<br><br>function TForm1.zuai(st:string):Boolean;<br>var<br>&nbsp;i:longint;<br>&nbsp;s:string;<br>&nbsp;zuaizi1:array [0..100] of string;<br>begin<br>zuaizi1[0]:=':';<br>zuaizi1[1]:='=';<br>zuaizi1[2]:='/*';<br>zuaizi1[3]:='*/';<br>zuaizi1[4]:='//';<br>zuaizi1[5]:=';';<br>zuaizi1[6]:='::';<br>zuaizi1[7]:='(';<br>zuaizi1[8]:=')';<br>zuaizi1[9]:='[';<br>zuaizi1[10]:=']';<br>zuaizi1[11]:='^';<br>zuaizi1[12]:='.';<br>zuaizi1[13]:=':';<br>zuaizi1[14]:='^.';<br>zuaizi1[15]:='*';<br>zuaizi1[16]:='/';<br>zuaizi1[17]:=',';<br>zuaizi1[18]:='"';<br>zuaizi1[19]:='{';<br>zuaizi1[20]:='}';<br>zuaizi1[21]:=')';<br>zuaizi1[22]:=#13;<br>zuaizi1[23]:=#10;<br>zuaizi1[24]:=' ';<br><br>&nbsp;Result:=false;<br>&nbsp;for i:=0 to 24 do<br>&nbsp; &nbsp;begin//1<br>&nbsp; &nbsp; &nbsp; if st=zuaizi1 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin//2<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result:=true;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;//2<br>&nbsp; &nbsp;end;//1<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp;j,linestrlen,cishu:longint;<br>&nbsp;linestr,analyse,linefen2:string;<br>&nbsp;linepos:array of longint;<br>begin<br>&nbsp; &nbsp; &nbsp;cishu:=0;<br>&nbsp; &nbsp; &nbsp;setlength(linepos,cishu);<br>&nbsp; &nbsp; &nbsp;linestr:=RichEdit1.Text;<br>&nbsp; &nbsp; &nbsp;if linestr='' then exit;<br>&nbsp; &nbsp; &nbsp;linestrlen:=Length(linestr);<br>&nbsp; &nbsp; &nbsp;form2.Show ;<br>&nbsp; &nbsp; &nbsp;form2.ProgressBar2.Progress &nbsp;:=0;<br>&nbsp; &nbsp; &nbsp;form2.ProgressBar2.MaxValue := linestrlen;<br>&nbsp; &nbsp; &nbsp;analyse:='';<br>&nbsp; &nbsp; &nbsp;for j:=1 to linestrlen do<br>&nbsp; &nbsp; &nbsp;begin//2<br>&nbsp; &nbsp; &nbsp; application.ProcessMessages ;<br>&nbsp; &nbsp; &nbsp; form2.ProgressBar2.Progress &nbsp;:= form2.ProgressBar2.Progress +1;<br>&nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;linefen2:=linestr[j];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if zuai(linefen2) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin//3<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;setlength(linepos,cishu+1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;linepos[cishu]:=j;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if cishu-1&gt;=0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin//4<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RichEdit1.SelStart :=linepos[cishu-1];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RichEdit1.SelLength :=linepos[cishu]-RichEdit1.SelStart-1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end//4<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin//5<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RichEdit1.SelStart :=0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RichEdit1.SelLength :=linepos[cishu]-1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;//6<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if whether(analyse) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin//4<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; form2.Edit1.Text &nbsp;:=analyse;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; form2.Edit1.Update ;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RichEdit1.SelAttributes.Color := clblue;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RichEdit1.SelAttributes.Style := [fsBold];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end//4<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin//6<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; form2.Edit1.Text :=linefen2;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; form2.Edit1.Update ;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RichEdit1.SelAttributes.Color := clBlack;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RichEdit1.SelAttributes.Style := [];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;//6<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inc(cishu);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;analyse:='';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end//3<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin//4<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; analyse:=analyse+linefen2;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;//4<br>&nbsp; &nbsp; &nbsp;end;//2<br>&nbsp; form2.Close ;<br>end;<br><br><br>procedure TForm1.Button2Click(Sender: TObject);<br>var<br>&nbsp;i:longint;<br>&nbsp;s:string;<br>&nbsp;ass:TStringList;<br>begin<br>&nbsp;ass:=TStringList.Create ;<br>&nbsp;s:='';<br>&nbsp;for i:=0 to richedit1.Lines.Count -1 do<br>&nbsp; begin//1<br>&nbsp; &nbsp; s:=s+ richedit1.Lines.Strings +#13#10;<br>&nbsp; end;//1<br>&nbsp; ass.Clear ;<br>&nbsp; ass.Add (s);<br>&nbsp; ass.SaveToFile(filename);<br>end;<br><br><br>function TForm1.zhaoqian(si:longint):longint;<br>var<br>&nbsp;i,j:longint;<br>&nbsp;s,tmp:string;<br>begin<br>&nbsp;j:=0;<br>&nbsp;tmp:=copy(richedit1.Text,1,si-1) ;<br>&nbsp;for i:=si-1 downto 1 do<br>&nbsp; begin//1<br>&nbsp; &nbsp; if zuai(tmp) then<br>&nbsp; &nbsp; &nbsp; begin//2<br>&nbsp; &nbsp; &nbsp; &nbsp;j:=i+1;<br>&nbsp; &nbsp; &nbsp; &nbsp;Result:=j;<br>&nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp; &nbsp;break;<br>&nbsp; &nbsp; &nbsp; end;//2<br>&nbsp; end;//1<br>end;<br><br>function TForm1.zhaohou(si:longint):longint;<br>var<br>&nbsp;i,j,t:longint;<br>&nbsp;s,tmp:string;<br>begin<br>&nbsp;j:=0;<br>&nbsp;tmp:=copy(richedit1.Text,si,length(richedit1.Text)) ;<br>&nbsp;tmp:=trim(tmp);<br>&nbsp;tmp:=tmp+' ';<br>&nbsp;for i:=1 to length(tmp) do<br>&nbsp; begin//1<br>&nbsp; &nbsp; if zuai(tmp) then<br>&nbsp; &nbsp; &nbsp; begin//2<br>&nbsp; &nbsp; &nbsp; &nbsp;j:=i-1;<br>&nbsp; &nbsp; &nbsp; &nbsp;t:=si+j;<br>&nbsp; &nbsp; &nbsp; &nbsp;Result:=t;<br>&nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp; &nbsp;break;<br>&nbsp; &nbsp; &nbsp; end;//2<br>&nbsp; end;//1<br>end;<br><br>procedure TForm1.Button3Click(Sender: TObject);<br>var<br>&nbsp;i:longint;<br>&nbsp;s:string;<br>&nbsp;ass:TStringList;<br>begin<br>&nbsp;ass:=TStringList.Create ;<br>&nbsp;s:='';<br>&nbsp;for i:=0 to Memo1.Lines.Count -1 do<br>&nbsp; begin//1<br>&nbsp; &nbsp; s:=s+ Memo1.Lines.Strings +#13#10;<br>&nbsp; end;//1<br>&nbsp; ass.Clear ;<br>&nbsp; ass.Add (s);<br>&nbsp; ass.SaveToFile(filename2);<br>end;<br><br><br>procedure TForm1.RichEdit1KeyUp(Sender: TObject; var Key: Word;<br>&nbsp; Shift: TShiftState);<br>var<br>&nbsp;analyse:string;<br>begin<br>{<br>&nbsp; &nbsp; &nbsp;Label2.Caption :=inttostr(richedit1.CaretPos.x );<br>&nbsp; &nbsp; &nbsp;if (key=17) then exit;<br>&nbsp; &nbsp; &nbsp;if (key=39) then exit;<br>&nbsp; &nbsp; &nbsp;if (key=37) then exit;<br>&nbsp; &nbsp; &nbsp; &nbsp;yqian:=zhaoqian(richedit1.CaretPos.x);<br>&nbsp; &nbsp; &nbsp; &nbsp;yhou:=zhaohou(richedit1.CaretPos.x );<br>}<br>end;<br><br>procedure TForm1.RichEdit1Change(Sender: TObject);<br>var<br>&nbsp;analyse:string;<br>begin<br>&nbsp; { &nbsp; if yhou&gt;yqian then<br>&nbsp; &nbsp; &nbsp; begin//1<br>&nbsp; &nbsp; &nbsp; &nbsp; RichEdit1.SelStart :=yqian-1 ;<br>&nbsp; &nbsp; &nbsp; &nbsp; RichEdit1.SelLength :=yhou-yqian+1;<br>&nbsp; &nbsp; &nbsp; &nbsp; analyse:=copy(richedit1.Text,yqian,yhou-yqian);<br>&nbsp; &nbsp; &nbsp; &nbsp; if whether(analyse) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin//5<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RichEdit1.SelAttributes.Color :=clblue;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RichEdit1.SelAttributes.Style := [fsBold];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end//5<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin//6<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RichEdit1.SelAttributes.Color := clBlack;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RichEdit1.SelAttributes.Style := [];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;//6<br><br>&nbsp; &nbsp; &nbsp; end;//1<br>&nbsp; }<br>end;<br><br>procedure TForm1.FormShow(Sender: TObject);<br>var<br>&nbsp;s:string;<br>begin<br>s:=extractfilepath(application.ExeName );<br>if rightstr(s,1)&lt;&gt;'/' &nbsp;then<br>&nbsp; &nbsp;s:=s+'/';<br><br>s:=s+'baoliuzi.txt';<br>if fileexists(s) then<br>begin//1<br>&nbsp; &nbsp;memo1.Lines.LoadFromFile(s);<br>&nbsp; &nbsp;filename2:=s;<br>end;//1<br><br><br>s:=extractfilepath(application.ExeName );<br>if rightstr(s,1)&lt;&gt;'/' &nbsp;then<br>&nbsp; &nbsp;s:=s+'/';<br>s:=s+'a.rtf';<br>if fileexists(s) then<br>begin//1<br>&nbsp; &nbsp;richedit1.Lines.LoadFromFile(s);<br>&nbsp; &nbsp;filename:=s;<br>&nbsp; &nbsp;Button1Click(nil);<br>end;//1<br><br>end;<br><br>end.<br>
 
我那个打字程序就可以变的嘛。。。不过,是RichEdit。。。。
 
后退
顶部