DBrichedit和 richedit用法一样,只是DBrichedit能连接数据库。
但richedit更加灵活。
//richedit属性
RichEdit.font属性可以设置字体(字符集,颜色,字体,大小,效果,字型);
RichEdit.color属性可以设置背景色;
Richedit1.Paragraph.Alignment属性可以设置段落的
居左(taLeftJustify),居右(taRightJustify)、居中(taCenter);
RichEdit.ScrollBars:=ssBoth;文字不满框时滚动条消失,满框时滚动条出现。
Richedit.Paragraph.FirstIndent //段落首行缩进
Richedit.Paragraph.LeftIndent //段落左缩进
Richedit.Paragraph.RightIndent //段落右缩进
Richedit.MaxLength //输入字数的限制
行距好像没有现成的属性
//在RichEdit的OnSelectionChange事件处理过程中加入适当的代码可以实时地获得当前光标位置,示例如下:
procedure TForm1.RichEdit1SelectionChange(Sender: TObject);
var
X, Y: LongInt;
begin
Y := SendMessage(RichEdit1.Handle, EM_LINEFROMCHAR,
RichEdit1.SelStart, 0);
X := RichEdit1.SelStart -
SendMessage(RichEdit1.Handle, EM_LINEINDEX, Y, 0);
Inc(Y); // 光标的行号
Inc(X); // 光标的列号
end;
//从数据库中装入文字
st := TMemoryStream.Create;
try
TBlobField(ADOQuery2.FieldByName('beizhu')).SaveToStream(st);
st.Position := 0;
DBRichEdit1.Lines.LoadFromStream(st);
finally
st.Free;
end;
//一个RichEdit里面的文本不断增多(并非用户键入),如何能总是看到最后一行呢?
richedit1.Perform(EM_SCROLL,SB_BOTTOM,0);
//RichEdit中删除某一特定字符
Delete(RichEdit1.Lines[0],Pos('{',RichEdit1.Lines[0]),1)出错
如果MyString := RichEdit1.Lines[0],然后在MyString中删除,再
RichEdit1.Lines[0]:=MyString,所有的格式都没有了
//如何改变RICHEDIT中的部分字的字体
with RichEdit1 do
begin
selStart:=5;
selLength:=10;
SelAttributes.Size:=20;
SelAttributes.Name:='宋体';
...
end;
//在RichEdit1中有一个正常的文本,想保存下来
if SaveDialog1.Execute then
begin
RichEdit1.Lines.SaveToFile(SaveDialog1.FileName);
//memo1.Lines.SaveToFile(SaveDialog1.FileName);
end;
//得到的是有其它字符混在一在的文本.
RichEdit1.plainText设为true;
//在RichEdit 中显示多个文件的内容
//下面的代码可以将Unit1.pas显示两次,稍加修改就可以显示多个不同的文件。
RichEdit1.Lines.Clear;
with RichEdit2 do
begin
Lines.LoadFromFile('Unit1.pas');
SelectAll;
CutToClipboard;
end;
with RichEdit1 do
begin
PasteFromClipboard;
Lines.Add('');
end;
with RichEdit2 do
begin
Lines.Clear;
Lines.LoadFromFile('Unit1.pas');
SelectAll;
CutToClipboard;
end;
with RichEdit1 do
begin
PasteFromClipboard;
Lines.Add('');
end;
//插入文件
procedure TFormMain.MenuInsertFileClick(Sender: TObject);
var
InsertString:TStringlist;
begin
with OpenDialog1 do
begin
InitialDir:=ExtractFilePath(FileName);
if Execute then
begin
with RichEdit1 do
SelStart:=SelStart+SelLength;
InsertString:=TStringlist.Create;
InsertString.LoadFromFile(OpenDialog1.FileName);
RichEdit1.SelText:=InsertString.Text;
end;
end;
end;
//同一窗体有两个RxRichEdit,要把其中一个的文本(包括所有格式)接到另一个的末尾,请问如何做
RxRichEdit1: TRxRichEdit;
RxRichEdit2: TRxRichEdit;
button1.click
for i:=0 to RxRichEdit1.lines.count-1 do
begin
RxRichEdit2.lines.add(RxRichEdit1.lines.strings
);
end;
//程序通过RichEdit1.Lines.Append添加信息到RichEdit1中,
//如何通过程序控制RichEdit的ScrollBar? 使最新添加的行始终显示在RichEdit1中.
(richedit1的文本框的卷屏)
RichEdit1.SelLength := 0;
currentline := Richedit1.Perform(EM_LINEFROMCHAR, Richedit1.SelStart, 0 );
Richedit1.SelStart := richEdit1.perform( EM_LINEINDEX, currentline+1, 0 );
RichEdit1.Perform( EM_SCROLLCARET, 0, 0 );
//关于TRichEdit对齐方式的奇怪问题
RichEdit1.SelAttributes.Size:=dbword0.Font.Size;
RichEdit1.SelAttributes.name:=dbword0.font.name;
RichEdit1.SelAttributes.Color:=dbword0.Font.Color;
我在程序中加上 RichEdit1.Alignment:=taCenter;也不行。请帮我看看是怎么回事?应该怎么做才能保持中对齐呢?
用self.RichEdit1.Paragraph.Alignment:=taCenter;试试
//如何用sql语句保存TRichEdit的值到数据库?
建一个text的字段,提示:
adoquery1.Parameters.ParamByName('text的字段').Assign(memo_yyfx.Lines);
建立一個memo類型的字段(如果你是用sql數據庫就是ntext類型)
insert into mytable (memo) values (richedit.text)
query1.text:='insert into mytable (field1) values ('''+RichEdit1.Text+''')';
或
query1.Text:='insert into mytable (field1) values pam)';
query1.Params[0]:=RichEdit1.Text;
query1.ExecSQL;
用BLOB 字段可以解决这个问题:
richedit1.Lines.SaveToFile('c:/temprich.rtf')
TBlobField(table1.FieldByName('fieldName')).LoadFromFile(''c:/temprich.rtf');
//如何将RichEdit的内容保存到数据库中
用流
1。保存到字段
procedure TForm1.Button2Click(Sender: TObject);
var
st: TMemoryStream;
begin
st := TMemoryStream.Create;
try
with RichEdit1 do
begin
SelStart := 0;
SelLength := Length(Text);
SelAttributes.Protected := FALSE;
Lines.SaveToStream(st);
end;
st.Position := 0;
Table1.Edit;
TBlobField(Table1.FieldByName('Bz')).LoadFromStream(st);
Table1.Post;
finally
st.Free;
end;
end;
2.读入到RichEdit
procedure TForm1.Button3Click(Sender: TObject);
var
st: TMemoryStream;
begin
st := TMemoryStream.Create;
try
TBlobField(Table1.FieldByName('Bz')).SaveToStream(st);
st.Position := 0;
RichEdit1.Lines.LoadFromStream(st);
finally
st.Free;
end;
end;
//RichEdit的行间距能调整了!
现贴出来分享:
uses
RichEdit;
procedure RE_SetLineSpacing(ARichEdit: TRichEdit; lineSpacing: Byte);
var
pf2: ParaFormat2;
begin
FillChar(pf2, SizeOf(pf2), 0);
pf2.cbSize := SizeOf(PARAFORMAT2);
pf2.dwMask := PFM_LINESPACING;
pf2.bLineSpacingRule := lineSpacing;
SendMessage(ARichEdit.Handle, EM_SETPARAFORMAT, 0, Longint(@pf2));
end;
//Example: Setlinespacing to 1:
procedure TForm1.Button1Click(Sender:TObject);
begin
RE_SetLineSpacing(RichEdit1, 1);
end;